Jump to content

.

AnotherTechGuy

1 is int

'1' is char

your code now is checking if the ascii value of 'exit' is 1

add the apices and it will work

Link to comment
Share on other sites

Link to post
Share on other sites

What Ciccioo said, char type are basically numbers which have characters assigned to them, so 1 is essentially char(1) if checked against a char type, which in this case means SOH, whatever it is :) you can test how it works with the following:

For(int i=0;i<256;i++)std::cout << "Character " << i << ": " << char(i) << std::endl; 

This will get the full list of ascii characters. OR just go here http://www.asciitable.com/

To compare single chars against hard coded values use ' ', e.g. '1', and when comparing arrays of chars or strings, use " ", e.g. "one".

Lastly, what you could do is at the start of the program you could define an exit char value, e.g. 

char exitChar = '1';

And then use 

if(exit == exitChar)return 0;

This way, if you have multiple exit points in your program, you can use the same IF statement everywhere and if you want to change the exit character, you'll only need to change it at the top.

GL & HF

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×