C++ 数组、For 循环和字符串数据类型

标签 c++ arrays string for-loop

<分区>

我正在尝试使用 for 循环将 4 个字符串放入一个数组中。 for 循环结束后,我尝试使用 for 循环显示 4 个字符串。我不知道我的错误在哪里。任何帮助和指导将不胜感激。

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
//declare variables
string array[4] ;

cout<<"input 4 strings"<<endl;

for (int i=1; i<4; i++)
{
    getline(cin,array[i]);
}
cout << "here are your 4 strings" << endl;

for (int j=0; j<4; j++)
{
    cout<<array[j]<<endl;
}

system("pause");
return 0;

} // end of main function

最佳答案

你的第一个循环应该从 i = 0 开始,而不是 i = 1

关于C++ 数组、For 循环和字符串数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26622261/

相关文章:

java - 将值添加到从文件 java 读取的数组

c++ - 将 std::map<int , std::string> 与 Google Protocol Buffer 结合使用

c++ - 如何检查 WriteFile 函数是否完成

c++ - 计算 Linux 上一组进程使用的内存

C - 字符数组在 memset 之后获取幻像值

php - 创建结果行的副本

c++ - std::vector<string> 奇怪的行为

android - 获取字符串资源的不​​同方式

c++ - 如何在 fedora 17 中使用 TCP_QUICKACK

java - 取多个拆分字符串的第一个大写字符