提示用户每个循环迭代

问题描述 投票:0回答:1

该代码的目的是使用for循环向用户询问5次名称,每次将所述名称存储到数组中的位置。这是我的代码:

#include<iostream>
#include<string>


 using namespace std;



int main () 
{



char names[5];
int i;  

for (i = 0; i < 5; ++i)
{


cout << "Enter a name: ";
getline(cin, names[i]);
cout<<endl;
} 
cout << names[5];


return 0;


}

这是错误:

main.cpp: In function ‘int main()’:
main.cpp:22:22: error: no matching function for call to ‘getline(std::istream&, char&)’
 getline(cin, names[i]);

即使我不应该这样做,我也将其更改为普通的cin运算符,它进行了编译,但是循环仅迭代了5次而没有提示用户任何操作,所以结果是:

Enter a name: 
Enter a name: 
Enter a name: 
Enter a name: 
Enter a name: 

有帮助吗?

c++ arrays for-loop
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.