Why don't I get an output?

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>
using namespace std;

int main()
{
	int firstNum;
	int secondNum;

	cout << "Enter a number:";
	cin >> firstNum;

	cout << "Enter a second number:";
	cin >> secondNum;

	if (firstNum < secondNum)
		cout << " ";
	else
		cout << "Error";

	for (firstNum;firstNum <= secondNum;firstNum++)
	{
		cout << firstNum << endl;
	}

	while (firstNum <= secondNum)
	{
		if ((firstNum % 2) != 0)
			cout << firstNum << endl;
		firstNum++;
	}


	return 0;
}
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
Topic archived. No new replies allowed.