if/else question

Hello, I'm experimenting with if statements and have encountered something I don't understand.

I don't understand why the program doesn't execute else when I enter a character or string; instead it executes the else if.

for example:
when I input 'a'
the program spits back you did not enter -1 or -2.

Is this because the program doesn't read the character or string because I've defined an integer?

Also, why does it choose to execute else if rather than if or else when the condition is stated?

I would expect it to go straight to else since a character is neither > or < 0.

Thanks!

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
#include <iostream>
using namespace std;

int main()
{
	int num;
	cout << "Enter a number";
	cin >> num;

	if (num > 0)
	{
		for (num; num == 1 || num == 2;)
		{
			cout << "you pressed 1 or 2\n";
			return 1;
		}

		cout << "you did not press 1 or 2\n";
	}

	else if (num < 0)
	{
		for (num; num == -1 || num == -2;)
		
			{
				cout << "you pressed -1 or -2";
				return 1;
			}

			cout << "you did not press -1 or -2";
		

	}

	else       
	{
		cout << "you entered 0 or a character\n";
	}
		
	return 0;
}






** edit
Also, I tried to assign a string to num instead of inputting a number.

1
2
3

int num = 'adf';


This now executes the if statement and responds
"you did not press 1 or 2"
Last edited on
A character is always > 0: http://www.asciitable.com/

When I ran your program from the button, and entered a, the output was
you entered 0 or a character


I have no idea why you have for loops when it does not loop, should just be if statements.
hmmm I still get
you did not enter -1 or -2


I took out the for loops and I still get the same thing.

http://s12.postimg.org/t2rcxbtb1/did_not.png
Last edited on
when I input 'a'
the program spits back you did not enter -1 or -2.
When the input is not a number cin will not change num.

So, if num is uninitialized you cannot predict the value.
If num is initialized that value will be used.
Did you try using the "edit and run" button on your original post? I'm lazy and did not try creating a project. Set a breakpoint at line 8, then step forward (over). You should be able to see what value num is holding than.

A character and a string are two different things. A string is an array of characters, even it is only one character. C++ will use the the ASCII value of a char, if it is expecting an int. It will also do the opposite if expecting a char.
Last edited on
I'm not sure what this means
Set a breakpoint at line 8, then step forward (over).



but I found that when I type in a letter or words

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
	int num;
	cout << "Enter a number";
	cin >> num;

	cout << num;

	return 0;
}


and print out num I get

-858993460


which i find really interesting and explains why I keep getting

you did not enter -1 or -2


I guess this points to what coder777 said about not being able to predict the value?



So, is there a reason characters and strings are holding this value in my program?

Thanks for everyone's help! Sorry if these are weird questions, I'm just starting out.



Last edited on
He is exactly right, I probably just got lucky.

It is not quite clear what the the operator>> does in case of an error. Nonetheless you should initialize the variable.

You may use one of the status function to determine whether the input failed or not:

http://www.cplusplus.com/reference/istream/istream/

For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
	int num;
	cout << "Enter a number";
	if(cin >> num)
	  cout << num;
	else
	  cout << "Failed";
	return 0;
}
Topic archived. No new replies allowed.