-
Basic Scanf() help
Hi, new to the boards, got a n00b question. Basically, what I want to do is have the user in put a series of numbers i.e.
3 5 2 1 6 4, then have the numbers summed up and added to a variable. This is what I have.
scanf("%d", &input);
total+=input;
However, it only reads the first number, in this case 3, and outputs 3 when I use a printf(). Why is that? I thought scanf() just skips over whitespace? What's the solution to this?
Thanks.
-
Put it in a loop. You've only told it to read one number.
Quzah.
-
Code:
scanf("%d %d %d", &input1, &input2, &input3);
Would also work if you absolutely know there will be only 3 numbers entered. scanf does know to ignore spaces, and that's exactly what it's doing in your example, ignoring everything after the space, unless you specify (above) that there will be more.
Hope that clears up a little bit.
-
Well, the thing is that I'm not supposed to specify the number of digits. It's completely random. So declaring %d over and over again isn't an option. As for the loop, how would I do that?
While(1)
{
scanf whatever
}
???
Thanks.
-
Well for starters, go read the Announcements before you ........ everyone off. This will teach you how to use code tags.
Second, you have to have some way for the user to stop inputting numbers. Once you've figured out how you want to stop getting numbers from them, you figure out how to put that together.
I'd suggest writing out some pseudocode on what you want. Something like:
Code:
while not done getting numbers
read a number
if number is somethingorother
stop getting numbers
else
add number to sum
There are multiple ways to do this. However, rather than just give you the answer, we'd like to see your attempt at it first. Furthermore, you'll have to figure out how you want to "be done" getting numbers and incorporate that, both in your question to us, and in your own code solution/attempt.
Otherwise, you're asking an open-ended question, and answers we give may or may not work for your scenario.
Quzah.
-
Arrays and Loops
Arrays would be most convenient for you. Arrays and loops practically go hand in hand, also. Why, because you can affect every element in an array thanks to the sequential ability of a loop. The loop you might want depends on knowing how many times you want the loop to repeat itself. I have a project that asks me to do the same thing. I personally used a for loop. How many numbers are you supposed to request the user to enter, by the way?
Just in case, it's best to declare an array with a good range of elements inside of it. For instance, Array[100] or Array[SIZE]
Array[SIZE] is extremely versatile because you can change it anytime. This is how it works.
Code:
#define SIZE 100
main()
{
int i;
int Array[SIZE];
int n; /*call it anything you wish. this is the number of times you wish the user to enter values*/
return 0;
}
Then declare a variable that will represent your total sum, such as int total=0
Then use your loop, like the for loop...initialize it, set a condition at which the loop has to stop, and increment it. Inside the first for loop you should have the input prompt, and use scanf("%d", &Array[i]) or something to read each value and store it.
On your second for loop, use the same things as the first one to fill in the parentheses, and inside the loop itself, use an expression that looks like:
Code:
total=total+Array[i];
Or something of the sort. This will have the values adding up as the loop continues. Outside the second and hopefully last for loop, you should probably have the output shown lol. Hope that helps.
-
Or you can simply use the getchar() function to check for spaces in the input buffer... this works easily enough...
Code:
#include <stdio.h>
int main()
{
int userInput;
int total;
scanf ("%d" , &total);
while (getchar() == ' ')
{
scanf ("%d" , &userInput);
total += userInput;
}
printf ("total %d\n", total);
return 0;
}
-
*slams head on desk repetedly* I guess none of you paid attention to the fact that you shouldn't do everything for them. Damn! How is anyone supposed to learn if they never try anything on their own because it's all handed to them?
That's it! You're all getting a sig out of this one.
Quzah.