Thread: a sum equal to or in excess of 100

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Central Coast, California
    Posts
    9

    a sum equal to or in excess of 100

    Code:
    #include <stdio.h>
    int main (void)
    {
    	int value, sum = 0;
    
    	printf ("Enter a value: ");
    	scanf ("%i", &value);
    
    	while ( sum > 100 )
    	{
    		scanf ("%i", &value);
    	}
    	while ( (value < 0) || (value > 25) )
    	{
    		printf ("Out of range, please re-enter: ");
    		scanf ("%i", &value);
    	}
    
    		sum = sum + value;
    		printf ("\nThe sum is: %i\n\n", sum);
    
    	return 0;
    }
    This program is to take user entered integers ranging from 0 to 25 until the sum of those entered integers is equal to or in excess of 100. I know the 0 to 25 constraint works fine, it's the sum part that isn't so fine.

    Currently what the program does is it takes the first acceptable entered integer and just prints that as the sum as well. Ex: if I input 25 it will say the sum is 25.

    It should keep asking for numbers until a sum of 100 or greater is reached.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    while ( sum > 100 )
    I only started learning C yesterday, but I think that that should be:
    while ( sum < 100 )
    ...because you want that loop to repeat until the sum is >= to 100.

    I might be wrong though. I have less than 1 month of programming experience so yeah...

    Edit: You have additional problems with your code though. Look Carefully.
    Last edited by Xlayer; 10-07-2007 at 06:01 PM.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Central Coast, California
    Posts
    9
    Xlayer- I tried your suggestion and it appears to have made matters worse. Doing that destroys my constraint checking loop and the program just keeps accepting numbers regardless and with no end.

    I'm a beginning programmer too, no worries.

    Edit: Would you mind pointing out these errors to me? Maybe changing those errors will make your solution work.
    Last edited by lyoncourt; 10-07-2007 at 05:27 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Presumably, to work, the accumulation of sum (sum = sum + value; which can also be expressed as sum += value;) needs to be within the loop that checks the value of sum.

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Do you understand what each line of code does? Do you understand the way while loops work?
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're checking the value of sum when sum is 0.... I don't get it. Think about what you're doing. It's most likely not what you want. Set sum first and then go ahead and check its value.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Yeah, you have 2 separate loops. I would re-think how you are solving this problem. You could write this program, with one loop that executes until the sum equals or is greater than 100. Also, are you supposed to print the sum after each number is inputed or only, after the sum exceeds or is equal to 100? Also I would prompt the user for the first number and then also, prompt the user for the rest of the values because it would be confusing for anyone using your program. You could also just prompt the user once by saying: "Enter integers separated by pressing enter until the sum is >= to 100." Or better yet, write your own prompts so it isn't so hard to understand (I am a bad writer). Also, consider Just Prompting with "Enter a Number" for the first time and then "Enter Another Number" for each time leading up to the sum being >= to 100.
    Last edited by Xlayer; 10-07-2007 at 06:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  3. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. Unconvertable?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2002, 08:10 PM