我的任务要求我打开一个文本文件和输出随机阵列和环回的问题。我想知道为什么我的代码没有输出?我感谢所有的帮助。非常感谢你。无输出和随机化字符串数组
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
ifstream fin;
fin.open("songs.txt");
if (!fin.good()) throw "I/O error";
string ans;
const int MAX_SONGS = 200;
int nSongs=0;
string song[MAX_SONGS];
while (fin.good())
{
// read txt file
string aSong;
getline(cin, aSong);
// add song if still have space
if (nSongs < MAX_SONGS)
song[nSongs++] = aSong;
}
fin.close();
cout<<"hi!";
for (int i=0; i<nSongs; i++)
{
song[i] = (rand() % nSongs);
cout << " play a song [Y/N]? ";
getline(cin, ans);
if (ans=="Y"||ans=="y")
cout << song[i]<<endl;
break;
if (ans=="n"||ans=="N")
break;
}
}
你能看到“嗨!”吗?在输出? – sonique
它终于做到了,当我改变得到线而不是cin! – xamfifa