I'm in an intro to c++ class, and I'm trying to complete an assignment that completes the following steps:
a. Prompt the user to input two integers: firstNum and secondNum
b. Validates that the firstNum is less than the secondNum. If the firstNum is greater than the secondNum shows an error message to the user.
c. Output all numbers between firstNum and secondNum.
d. Output all odd numbers between firstNum and secondNum.
e. Output the sum of all odd numbers between firstNum and secondNum.
f. Output the square of all odd numbers between firstNum and secondNum.
However, after I complete task c, I don't get any output for task d. I was wondering if it was because it goes off the values after task c has been completed. Here is my code:
The for loop increments firstNum until it is greater than secondNum. When you come to use firstNum in the while loop it is already > secondNum so nothing happens.
1 2 3 4
for (int f = firstNum; f <= secondNum;++f)
{
cout << f << endl;
}
Now firstNum isn't changed.
Your while loop would also benefit from using a for loop in the same way