Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for me to get rid of the end of line character after an input? [duplicate]

Tags:

c++

char choice;
float inputa, inputb;
cin >> inputa >> choice >> inputb;

while(inputa != 0 && choice != '0' && inputb != 0)
{
    cout << " = " << calculation(inputa,choice,inputb) << endl << endl;
    cin >> inputa >> choice >> inputb;
}

system("PAUSE");
return 0;

This is what the screen looks like:

5 + 4
 = 9

The user gives their input and presses enter, so that's what takes them to the next line I guess, but is there a way for it to show:

5 + 4 = 9

After the result is shown, it would use the endl to go to the next line and the user can input their problem again.

I'm a complete beginner so sorry if the question doesn't make a lot of sense.

like image 848
Shahzad Avatar asked Nov 11 '22 15:11

Shahzad


1 Answers

You can't use do it since cin prints every character after you hit it. You will have to define your own input function.

For exemple you can use getch in the conio.h (This is not an efficient solution since getch accept only one character. You will have to fin another way. The importent here is to get the idea).

#include <iostream>
#include <conio.h>

using namespace std;

void input(float *a, char *c, float *b)
{
    char in;
    do {
        in = getch();
        cout << in;
    } while (in == ' ');
    *a = in - '0';
    do {
        in = getch();
        cout << in;
    } while (in == ' ');
    *c = in;
    do {
        in = getch();
        cout << in;
    } while (in == ' ');
    *b = in - '0';
}

int main()
{
    char choice;
    float inputa, inputb;
    cout << "Expression: ";
    input(&inputa, &choice, &inputb);
    cout << " = " << inputa+inputb << endl;
    return 0;
}
like image 133
rullof Avatar answered Nov 14 '22 23:11

rullof