n00b input help

M

moostafa

Hi,
I'm writing a program that performs arithmetic operations on integers.
I want to be able to type in a bunch of integers seperated by any
amount of white space then terminate input with a non-integer
character. I plan to put my input into an array, and while I have a max
size I don't have a min and don't know exactly how many arguments to
expect.
I would really appreciate any ideas.

Cheers.
 
P

pete

moostafa said:
Hi,
I'm writing a program that performs arithmetic operations on integers.
I want to be able to type in a bunch of integers seperated by any
amount of white space then terminate input with a non-integer
character.

You can terminate with the Enter key, instead, just as simply.
I plan to put my input into an array, and while I have a max
size I don't have a min and don't know exactly how many arguments to
expect.
I would really appreciate any ideas.

Are these integers coming in as command line arguments
or as querried input?
 
C

CBFalconer

moostafa said:
I'm writing a program that performs arithmetic operations on
integers. I want to be able to type in a bunch of integers
seperated by any amount of white space then terminate input with
a non-integer character. I plan to put my input into an array,
and while I have a max size I don't have a min and don't know
exactly how many arguments to expect.
I would really appreciate any ideas.

The usual solution for input of unknown length is a linked list.
Once you have determined the total input you can then use it as is,
sort it, convert it into an array, or what-not. Linked lists are
best sorted with mergesort.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
M

moostafa

You can terminate with the Enter key, instead, just as simply.

It's for a school assignment, otherwise I would just leave it as the
enter key.
Are these integers coming in as command line arguments
or as querried input?

I'm not sure what you mean by command line arguments. I want it to look
something like this:

Computing, please input list of integers
> 1 2
> 3 4
> a
The sum of your integers is etc. etc.

I tried using scanf but it only ignores the new line if you specify how
many arguments you're expecting - which I can't do.
The usual solution for input of unknown length is a linked list.
Once you have determined the total input you can then use it as is,
sort it, convert it into an array, or what-not.

Don't you still need to read the input using gets or something to put
it into a linked list? I'm not really sure how they work, but I thought
that if I was giving my input all at once it would just go into one
list.
 
K

Keith Thompson

moostafa said:
Don't you still need to read the input using gets or something to put
it into a linked list? I'm not really sure how they work, but I thought
that if I was giving my input all at once it would just go into one
list.

Never use gets(). If you want to read a line at a time (which is
usually the right thing to do), use fgets(). gets() cannot be used
safely (except in rare and obscure circumstances you're unlikely ever
to run into), and there's a great deal of support for removing it from
the language.
 
B

Ben Bacarisse

It's for a school assignment, otherwise I would just leave it as the enter
key.


I'm not sure what you mean by command line arguments. I want it to look
something like this:

Computing, please input list of integers
The sum of your integers is etc. etc.

I tried using scanf but it only ignores the new line if you specify how
many arguments you're expecting - which I can't do.

I don't know what you mean by this. Writing:

scanf("%d", &number)

will read an integer into the int variable "number" (after skipping over
any white space characters). The return value will be either 1 (meaning 1
number read and converted); 0 (meaning something was found that could not
be part of a number) or EOF (meaning that something else went wrong). It
is not a coincidence that this is almost exactly what you want -- a number
read in or an indication of what went wrong if no number was found!
Don't you still need to read the input using gets or something to put it
into a linked list? I'm not really sure how they work, but I thought that
if I was giving my input all at once it would just go into one list.

This reads like an early assignment so a linked list may be overkill. You
need a list (or some other storage) if the calculations you want to do
require all the numbers to available at once (for example finding the
middle number[1]) but not if you want to just add them up.

And may I second the advice from KT -- don't use gets. Ever. You need to
read the number and calculate with it or store it somewhere (like a linked
list) if you need to keep them all for later.

[1] in the most general case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top