【发布时间】:2021-01-03 17:07:08
【问题描述】:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int person;
cout << "How many person to data: ";
cin >> person;
int x = 0;
int i = 1;
string name[person];
string adress[person];
while(x < person){
cout << i << " person data" << endl;
cout << "Name: ";
getline(cin, name[x]);
cout << "Adress: ";
getline(cin, adress[x]);
cout << endl;
x++, i++;
}
for(x=0; x<person; x++){
cout << name[x] << setw(15) << adress[x] << endl;
}
}
这是我将名称和地址存储到数组 name[] 和 adress[] 中的代码 然后我用for循环打印他们的名字和地址
这是输出图像 Result
为什么我的 1 个人数据损坏了? 姓名和地址在同一行,而我的第二个人数据没问题?
如果我使用 cin 很好 >> 但我使用 getline 所以我可以使用全名和带空格的地址
【问题讨论】:
标签: c++ arrays string vector getline