What's wrong with my loop?

I need a user to enter two numbers. Then the output needs to show the square and cube of those number. If the numbers are spread apart, 1 and 3 for example, then the numbers between them need to show as well.

So 1 and 3 would show

Number Square Cube
1 1 1
2 4 8
3 9 27

My problem is I can't get the thing to show all the numbers in between correctly. please check my code and tell me what I should look at. I'm a complete beginner so please go easy on me. Thanks.


Here's what I got...


int main ()
{
//declare variables
int first = 0;
int second = 0;
double totalNum = 0.0;
double squareFirst = 0.0;
double cubeFirst = 0.0;
double squareSecond = 0.0;
double cubeSecond = 0.0;

//enter input items
cout << "Enter your first number:" << endl;
cin >> first;
cout << "Enter another number greater than the first:" << endl;
cin >> second;

cout << "Number" << " Square" << " Cube" << endl;
squareFirst = pow(first, 2.0);
cubeFirst = pow(first, 3.0);
cout << " " <<first << " " << squareFirst << " " << cubeFirst << endl;

do
{
totalNum = first + 1;

squareSecond = pow(totalNum, 2.0);
cubeSecond = pow(totalNum, 3.0);
cout << " " <<totalNum << " " << squareSecond << " " << cubeSecond << endl;

}while (second != totalNum);





closed account (z05DSL3A)
Every time you loop you are setting totalNum to the same value.

put 'totalNum = first;' before the 'do' statement and 'totalNum++;' where 'totalNum = first + 1; 'currently is.

1
2
3
4
5
6
...
totalNum = first ;
do
{
    totalNum++;
...


It would also be better to use >= in your while expression to stop the program running on if the user does put in a lower number for the second value.
Last edited on
Thanks grey wolf. That makes sense.
Topic archived. No new replies allowed.