C++关于输入字符的问题

来源:百度知道 编辑:UC知道 时间:2024/06/25 09:38:21
char a[99]; //看这两行!!!!!!
cin>>a;
大家看看下面代码中的这两行
目的是要玩家输入名字
但是如果玩家输入的名字中带有空格就会导致错误(谁知道为什么会错误……)
我该如何避免这个问题

#include <iostream>
using namespace std;

main()
{
cout<<"Welcome to Stonee's Quest"<<"\n\n\n\n\n";
cout<<"Enter the name of your character:";
char a[99]; //看这两行!!!!!!
cin>>a;
cout<<"\n\nnext,you are going to decide your characteristics"<<endl;
cout<<"warning:If the total exceeds 15, then 5 points are assigned to each characteristic, good luck"<<endl;
int s,h,l;
cout<<"Enter strength (1-10):";
cin>>s;
cout<<"Enter health (1-10):";
cin>>h;
cout<<"Enter luck (1-10):";
cin>>l;
cout<<"&#

注意注意注意!

请注意你的措辞!那有这么跟老师说话的!真是没有教养!

这么简单的问题都解决不了吗??

这么用:

char a[99];
cin>>a;

改为:

char a[99];
cin.getline(a,98);//a是字符数组,98是表示最多读取多少个字符。

用Getline