Thread: Basic Scanf() help

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    2

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Put it in a loop. You've only told it to read one number.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    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.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    2
    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.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    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.

  7. #7
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    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;
    }
    Last edited by Scribbler; 10-11-2004 at 11:04 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  2. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  3. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM