如何避免在此函数中跳过我的getline()? [英] How do I avoid my getline() getting skipped in this function?

查看:97
本文介绍了如何避免在此函数中跳过我的getline()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试制作一个能够根据给出的各种信息对歌曲进行排序的排序程序。这只是练习课程,非常简单的东西。我只是有点困惑为什么,在main函数的for循环中,我的readAlbum()调用中的getline()在第二个循环runthrough上被跳过。循环中的couts就在那里,我确保将值记录下来并存储在我想要的位置。



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.

#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

推荐答案

你正在混合调用 cin getline 会产生问题。对 cin 的调用只会在填充列表中的所有项目之前读取。这可能会在输入缓冲区中留下空格和/或行结尾。因此,如果您再调用 getline ,您将读取该残差数据,而不是您下次输入的信息。
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.


这篇关于如何避免在此函数中跳过我的getline()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆