我想获取用户输入并将其输出到我的 shell 环境中
i want to take a user inputs and output it in my shell environment
我想获取用户输入并将其输出到屏幕上。我假设让用户输入他们需要哪种类型,例如类型 O,但我的输出没有捕获我的 O,只是我的类型 ,那么有没有办法让它只捕获类型内的整行?
我的代码的示例输出。
输入太阳 类型:键入 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;
只读取一个单词,这就是为什么它停在 Type 和 O 之间的空格。
尝试使用gets() / getline()
函数。 默认情况下,cin
和cout
忽略空格。我不知道如何让他们接受空格。但是使用上述功能,您将获得所需的结果。
参考: gets(), getline()
相关文章:
- 为什么我的代码在输出中增加了93天
- 在我的代码中,获得最大的Pair Wise产品C++和输出并不总是正确的
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 我的输出中有一个额外的 0,为什么会这样
- 为什么我的 cout 在输出中没有显示字符串?
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 为什么我的输出与输入不同?(C++)
- 当使用我在一个函数、另一个函数中修改的数组时,在我的输出中得到一个奇怪的负数
- 为什么我的代码块上没有显示输出?
- "Program ended with exit code: 0"出现在我的输出的开头?
- gdb 中的列表命令不输出我编写的代码
- 我的代码中是否有任何类型的错误,因为它没有给出正确的输出
- 我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
- 我的求解(字符串 a、字符串 b)的输出与随机哈希中的预期输出不匹配
- fftw:为什么我的 2D DFT 输出与每行的 1D DFT 输出不同?
- 即使我的代码没有错误,我也没有得到正确的输出
- 为什么这一行不输出我的文本文件?
- 在任何情况下都会出现意外输出.我的解决方案出了什么问题