|
-
January 2nd, 2011, 10:17 AM
#6
Re: How to manipulate vector of class type in C++
 Originally Posted by ertss
But I met another problem with my code using getline(cin,temp);
I changed my code to this. The thing is, the first time in the do...while loop, everything is good.
But after i input 'y' to execute another do...while loop, it just skip the new_book.getname() part.
I believe it's something wrong with the getline usage. Because if i changed it to cin>>temp, it works just
fine.
The problem is that after you read the price using >> the endline following the price remains in the input stream. Next time you call getline, it will read an emtpy line (i.e. only the endline character). The easiest way to avoid this is to use getline all the time, then use a stringstream to parse the input.
Code:
#include <iostream>
#include <sstream>
int main()
{
std::string name;
std::getline(std::cin, name);
int price;
std::string line;
std::getline(std::cin, line);
std::istringstream iss(line);
iss >> price;
}
Also see http://www.parashift.com/c++-faq-lite/input-output.html
Last edited by D_Drmmr; January 2nd, 2011 at 10:19 AM.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|