Thread: Need help modifying my program

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    34

    Need help modifying my program

    I wrote the program below, but I want to change it so that it calculates after every number is entered.
    Example:
    Enter your number:
    5
    Total: 5
    17
    Total: 22

    And so on.

    Is there any way to do this?

    Code:
    #include <stdio.h>
    
    int main()
    {
        int x
        int sum = 0;
        
        printf("Enter your numbers: <EOF> to stop. \n");
        while(scanf(&#37;d", &x) != EOF)
        sum += x;
        printf("\nThe total is: %d\n", sum);
        return 0;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I wrote the program below, but I want to change it so that it calculates after every number is entered.
    To achive that u need an array. Declare an array and read value from user and store them in array. And then add them all.

    ssharish

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wait a minute...
    What's wrong with your current code? It calcs together the total and displays it afterwards.
    Changing it to calculate everything afterwards just adds complexity which is unneccesary.
    Last edited by Elysia; 11-11-2007 at 10:06 PM.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    I need it to calculate the new number after each number is entered instead of after all the numbers are entered. I think I figured it out though, by adding brackets around while and sum.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Northstar View Post
    I need it to calculate the new number after each number is entered instead of after all the numbers are entered.
    Now that's just contrary to what you mentioned in your original post! Which is it?

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Northstar View Post
    I need it to calculate the new number after each number is entered instead of after all the numbers are entered. I think I figured it out though, by adding brackets around while and sum.
    I thinks this is not the one which u asked in your first post. I through u wanted all numbers to be read and then add them together?

    ssharish

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Oh, whoops, I was very unclear. "I want to change it so that it calculates after every number is entered." <-- That was supposed to mean that I want it to calculate after each new number is entered. Sorry for the confusion, should have been a lot more clear.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's what you're doing in your code.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Is it? My compiler does not like getchar and so it never shows me what the program actually comes up with, but I did the program in the lab and it just added them all together at once. I think adding brackets where I said above would solve it though?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM