0

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?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

2 Answers2

0

You don't need to use goto and kbhit() inside cin. a simple way is:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a,b;
    string A, B
    char k;

    while(1)
    {
        cin >> A;
        if(A == "q") break;
        cin >> B;
        if(B == "q") break;

        a = atoi(A.c_str());
        b = atoi(B.c_str());

        cin >> k;
        if(k == 'q') break;

        if (k == '+')
            cout << (a + b);
        if (k == '*')
            cout << (a*b);

    }
}
  • Really good cut at the problem, but `cin >>` doesn't return until enter has been pressed. This means it doesn't exit immediately on hitting q. You have to go outside the standard library to make this work. – user4581301 Nov 10 '16 at 20:32
0

You can't get where you want to be on this path. The standard input stream reads will block, preventing you from looking for 'q' and exiting.

Instead look at all the input for 'q' as it comes in and convert it to the values you require later after you have a complete message. Something like:

while (int input = _getch()) != 'q') // if read character not q
{
    accumulate input into tokens
    if enough complete tokens
       convert numeric tokens into number with std::stoi or similar
       perform operation
       print output 
}

It is possible that you can do something along the lines of

std::stringstream accumulator;
while (int input = _getch()) != 'q') // if read character not q
{
    accumulator << std::static_cast<char>(input);
    if (got enough complete tokens)// this be the hard part
    {
        int a;
        int b;
        char op;
        if (accumulator >> a >> b >> op)
        { // read correct data
            perform operation op on a and b
            print output 
        }
        accumulator.clear(); // clear any error conditions
        accumulator.str(std::string()); // empty the accumulator
    }
}

std::stringstream documentation.

user4581301
  • 33,082
  • 7
  • 33
  • 54