gpt4 book ai didi

c++ - 如何在 C++ 中从控制台读取字符?

转载 作者:搜寻专家 更新时间:2023-10-31 00:23:03 24 4
gpt4 key购买 nike

我正在努力从 C++ 控制台读取字符。这是我尝试做的:

char x;
char y;
char z;

cout<<"Please enter your string: ";
string s;
getline(cin,s);
istringstream is(s);

is>> x >> y >> z;

问题是如果用户输入像这样的“1 20 100”:

x will get 1
y will get 2
z will get 0

我要得到的是x = 1; y = 20; z = 100;

有人有建议吗?

最佳答案

你不想读取字符而是整数。

int x;
int y;
int z;

cout<<"Please enter your string: ";
string s;
getline(cin,s);
istringstream is(s);

is>> x >> y >> z;

关于c++ - 如何在 C++ 中从控制台读取字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2557638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com