ctrl - z question

I want to express something in C++ but I'm unsure... basically if I enter a number less than 1 or enter ctrl-z, then my program will display a cout message... how do I go about this?

if (num1 < 0)
if (num1 = '^z')

cout << "hey, the sum is"; ......... and so on.

is what I have in my head.
Last edited on
if (num1 = '^z')

For starters...
if (num1 == ") //...





I cant remember how to check for two keys at once but to check if ctrl was pressed you would use the ASCII key for ctrl. For instance if ctrl's ascii code was 36 (which it isn't) you would check it like this

1
2
3
if(num1 == 36) {
    //do something
}


note that you use '==' and not '=' when evaluating a boolean expression.

a single '=' will be assignment.
I have now:

if (num1 == -1)
if (num1 == '^z')

cout << "The sum is" etc....


it closes my program instead... but if i enter -1, it stays open but displays nothing.

If i remove the if (num1 == '^z'), then if I enter -1, it will display the cout correctly
Last edited on
if that's verbatim what you wrote then what you are saying is


if num1 is equal to -1 then
if num1 is equal to '^z' then
do something

or written simpler

if num1 is equal to -1 and '^z' then
do something

this will never be true because num1 cannot be both -1 and '^z' at the same time
what you need to do is use the conditional 'or' operator. In case you don't know it, you type ||.
so your code becomes if(num1 == -1 || num1 == '^z') cout << "the sum is " etc




I'm still having issues with it, even using your method... I have now...


cin >> num2;

{
if (num2 < -1)

return 0;

if (num2 == -1 || num2 == '^z')

cout << "The sum is: "<< (num1 * 10)<< endl;

Aim of the code is to display the previous accumulated value in num1.

Basically, it will just close if i do crtl z, but will stay open and cout if i enter -1
Last edited on
Topic archived. No new replies allowed.