reading a string problem

When I call out add_record in main the output looks like this Title? Artist? Why is it not waiting for Title and skiping to Artist? It's like it's reads in title aoutomatically. Do I need to call out a constructor and initialize my strings.

class CD_Database {
public:
struct CD_TYPE {
string title;
string artist;
string albumsingle;
float price;
int tracks;
};

typedef struct CD_TYPE CD_T;

void Add_Record();

}; /*end class CD_Databae*/

/* This is input.h */
string read_string(string disp_string)
{
string answer;
cout << disp_string;
getline (cin, answer);
return (answer);

}


void CD_Database::Add_Record()
{
CD_T one_cd;

one_cd.title = read_string("Title? ");

one_cd.artist = read_string("Artist? ");

one_cd.tracks = read_int("Number of tracks? ");

if (yesno("Is it an Album y or n?"))
one_cd.albumsingle = "Album";
else
one_cd.albumsingle = "Single";

one_cd.price = read_float("Retail price (e.g. 4.65)? ");

cdsvector.push_back(one_cd);

} /*end Add_Record */

int main()
{
CD_DATABASE CD_Dbase;

CD_Dbase.Add_Record();
system("PAUSE");
return EXIT_SUCCESS;
Last edited on
I flushed out cin using cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') and it works now. Was this bad code that I wrote or is this just bugy?
Topic archived. No new replies allowed.