我可以在输入后摆脱行字符的结束吗? [英] Is it possible for me to get rid of the end of line character after an input?

查看:120
本文介绍了我可以在输入后摆脱行字符的结束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;

这是屏幕显示的样子:

5 + 4
 = 9

用户给出他们的输入并按下enter,这就是他们到下一行我想,但是有一种方法,它显示:

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

结果显示后,

我是一个完整的初学者,所以很抱歉,您可以使用c $ c> endl

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

推荐答案

你不能使用它,因为 cin 在打完每个字符后打印。

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

例如,您可以在<$ c $中使用 getch c> conio.h (这不是一个有效的解决方案,因为getch只接受一个字符,你必须用另一种方式,这里的importent是获取想法

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;
}

这篇关于我可以在输入后摆脱行字符的结束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆