C++ for循环中的getline和cout语句被截断

C++ for循环中的getline和cout语句被截断,c++,truncate,getline,C++,Truncate,Getline,对于这段代码,它请求第一首歌,并存储和打印它。但是在第一次输入之后,我被问到是否愿意继续,但是在我输入Y之后,它跳过了我的歌曲问题和getline函数,直接问我是否想继续,我在这里做错了什么 这是我的输出 输入歌曲的名称。我需要帮助 是否要继续(是/否)?y 输入歌曲的名称。是否要继续(是/否)?y 输入歌曲的名称。是否要继续(是/否)?y 输入歌曲的名称。是否要继续(是/否)?n 输入了4首歌曲标题。 我需要帮助 我想要的输出和期望的输出在哪里 输入歌曲的名称。我需要帮助 是否要继续(是/否)

对于这段代码,它请求第一首歌,并存储和打印它。但是在第一次输入之后,我被问到是否愿意继续,但是在我输入Y之后,它跳过了我的歌曲问题和getline函数,直接问我是否想继续,我在这里做错了什么

这是我的输出

输入歌曲的名称。我需要帮助
是否要继续(是/否)?y
输入歌曲的名称。是否要继续(是/否)?y
输入歌曲的名称。是否要继续(是/否)?y
输入歌曲的名称。是否要继续(是/否)?n
输入了4首歌曲标题。
我需要帮助

我想要的输出和期望的输出在哪里

输入歌曲的名称。我需要帮助
是否要继续(是/否)?y
输入歌曲的名称。打印
是否要继续(是/否)?y
输入歌曲的名称。并进入
是否要继续(是/否)?y
输入歌曲的名称。这些歌曲
是否要继续(是/否)?n
输入了4首歌曲标题。
我需要帮助
打印
并进入
这些歌

/*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];
    }
}
/*此程序将要求用户输入歌曲列表。每次用户输入时都这样做
一首歌,他/她将被询问是否要输入另一首歌,如果是,则他们将输入另一首歌。最多15首歌。
此程序将帮助用户跟踪他们的歌曲。然后程序会输出歌曲的数量以及它们是什么。
输入:一首歌。然后是或否,如果是,另一首歌,如果否,程序将输出。
输出:歌曲的数量,然后是歌曲本身,每首歌曲都有自己的个人行。
处理:将有两个功能,其中一个将要求用户的歌曲和存储它们,另一个将是计算
输出。
*/
#包括
#包括
使用名称空间std;
int输入(字符串[15],int);
void printary(字符串[15],int);
int main()
{
常数int arraysize=15;
字符串数组[arraysize];
int x=输入(songarray,arraysize);
问题是,您的“cin>>答案”只读取一个字符,但您键入“y”,然后按enter键,因此换行符仍然位于cin中。因此,当您的循环返回getline时,只读取该换行符,结果是一个空字符串

如果您这样做:

string dummy;
getline(cin, dummy);
在“cin>>应答”之后,它将吃掉尾随的换行符。

问题是,您的“cin>>应答”读取一个字符,但您键入“y”,然后按enter键,因此换行符仍然位于cin中。因此,当您的循环返回getline时,只读取该换行符,而最终得到一个空字符串

char answer;
cin >> answer;
如果您这样做:

string dummy;
getline(cin, dummy);
在“cin>>应答”之后,它将吃掉尾随的换行符

char answer;
cin >> answer;
您按了哪些键来回答程序的问题?哪个键在
回答中结束了?哪个键保留在stdin中,在您键入任何其他内容之前终止下一个getline

char answer;
cin >> answer;
您按了哪些键来回答程序的问题?哪个键在
回答中结束了?哪个键保留在stdin中,在您键入任何其他内容之前终止下一个getline

char answer;
cin >> answer;
问题确实存在:寻找另一种解决方案


问题确实存在:寻找另一种解决方案

这是家庭作业吗?如果是,这没关系,但我们想知道。尝试使用
cin>>标题[i]
而不是
getline
这是家庭作业吗?如果是,这没关系,但我们想知道。尝试使用
cin>>标题[i]
而不是
getline
它完全起作用了,它非常有意义,因为对我来说,我看着代码思考,它不可能只是随机跳过我的cout语句,它一定是接受了另一个字符或其他东西。现在它有意义了,谢谢你1它完全起作用了,它非常有意义,因为对我来说,我是在看看着代码思考,不可能只是随机跳过我的cout语句,它一定是接受了另一个字符或其他东西。现在它有了意义,谢谢1