c++ - 我想接受用户输入并在我的 shell 环境中输出它

标签 c++ input

我想获取用户输入并将其输出到我的屏幕上。 我想让用户输入他们需要什么样的类型,例如 Type O ,但是我的输出没有捕获我的 O ,只是我的 Type ,那么有没有办法让它只捕获 Type 内的整行?

我的代码的示例输出。

输入太阳类型:k

输入行星:10

输入的太阳类型:类型

行星数量:10

这只是我整个冗长代码的一部分。

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class LocationData
{   
    private:
    string sunType;
    int noOfEarthLikePlanets;
    int noOfEarthLikeMoons;
    float aveParticulateDensity;
    float avePlasmaDensity;
    public:

};
int main()
{
    int i;
    string s;
    LocationData test;
    cout<<"Enter Sun Type: ";
    cin>>s;
    test.setSunType(s);
    cin.clear();
    cin.ignore(10000,'\n');
    cout<<"Enter planets: ";
    cin>>i;
    test.setNoOfEarthLikePlanets(i);
    cout<<"Sun type that was entered: "<<test.getSunType();
    out<<"\nNo of Planets: "<<test.getNoOfEarthLikePlanets()<<endl;
}

最佳答案

getline(cin, s);

读取整行并将该行放入 s 变量中。正如你所发现的

cin >> s;

只读一个单词,这就是为什么它会停在 TypeO 之间的空格处。

关于c++ - 我想接受用户输入并在我的 shell 环境中输出它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12914080/

相关文章:

c++ - error_code 的格式说明符是什么?

c++ - 在带有 block 的 switch/case 语句中放置中断的位置

java - while 循环后的代码不接受输入 [Java]

c - 无法通过 fgets() 从输入中正确读取整数

html - 如何使这两个元素彼此相邻

c - 出现一个额外的输入

c++ - 如何从 Arduino 正确接收和发送原始红外数据

c++ - 为什么用构造函数调用符号定义指向指针变量的指针是非法的?

c++ - 使用继承时,带有 CRTP 的 typedef 不起作用

python - 在 Python 中验证用户输入的字符串