The program should read 2 ints and calculate the sum or product depending on the symbol introduced from the keyboard. If you press q at any given momement, it must exit.
#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
char k, l ='h',c;
int a,b,s, p;
aici: while (l != 'q')
{
cin >> a;
if (_kbhit() == 0) //getting out of the loop
{
c = a;
if (c == 'q')
{
l = 'q';
goto aici;
}
}
cin >> b;
if (_kbhit() == 0)
{
c = b;
if (c == 'q')
{
l = 'q';
goto aici;
}
}
k = _getch();
if (_kbhit() == 0)
{
c = k;
if (c == 'q')
{
l = 'q';
goto aici;
}
}
if (k == '+')
{
s =(int)(a + b);
cout << s;
}
if (k == '*')
{
p = (int)(a*b);
cout << p;
}
}
return 0;
}
It expects both a and b to be ints so typing 'q' makes a total mess.
Is it possible to make the program work without having a and b declared as chars?
Copyright Notice:Content Author:「Beach Williams」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40535866/checking-if-a-char-is-pressed