我的for循环中的getline和cout语句被截断了
My getline and cout statement within my for loop are being truncated
对于这段代码,它会询问第一首歌曲,并存储和打印它。但在第一个条目之后,我会被问到是否会像我想要的那样撒谎继续,但在我键入Y之后,它会跳过我的歌曲问题和getline函数,直接问我是否想继续,我在这里做错了什么?
这是我的输出
输入歌曲的名称。我需要帮助
是否要继续(y/n(?y
输入歌曲的名称。是否要继续(y/n(?y
输入歌曲的名称。是否要继续(y/n(?y
输入歌曲的名称。是否要继续(y/n(?n
共输入4首歌曲标题
我需要帮助
我想要的输出和期望的输出是
输入歌曲的名称。我需要帮助
是否要继续(y/n(?y
输入歌曲的名称。打印
是否要继续(y/n(?y
输入歌曲的名称。并输入
是否要继续(y/n(?y
输入歌曲的名称。这些歌曲
是否要继续(y/n(?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];
}
}
问题是,您的"cin>>答案"读取单个字符,但您键入"y",然后按enter键,因此换行符仍位于cin中。因此,当循环返回到getline时,只需读取换行符,最后得到一个空字符串。
如果你这样做:
string dummy;
getline(cin, dummy);
在"cin>>答案"之后,它将吃掉后面的换行符。
char answer;
cin >> answer;
问题确实存在:在cin.ignore中查找另一个解决方案
char answer;
cin >> answer;
你按了哪些键来回答程序的问题?哪一个最终进入answer
?哪一个保留在stdin中,在键入其他内容之前终止下一个getline?
相关文章:
- 在 COUT 语句中使用 COUT 调用函数
- C++程序在一定行后未正确编译[cout语句]
- 这种递归是如何到达 COUT 语句的?
- 0x%08lx 格式说明符在 printf 语句到 cout
- 我收到一个断言错误,但是当我编写 cout 语句时,它会消失
- 一个语句中没有多个 cout 的错误/警告
- 为什么 cout 语句在传递指向函数的指针时没有执行?
- 两个 cin / cout 语句合并为一个
- 在 cout 语句中首先运行什么?(C++17)
- if 语句仅在前面有调试 cout 行(C 中的多线程)时才通过
- 使用自定义宏来简化 cin 和 cout 语句等内容被认为是更好还是更差
- 单个cout中的多个语句产生不同的结果
- 尽管我的功能和cout语句的放置错误,但我的程序为什么会起作用
- 如何在COUT语句中使用移动运算符
- 如果语句在第一次尝试后没有给出 cout
- C :Bool方法在COUT语句之后返回意外数字
- cout 输出乱码消息行,而不是实际用户的 cout 语句
- 添加 cout 语句会更正输出
- 如何仅使用循环/控制语句和两个cout语句创建一个倒三角:一个输出“#”和一个输出' n'
- 为什么语句"cout << '\\\\';"没有失败?