#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;
}

Recommended Answers

All 18 Replies

Is there a problem? If so, please explain it. If not, this should be moved to Code Snippets.

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.

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?

add xLow to sum

That will sum all numbers between 50 and 90

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

If the attitude of helping disgusts you, go somewhere else.
People are wasting their time on you.

>> 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.

commented: 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. -3

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

Take a read through this:
http://www.cplusplus.com/doc/tutorial/control/

Then give it another shot.

Please keep in mind my suggestion of:

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

Then post your new code if it still doesn't work.

David

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;
}

Please use code tags. I still don't understand why you are incrementing the lowerbound?

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....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.