First time poster (I'm sorry)

I'm going to be honest this is a homework question but its one specific thing that I am confused with. All the parts of the problem have to be done with while loops. I have 2 different loops both of them work separately but when I run them together the odd numbers are displaying but it just says even total = 0. Should they not both be separate while loops?? I tried doing it as an if else but that was even worse than this and there will be other parts of the problem that need to manipulate the same original numbers.

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
35
36
37
  #include <iostream>

using namespace std;

int main ()
{
	int firstNum, secondNum;
	int mod;
	int even = 0;
	int odd;
	int counter;

	cout << "Enter two numbers the first must be lower than the second" << endl;
	cin >> firstNum >> secondNum;
		
	mod = (firstNum % 2);

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

	while (firstNum <= secondNum)
	{ 
		if (mod == 0);
		even = firstNum + even;
		firstNum = firstNum + 2;
	}

	cout << "even total = " << even << endl;

	system ("pause");
	return 0;
}
Line 28: Extra semicolon.

Shouldn't the mod ==/!= 0 conditions for your if statements be conditions for your while loops? The way it is now, the first while loop will increase firstNum until it's greater than secondNum, and then the second loop will never run, leaving even equal to zero.

-Albatross
Last edited on
Thanks for responding, it did work but it still can't read the next loop. I am now trying while (cin.eof ()) but that seems to be a bust too.

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
35
36
#include <iostream>
#include <cmath>
#include <iomanip>
#include <istream>

using namespace std;

int main ()
{
	int firstNum, secondNum;
	int mod;
	int even = 0;

	cout << "Enter two numbers the first must be lower than the second" << endl;
	cin >> firstNum >> secondNum;
	
	mod = (firstNum % 2);

	while (firstNum <= secondNum && mod != 0)
		{
			cout << firstNum << " " << endl;
			firstNum = firstNum + 2;
	}
	
	while(cin.eof())
		{
			while (firstNum <= secondNum && mod == 0)
			{
				even = firstNum + even;
				firstNum = firstNum + 2;
	}
	cout << "Even total = " << endl;
	}
system ("pause");
return 0;
}
o.
Hello fellow first time poster!

Honestly, it's hard to help because without a description I can't actually pin down what it is this program is trying to do. I rewrote your first example as Albatross suggested, and it seems to work: if firstNum is even, the second loop runs. If it's odd, the first one runs - of course, even will still be 0 if firstNum is odd.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main () {
        int firstNum, secondNum, mod, even = 0, odd, counter;
        cout << "Enter two numbers the first must be lower than the second" << endl;
        cin >> firstNum >> secondNum;
        mod = firstNum % 2;
        while (firstNum <= secondNum && mod != 0) {
                cout << firstNum << " " << endl;
                firstNum += 2;
        }
        while (firstNum <= secondNum && mod == 0) { 
                even += firstNum;
                firstNum += 2;
        }       
        cout << "Even total: " << even << endl;
        return 0;
}


Sorry if that wasn't much help, as I said, I don't know what this program is trying to achieve.
Last edited on
Actually that was very helpful. I need them both to run using the same set of numbers.
So no matter if the firstNum is odd or even I need to know all the odd numbers in the set and what the total of all the even numbers are.

I tried it this way but I think I'm just getting further from the solution. Back to the drawing board I go... haha

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
35
36
37
38
39
  #include <iostream>

using namespace std;

int main ()
{
	int firstNum, secondNum;
	int mod;
	int even = 0;
	int odd;
	int counter;

	cout << "Enter two numbers the first must be lower than the second" << endl;
	cin >> firstNum >> secondNum;
		
	mod = (firstNum % 2);
	counter = firstNum;

	cout <<"These are the odd numbers: " << endl;
	
	while (mod !=0 && counter <= secondNum)
			{
				cout << counter << " " << endl;
				counter = counter + 2;
			}
	
	mod = (firstNum % 2);
	counter = firstNum;

       while (mod == 0 && counter <= secondNum)
		{
			even = counter + even;
			counter = counter + 2;
		}
	cout << "even total = " << even << endl;
	
	system ("pause");
	return 0;
}

You're on the right track. The important thing to realize is that if firstNum % 2 dosen't return 0, then it will definitely return 1. Which means that if the number is even (mod == 0) then we just need to add 1 to it to make it odd. Likewise, when it's odd (mod == 1) we just need to subtract 1 to make it even. So then the code becomes:

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
#include <iostream>

using namespace std;

int main() {
        int firstNum, secondNum, mod, even = 0, odd, tempNum;
        cout << "Enter two numbers, the first must be lower than the second." << endl;
        cin >> firstNum >> secondNum;
        mod = firstNum % 2;
        cout << "These are all of the odd numbers in the set: " << endl;
        tempNum = firstNum;
        if (mod == 0) { //this means to to get all the odd numbers, we'll need firstNum + 1 aswell.
                tempNum += 1;
        }
        while (tempNum <= secondNum) {
                cout << tempNum << " ";
                tempNum += 2;
        }
        cout << endl;
        cout << "Even total: ";
        if (mod != 0) { //like with the odd numbers, I'll need to subtract 1 here
                firstNum -= 1;
        }
        while (firstNum <= secondNum) {
                even += firstNum;
                firstNum += 2;
        }
        cout << even << endl;
}


I haven't tested this, mind you, so I could be wrong and/or there could be errors EVERYWHERE! But I think it should work ;)
Last edited on
Guess he could use two arrays or vectors to store all the even and odd numbers and then print them all out and the total. Though,I am probably over complicating it by bringing that up.
That's certainly one way to do it, but I was trying to stay as close to his code as possible.
Thanks that did the trick! I ended up doing something slightly different because it worked better with the rest of the problem that I didn't post in here.

1
2
3
4
if (mod != 0)						//makes sure loop control variable is odd
		counter = firstNum;				
	else								
		counter = firstNum + 1;


1
2
3
4
if (mod == 0)					//makes sure LCV is even
		counter = firstNum;
	else
		counter = firstNum + 1;	
Topic archived. No new replies allowed.