#include <iostream>
using namespace std;
 
int main() 
{
   int sum = 0;    // Declare an int variable sum to accumulate the numbers
   int lowerbound;                // Set the initial sum to 0
   int upperbound; // Sum from 1 to this upperbound
 
   // Prompt user for an upperbound
   cout << "Enter a integer: ";
   cin >> upperbound;
   cout << "Enter another integer: ";
   cin >> lowerbound;
   while (lowerbound <= 100  && upperbound <= 100) 
   {
      sum = sum + upperbound++ + lowerbound++;

   }
   // Print the result
   cout << "The total of the numbers " << upperbound << " and " << lowerbound << " is " << sum << endl;
}

Yes the code does not calculate the numbers properly and I cannot figure out why?

Why bother with user input on the lowerbound? and not just initialize it to 1, and then have.

while(lowerbound!=upperbound)
{
sum+=lowerbound;
lowerbound++;
}

Maybe I am miss understanding your problem.

Edited 5 Years Ago by Nick Evan: Added CODE tags

I suggest replacing user input with hard coded values for testing. You should also tell us the expected output and the current output.

Have you tried stepping through the code with a debugger? This would be where I would start.

Actually, I would start with this line:

sum = sum + upperbound++ + lowerbound++;

Yikes! Why are you incrementing these things in this expression? The bounds should stay the same, and a counter is what should increment.

There is also no need to sum from 1 to the lower bound and then 1 to the upper bound. Simply sum from the lower bound to the upper bound.

Program calls for two entries as one example, "The total of the numbers between 50 and 90 is 2870"

Cannot figure out what I am doing wrong?

Start xLow at 50
start count at 0
while xLow not 90
  add 1 to count
  increment xLow

Is this really that hard?

Why did I even try to use this site? Attitude is all people respond with!

>> Why did I even try to use this site? Attitude is all people respond with!

Your refusal to read the helpful replies in this thread has nothing to do with our attitude, but more with yours.

not sure im reading this right care to explain what you wished to do that u wrote
sum = sum + upperbound++ + lowerbound++; ??
if im not mistaken your adding 1 to upperbound and 1 to lower bound
try making a change to the condition of the loop, and what the loop adds to sum.

would help me to help you if you told us what is your mathemetical algorithm :)
have a nice day !

Thank You "Depairy" that is a helpful response. I am trying to do the following
prompt the user for two integers. USING A FOR LOOP, calculate the total of the numbers between those numbers, inclusive.

Comments
Still not listening to the suggestions! Give it another try and we can try to guide you. This problem is so short that we can't really help anymore without flat out writing it for you.

Is this really that hard?

a lot of the code is hard for us noob at c++
dont assume us to know every "basic" you'd learned

CORRECT CODE.....

#include <iostream>
using namespace std;

int main()
{
   int sum = 0;    // Declare an int variable sum to accumulate the numbers
   int lowerbound=0;                // Set the initial sum to 0
   int upperbound=0; // Sum from 1 to this upperbound
  // Prompt user for an upperbound
   cout << "Enter a integer: ";
   cin >> lowerbound;
   cout << "Enter another integer: ";
   cin >> upperbound;

   while(lowerbound <= 100  && upperbound <= 100)
   {
	   sum += lowerbound++;
	   if(lowerbound == upperbound)
		   break;
   }
    // Print the result
   cout << "The total of the numbers FIRST and SECOND is " << sum << endl;
   return 0;
}

Edited 5 Years Ago by Narue: Added code tags

CORRECT CODE.....

1) It's not correct code
2) Do not do homework for others. It's not your grade, and it is their future.

so simple. Increment lower value Add to the sum ,till the Upper value. Then break the loop.
OR you can decrement higher value till lower value....

This article has been dead for over six months. Start a new discussion instead.