keeping track of values

This is a discussion on keeping track of values within the C Programming forums, part of the General Programming Boards category; if you have a loop that asks for the user to input a value and the loop doesn't end until ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    41

    keeping track of values

    if you have a loop that asks for the user to input a value and the loop doesn't end until the sentinal, how can you keep track of all the values entered and add them together?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A sum and an input?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    i.e. scanf("%d", &d)...if this is in a loop an dthe user enters values 5, 8, & 9 how do you write the language to add these together....i.e. add first loop u have 5, next loop you add 5 to 8, next loop you add 13 to 9....how do you write that?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
       sum = 0;
       while ( scanf("%d", &d) == 1 )
       {
          sum += d;
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get pixel values from pic
    By Leite33 in forum Windows Programming
    Replies: 4
    Last Post: 09-12-2006, 01:13 PM
  2. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  3. keeping values of scanf
    By n00by in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 03:38 AM
  4. F1 track analyser
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-22-2002, 08:48 AM
  5. keeping track of # of files
    By blind_collision in forum C Programming
    Replies: 1
    Last Post: 10-13-2001, 09:10 AM

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