Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I'm trying to make a sorting program that will be able to sort songs based on various information it's given. It's just practice for classes, pretty simple stuff. I'm just a bit confused as to why, in the for loop in the main function, the getline() in my readAlbum() call gets skipped on the second loop runthrough. The couts in the loop are just there for me to make sure the values are getting taken and stored where I want them.

C++
#include <iostream>
#include <string>

using namespace std;

class Song
{
public:
	Song();
	void readArtist();
	void readAlbum();
	void readName();
	void readLength();
	void readYear();
	void setArtist(string Artist);
	void setAlbum(string Album);
	void setName(string Name);
	void setLength(string Length1, string Length2);
	void setYear(int Year);
	string getArtist();
	string getAlbum();
	string getName();
	string getLength();
	int getYear();
	~Song();

private:
	string artist, album, name, length;
	float year;
};

Song::Song()
{
	length = ":";
	year = 0;
	artist = "";
	name = "";
	album = "";
}

void Song::readArtist()
{
	string Artist;
	cout << "Enter artist name: ";
	getline(cin, Artist);
	setArtist(Artist);
}

void Song::setArtist(string Artist)
{
	artist = Artist;
}

void Song::readAlbum()
{
	string Album;
	cout << "Enter album name: ";
	getline(cin, Album);
	setAlbum(Album);
}

void Song::setAlbum(string Album)
{
	album = Album;
}

void Song::readName()
{
	string Name;
	cout << "Enter song name: ";
	getline(cin, Name);
	setName(Name);
}

void Song::setName(string Name)
{
	name = Name;
}

void Song::readLength()
{
	string LengthMin, LengthSec;
	cout << "Enter song length (minutes then seconds, separated by a space): ";
	cin >> LengthMin >> LengthSec;
	setLength(LengthMin, LengthSec);
}

void Song::setLength(string Length1, string Length2)
{
	length.insert(0, Length1);
	length.append(Length2);
}

void Song::readYear()
{
	int Year;
	cout << "Enter the year the song was released: ";
	cin >> Year;
	setYear(Year);
}

void Song::setYear(int Year)
{
	year = Year;
}

string Song::getArtist()
{
	return artist;
}

string Song::getAlbum()
{
	return album;
}

string Song::getName()
{
	return name;
}

string Song::getLength()
{
	return length;
}

int Song::getYear()
{
	return year;
}

Song::~Song()
{

}

int main()
{
	Song songs[20];
	bool done = false;

	for (int i = 0; (i < 20) || (!done); i++)
	{
		cout << "-Song " << i + 1 << "-" << endl;
		songs[i].readAlbum();
		cout << "Album: " << songs[i].getAlbum() << endl;
		songs[i].readArtist();
		cout << "Artist: " << songs[i].getArtist() << endl;
		songs[i].readName();
		cout << "Name: " << songs[i].getName() << endl;
		songs[i].readLength();
		cout << "Length: " << songs[i].getLength() << endl;
		songs[i].readYear();
		cout << "Year: " << songs[i].getYear() << endl;
	}

	return 0;
}


What I have tried:

not really sure what to try, bro
Posted
Updated 1-Dec-17 2:46am

1 solution

You are mixing calls to cin and getline which creates the problem. Calls to cin will only read until it has filled all the items in the list. That may leave whitespace and/or line ends in the input buffer. So if you then call getline you will be reading that residual data, rather than the information you next type.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900