Need help modifying my program

This is a discussion on Need help modifying my program within the C Programming forums, part of the General Programming Boards category; I wrote the program below, but I want to change it so that it calculates after every number is entered. ...

  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,682
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    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 09: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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    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,682
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    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, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09: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, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21