Checking if a char is pressed
NickName:Beach Williams Ask DateTime:2016-11-11T03:50:19

Checking if a char is pressed

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

Answers
Mindchallenges 2016-11-10T20:25:36

You don't need to use goto and kbhit() inside cin.\na simple way is:\n\n#include <iostream>\n#include <string>\n\nusing namespace std;\n\nint main()\n{\n int a,b;\n string A, B\n char k;\n\n while(1)\n {\n cin >> A;\n if(A == \"q\") break;\n cin >> B;\n if(B == \"q\") break;\n\n a = atoi(A.c_str());\n b = atoi(B.c_str());\n\n cin >> k;\n if(k == 'q') break;\n\n if (k == '+')\n cout << (a + b);\n if (k == '*')\n cout << (a*b);\n\n }\n}\n",


More about “Checking if a char is pressed” related questions

Reading a sentence until ENTER key pressed using 2-D char array

I need to read a sentence word by word until "ENTER" key is pressed. I used a do..while loop to read words until ENTER key is pressed. Please suggest me some conditions for checking ENTER key press...

Show Detail

Checking if a char is pressed

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" #inclu...

Show Detail

Detect when the key pressed is the first char

I've added a KeyPress event handler to my DataGridView. If user presses &quot;=&quot; in a Cell, this event fires. But the = key must be first char. How can I detect whether the pressed key is the ...

Show Detail

Checking time since last key was pressed

I'm making a script that requires the ability to redirect someone if a key has not been pressed within 15 seconds, the only problem is I don't know how to check how much time has lapsed since the l...

Show Detail

How to get key pressed as char in DrawingPanel

I'm putting together a simple game as part of a project in Java. I'm using a DrawingPanel window, and need to be able to get the current keyboard key pressed as a char. Specifically, I have the

Show Detail

checking if callout view is pressed (mapBox iOS SDK)

i'm wondering what is the best way to check - is callout view pressed using mapBox iOS SDK? The only method that i find is this one - (void)tapOnCalloutAccessoryControl:(UIControl *)control

Show Detail

Checking if the specific key is pressed on a looping function

So my script has a looping function that is designed to print out a string when a specific key (the a key) is pressed down, and if its not then it prints out a different string, so that when the ke...

Show Detail

how to change char arry from string to integer (encoding problem)

Hello everyone I am new to C and learning new stuff daily. I am working on a project which involves qt creator and controller. so basically I am getting pin(password) from qt application and sendin...

Show Detail

C# Checking if more than one key is pressed (Global Keyboard Hook )

I'm making a C# form app that runs in the background and checking if you pressed CTRL + A + S. So I was checking forums on the internet and I already setup that the app runs in the background and n...

Show Detail

char written to a char* array mutates somehow

This should be a simple function (it counts the number of unique chars in a string), but I'm getting a weird issue. Note that my code use expects only ASCII letters a-z, and A-Z. int unique_chars(...

Show Detail