Almost done with the coding but some getline problems

For this code, it asks for the first song, and stores and prints it. But after the first entry, i'm asked if i would liek to continue like i wanted, but then after i type in Y, it skips over my song question and getline function and straight into asking about if i want to continue or not, what am i doing wrong here?

This is my output

Enter the name of the song. I need help
Do you want to continue (y/n)? y
Enter the name of a song. Do you want to continue (y/n)? y
Enter the name of a song. Do you want to continue (y/n)? y
Enter the name of a song. Do you want to continue (y/n)? n
There were 4 song titles entered.
I need help


Where as my wanted output and expected output is


Enter the name of the song. I need help
Do you want to continue (y/n)? y
Enter the name of a song. Printing
Do you want to continue (y/n)? y
Enter the name of a song. And entering
Do you want to continue (y/n)? y
Enter the name of a song. These songs
Do you want to continue (y/n)? n
There were 4 song titles entered.
I need help
Printing
And entering
These songs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*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];
	}
}
Topic archived. No new replies allowed.