1

このコードでは、最初の曲を要求し、それを保存して印刷します。しかし、最初のエントリの後、希望どおりに続行するかどうかを尋ねられますが、Yを入力すると、曲の質問とgetline関数がスキップされ、続行するかどうかが直接尋ねられます。私はここで何が間違っているのですか?

これは私の出力です

曲の名前を入力します。助けが必要
です続行しますか(y / n)?y
曲の名前を入力します。続行しますか(y / n)?y
曲の名前を入力します。続行しますか(y / n)?y
曲の名前を入力します。続行しますか(y / n)?n4
曲のタイトルが入力されました。
私は助けが必要です

私の望む出力と期待される出力は

曲の名前を入力します。助けが必要
です続行しますか(y / n)?y
曲の名前を入力します。印刷
続行しますか(y / n)?y
曲の名前を入力します。そして、入力
しますか(y / n)続行しますか?y
曲の名前を入力します。これらの曲
続行しますか(y / n)?n4
曲のタイトルが入力されました。 これらの曲の 印刷 と入力
についてサポートが必要です


/*This program will ask a user to enter a song list. By doing this each time that the     user enters
a song, he/she will then be asked if they want to enter another song, if yes then they     will enter another song.There will be a maximum of 15 songs.
This progarm will help a user keep track of their songs. But then the program will     output the amount of songs, and what they are.
input:A song. Then a yes or no, if yes, another song, if no the program will output.
output:The number of songs, and then the songs themselves, each song with it's own     individual line.
processing: There will be two functions, one of which will ask the user for the songs and     store them, and another for which will be computing
the output.
*/
    #include<iostream>
    #include<string>

using namespace std;

int input(string[15], int);
void printArray(string[15], int);

int main()
{
    const int arraysize=15;
    string songarray[arraysize];

     int x=input(songarray, arraysize);



      cout<<"There were "<<x<<" song titles entered.\n";
      printArray(songarray, x);
       return 0;
       }

/*This function will ask for the users input for a song and store the song in the     songarray. He or she
will then be asked whether or not they want to enter another song, if so, then they     will enter another song.
input:a song, y/Y or n/N for if they want to continue or not with another song
output:song will be sent to main function
processing: A while loop will be used for the user to enter is Y or N, and a for loop     (while loop nested) for the user to enter the 
songs
*/
int input(string titles[15], int rows)
{

      char answer='y';
      int k=0;
      for(int i=0;i<rows && (answer=='Y' || answer=='y');i++)
      {

           cout<<"Enter the name of a song. ";
           getline(cin, titles[i]);
           cout<<"Do you want to continue (y/n)? ";
           cin>>answer;
           k=i+1;
            }
           return k;
           }

/*The purpose of this function is to print the array from the main function.
input:accepts the array from the main function
output: prints the array
processing:nested loops will pring this array.
*/

void printArray(string playlist[15], int quantity)
{
    for(int j=0; j<quantity; j++)
    {
        cout<<playlist[j];
    }
}
4

3 に答える 3

2
char answer;
cin >> answer;

確かに問題です:別の解決策についてはcin.ignoreを見てください

于 2011-04-19T15:22:07.467 に答える
2

問題は、「cin >> answer」が単一の文字を読み取るが、「y」と入力してEnterキーを押すため、改行文字がまだcinに残っていることです。したがって、ループがgetlineに戻ると、その改行文字が読み取られ、空の文字列になります。

もし、するなら:

string dummy;
getline(cin, dummy);

「cin>>answer」の後、その末尾の改行文字を食べます。

于 2011-04-19T15:17:27.703 に答える
0
char answer;
cin >> answer;

プログラムの質問に答えるためにどのキーを押しましたか?どちらになりましたanswerか?他に何かを入力する前に次のgetlineを終了して、stdinに残ったのはどれですか?

于 2011-04-19T15:19:06.050 に答える