输入字符串列表 [英] input list of strings

查看:105
本文介绍了输入字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vs 2010中编写了这段代码,以获取学生的ID和全名,然后打印出来,但此方法不再起作用
我需要你的忠告
请帮助我

i writed this code in vs 2010 for get students id and full name and then print them,but it doesn''t work again
i need your advise
please help me

#include <iostream>
#include<string>
using namespace std;
int main(){
string str[100];
int ID[100],i,count;
cout<<"enter number of students:";
cin>>count;
for (i=0;i<count;i++){
	cout<<"\nenter student id:";
	cin>>ID[i];
	cout<<"\nenter student full name:";
	getline(cin,str[i]);
}
cout<<endl;
for(i=0;i<count;i++){
cout<<i+1<<"\t"<<ID[i]<<"\t"<<str[i].data();
}

return 0;
}

推荐答案

正确声明变量:
Declare your variables properly:
string str[100];
int sum;
cin >> sum;
if (sum >= 100) // Watch out for buffer overflow
	sum = 100;
int count=0;
while (count < sum)
{
	getline(cin,str[count]);
	count++;
}
for (int i=0; i < sum; i++)
	cout << str[i] << endl;


这篇关于输入字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆