相关疑难解决方法(0)

使用字符串类输入空格时发出cin问题

我有以下代码:

main.cpp中

#include <iostream>
#include <string>

using namespace std;

string name;
string age;

int main() {
    cout <<"Name: ";
    cin >> name;
    cout << endl;
    cout <<"Age: ";
    cin >> age;
    cout << endl;
    cout << "Your name is " << name << ", and you are " << age << " years old."  << endl;
    cout << "Press enter to close this application" << endl;
    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我注意到如果我在输入中输入一个空格来表示它不会给我输入名称的机会,它会在空格后查看该条目作为年龄.如果这是一个新手的错误,我很抱歉,这可能是.我之前编写过Java并决定转换到C++,因为它更适合我的需求.我也可能将我的代码格式化为您的标准,如果您愿意,请更正.

控制台截图

我也注意到了另一个错误,我在Java中从来没有遇到任何问题.我无法弄清楚如何防止它在完成处理后立即关闭.我听说你可以使用"(暂停")系统.";但我也被告知不要使用它,我什么就用我听说使用的getchar()真的很困惑;不过.它似乎没有做任何事情.

任何帮助都会非常感激,因为我在C++方面是一个完全的初学者.

c++ string input

10
推荐指数
2
解决办法
5万
查看次数

标签 统计

c++ ×1

input ×1

string ×1