gpt4 book ai didi

c++ - 当我在单词之间留出空格时出现运行时错误

转载 作者:行者123 更新时间:2023-11-30 02:52:30 25 4
gpt4 key购买 nike

我在运行时将单词存储在数组中,但是当我在单词之间留出空格时,程序不要求第二个输入,它直接给我一个输出,而不需要第二个输入,这是我的编码。

#include<iostream>
#include<conio.h>

using namespace std;
int main(){
char a[50];
char b[50];
cout<<"please tell us what is your language\t";
cin>>a;
cout<<"please tell us what is your language\t";
cin>>b;
cout<<a<<b;
getch();
}

here is my output

最佳答案

#include<iostream>
//#include<conio.h> // better don't use this, it's not portable
#include <string>

//using namespace std; // moving this inside the function
int main(){
using namespace std; // a bit more appropriate here

string a;
string b;

cout<<"please tell us what is your language\t";
getline(cin, a); // `a` will automatically grow to fit the input
cout<<"please tell us what is your language\t";
getline(cin, b);
cout<<a<<b;

//getch(); // not portable, from conio.h
// alternative to getch:
cin.ignore();
}

std::getline 的引用(底部有一个例子)和std::string .

关于c++ - 当我在单词之间留出空格时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18820595/

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