Invalid Input not applying to all statements

Hello Im a few days into learning C++ and was doing an assignment for a class. but when I type (a letter) Ex. "C" instead of an invalid integer like the 1st statement it just produces
-858993460 is even.
-858993460 is Divisible by 4.
-858993460 is Divisible by 5.
-858993460 is not all Divisible by 3,4 or 5..

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Write a program that asks the user to enter any integer.  
The program outputs whether the number is negative or positive (consider the number 0 as positive), 
whether it is odd or even, 
and whether it is divisible by any of 3, 4, and 5.  
For example, if the input number is -90, the output should say something like:
The number is negative, even, and divisible by 3, 5.
*/

#include <iostream>
using namespace std;

int main()
{
	int x;
	cout << "Please enter an integer; " << endl;
	if (!(cin >> x))
	{
		cout << "Invalid integer" << endl;
	}
	else if (x >= 0)
	{
		cout << x << " is positive." << endl;
	}
	else if (x<0)
	{
		cout << x << " is negative." << endl;
	}
	if (x % 2 == 0)
		cout << x << " is even." << endl;
	else
		cout << x << " is odd." << endl;
	if ((x % 3 == 0))
	{
		cout << x << " is Divisible by 3." << endl;
	}
	if ((x % 4 == 0))
	{
		cout << x << " is Divisible by 4." << endl;
	}
	if ((x % 5 == 0))
	{
		cout << x << " is Divisible by 5." << endl;
	}
	if ((x % 3 == 0) && (x % 4 == 0) && (x % 5 == 0))
	{
		cout << x << " is Divisible by all 3,4 and 5." << endl;
	}
	else
	{
		cout << x << " is not all Divisible by 3,4 or 5." << endl;
	}

	system("pause");
	return 0;
}


its not really part of the homework since I did everything it was asking for already. but I have a feeling that this I would have to do this in the future so any suggestions?
Last edited on
When you enter an invalid character x is not modified. In your case it remains uninitialized. All calculation should be in one else branch not else if.
Topic archived. No new replies allowed.