tags:

views:

140

answers:

7

I know this probably really simple but Im not sure what im doing wrong...

The assignment states:

For the second program for this lab, you are to have the user enter an integer value in the range of 10 to 50. You are to verify that the user enters a value in that range, and continue to prompt him until he does give you a value in that range.

After the user has successfully entered a value in that range, you are to display the sum of all the integers from 1 to the value entered.

I have this so far:

#include <iostream.h>

 int main () {

int num, sum;

  cout << "do-while Loop Example 2"
       << endl << endl;

  do {
    cout << "Enter a value from 10 to 50: ";
    cin >> num;
    if (num < 10 || num > 50)
      cout << "Out of range; Please try again..."
           << endl;
  } while (num < 10 || num > 50);

    {
  int i;
  int sum = 0;

  for (num = 1; num <= 50; num ++) 
  sum = sum + num;
}

  cout << endl << "The sum is " << sum << endl;

return 0;

}

Im just not sure exactly what i'm doing wrong... I keep getting the wrong sum for the total...

+2  A: 

Your loop conditions are wrong.

First, you should use a separate variable as your index (in fact you already declared one using "int i" earlier).

Second, your upper limit shouldn't 50, it's whatever the user entered.

So you want to change all "nums" in the loop to "i", and the "50" to "num".

Platinum Azure
Thanks for the help!!
Deonna
+1  A: 

Let me make sure I understand this correctly, your assignment asks for a user input for a given number and store it in num and then display a running sum of 1 up to num?

If that's the case, inside your loop you override the user's input of num when you call num = 1. You'll just calculate the running sum of 1-50 every time.

To correct that, you need to use a different variable to keep incrementing, i.e. count or the variable i since it's already been declared. Then you should loop up from i to num as long as i <= num.

Other than that, I cannot see any problems and it should work correctly.

Note to add about a good investment:

It would definitely be worth while to see if the IDE you are developing in has a debugger you can use. Debugging is a great tool to help figure out why your code is not being executing as it is intended to.

If there is no debugger (which would surprise me) might I suggest my go-to alternative method of stepping through the for loop on a sheet of paper and compare the sum to another hand-written solution already solved, i.e. num = 5 sum = 1+2+3+4+5 = 15

Hope this helps.

Anthony Forloney
The assignment wants us to add 1 +2+3+ 4 etc until we reach the number that the user input...
Deonna
Good catch. But in this case, the code compiles because there is an extra starting brace... and it should run correctly since the for loop has only one statement... I think maybe someone should link the original poster to a style guide.
Platinum Azure
@Deonna: OK, just wanted to double check I understood the question, @Platinum Azure: Thanks for letting me know, wasn't sure how it would have been interpreted.
Anthony Forloney
A: 

The for loop should be:

for (int x = 1; x <= num; x++)
{
   sum += x;

}
Bobby
it didn't work.
Deonna
According to your requirements(you are to display the sum of all the integers from 1 to the value entered), this is correct.
Bobby
You've tagged the question as C++. This would work if you're actually using a C++ compiler. If you're using a C compiler, though, you may have to take "int" outside the for loop and write a separate declaration "int x;" (and perhaps put that at the top of the function, depending on how old the compiler is).
Ben Voigt
+1  A: 

The Corrected code is

#include <iostream.h>

 int main () {

int num;

  cout << "do-while Loop Example 2"
       << endl << endl;

  do {
    cout << "Enter a value from 10 to 50: ";
    cin >> num;
    if (num < 10 || num > 50)
      cout << "Out of range; Please try again..."
           << endl;
  } while (num < 10 || num > 50);

                         //<----Removed the extra set of {}
  int i,sum=0;//<---- Changed the declaration here


  for (i= 1; i <= num; i++) //<----Change Here
  sum += i;


  cout << endl << "The sum is " << sum << endl;

return 0;

}
anijhaw
Thanks alot!!! It worked!!!
Deonna
Shouldn't the code in the for loop be sum += i. you're just adding num to sum repeatedly.
Bobby
Thanks, maybe a cut paste error or something but nice catch
anijhaw
A: 
#include <iostream.h>

 int main () {

int num, sum;    // here you define (but don't initialize) one variable named sum.

[ ... ]    

    {         // here you start an inner scope.
  int i;
  int sum = 0;    // here you define *another* `sum`, local to the inner scope.

  for (num = 1; num <= 50; num ++)
  sum = sum + num;    // here you add the numbers to the *second* sum.
}    // here the second sum goes out of scope.

  // and here you print out the first (still un-initialized) sum.
  cout << endl << "The sum is " << sum << endl;
Jerry Coffin
Thanks alot for the help!!
Deonna
+1  A: 

Actually, you can simplify the for loop into:

sum = ((num+1)*num)/2;

Credits to Carl Friederich Gauss. :D

eckesicle
A: 

Your upper bound is not 50, but the number you entered. Therefore your summation is from 1 t0 inclusive of your entered number.

Say you enter 10, logic will be. You add all the digits from 1 to 10.

jaysivo