当前位置:C++技术网 > 资讯 > 为什么输入名字的时候,只能输入单个字符

为什么输入名字的时候,只能输入单个字符

更新时间:2016-05-10 12:12:44浏览次数:1+次

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int a,b,c,e,f=1,g=1;
    cout<<"how many g "<<endl;
    cin>>a;
    char name[a][10];
int how[a][5];
    for(c=0;c<=a-1;c++)
    {cout<<"type name"<<g<<endl; //???can't type more than 1 word
       cin>>name[c][10];
cout<<"type scope"<<g<<endl;
        cin>>how[c][5];
g++;
        }

    for(e=0;e<=a-1;e++){
        cout<<f<<"\t"<<name[e][10]<<"\t"<<how[e][5]<<endl;
 f++;
    }
return 0;
}
C++技术网的解答:

我将你的代码修改了一下,使用了静态数组
int a,b,c,e,f=1,g=1;
cout<<"how many g "<<endl;
cin>>a;
char name[1][10];
int how[1];
for(c=0;c<=a-1;c++)
{cout<<"type name"<<g<<endl; 
cin>>name[c];
cout<<"type scope"<<g<<endl;
cin>>how[c];
g++;
}
for(e=0;e<=a-1;e++){
cout<<f<<"\t"<<name[e]<<"\t"<<how[e]<<endl;
f++;
}
你不能输入超过一个词是因为你用一个字符去接受输入,要用字符数组去接受输入,这样才能输入多个词
输出结果:
how many g
1
type name1
test
type scope1
123
1       test    123