getline problem

Hello, I'm having troubles with this part of my program reading song names including spaces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	for (int i = 0; i < 3; i++)
	{
		cout << "Enter Artist first name: ";
		cin >> y;
		x[i].setArtist(y);
		
		cout << "Enter Artist last name: ";
		cin >> y;
		x[i].setArtistl(y);

		cout << "Enter Song name: ";
		getline(cin, y, '\n');
		x[i].setSong(y);

		output << x[i].getArtist() << " ";
		output << x[i].getArtistl() << endl;
		output << x[i].getSong() << endl << endl;
	}


Every time I run the program it will output:

Enter Artist first name: Justin
Enter Artist last name: Bieber
Enter Song name: Enter Artist First name:



Not sure if I'm using getline properly

Thanks!
Last edited on
Hope this helps

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
#include <iostream>
#include <string>
using namespace std;

int main() {

	string firstname = "";
	string lastname = "";
	string songname = "";
	int pause(0);

	cout << "Enter Artists first name" << endl;
	getline(cin, firstname);

	cout << "Enter Artists last name" << endl;
	getline(cin, lastname);

	cout << "Enter Artists last name" << endl;
	getline(cin, songname);

		cout << firstname << endl;
		cout << lastname << endl;
		cout << songname << endl;

		cin >> pause; //This line of code simply pauses the consol so that the program doesnt end automatically



}
Does that apply to using classes and arrays?
I'm assuming pause won't work since i have a loop to send the information to 3 arrays.
I'm also outputting them to a txt file.

I should have been more clear, my apologies.
Last edited on
I have solved it

I just decalred the strings how you did and changed the getline

Thanks!
Topic archived. No new replies allowed.