【发布时间】:2020-04-18 04:18:06
【问题描述】:
所以我对 C++ 中的 2d 数组相当陌生,我知道我做错了什么,但我不确定是什么。
#include <iostream>
using namespace std;
int main(){
string favBands[10][2];
cout << "Welcome to the favorite band printer outer!" << endl;
int count = 1;
string band;
string song;
for(int i = 0; i < 10; i++){
for(int j = 1; j < 2; j++){
cout << "Enter your number " << count << " band:\n" << endl;
count += 1;
cin >> band;
favBands[i][j] = band;
cout << "Enter " << favBands[i][j] << "'s best song:\n" << endl;
cin >> song;
favBands[i][j] = song;
}
}
}
我想请用户输入他们最喜欢的 10 个乐队,然后从该乐队中选出他们最喜欢的歌曲。比如:
Enter your number 1 favorite band:
Black Eyed Peas (user input)
Enter your favorite Black Eyed Peas song:
Boom Boom Pow (user input)
我能够做到所有这些,但是当我尝试将数组打印给用户时问题就来了。我认为我的问题可能在于我如何将用户数据输入到我的数组中,但我不确定如何修复它。谢谢!
【问题讨论】:
-
favBands[i][j] = band;和favBands[i][j] = song;都写入同一个位置。如果您使用调试器或在一张纸上逐步完成程序,其余的应该能够弄清楚。
标签: c++ arrays multidimensional-array