problem with string -get line with while loop

hi there, why can't i get the line again in while loop ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include<iostream>
#include<string>

int main()
{
	char again;
	do 
	{
	std::string name;
	std::cout<<"please enter your full name"<<std::endl;
	std::getline(std::cin,name);
	std::cout<<"hello " <<name<<std::endl;
	std::cout<<"do you want to enter another name (y/n) ? "<<std::endl;
	std::cin>>again;
	}while (again=='y');
	return 0;
}
https://www.cplusplus.com/reference/istream/istream/ignore/
You need to be careful when you mix line-based input like getline, with type-based input like >>

> std::cin>>again;
This will leave a \n on the input stream.
Use cin.ignore() to get rid of it.
put this:
std::cin.ignore();
between line 14 and 15
and it will work.

see https://stackoverflow.com/questions/33316564/mixing-cin-and-getline-input-issues
or just memorize that mixing cin and getline is troublesome because they handle the stream buffer differently and you need to clean it out before swapping between them.

also, does not matter here, but putting the string in the loop creates and destroys it over and over, which is inefficient and in some places (not here) could cause a bug. move the string variable out of the loop.
Last edited on
> in some places (not here) could cause a bug.
¿what kind of bug?
put this:
std::cin.ignore();
between line 14 and 15

That will ignore a single character, but what if the user types "yes" instead of "y"? It's better to ignore everything through the newline with something like:
std::ignore(1000000, '\n');

But that "only" works if the input line is less than 1 million characters. If you really want to bullet-proof your code to handle a 2TB input file with a newline at the next-to-last character, you'd do this:
1
2
3
#include <limits>
...
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

what kind of bug?

this kind, though imagine harder to spot in more complicated code doing more interesting things.

for(int i = 0; i < 10; i++)
{
int sum = 0; //this should be outside the loop.
sum += 1;
}
"jonnin" "dhayden" "ne555" "salem c"
thank you guys for your answers i appreciate your help, just a small question, how do you know this information my friends? how should i know as a beginner these issues whilst the code sequence is logically correct? the problems like this disappointing me when i look to my code and revise it many time and couldn't find the problem, i need your advice please as a professionals :) .. many thanks again .. and glad to find support in such friendly way.

best regards
semsemdiver
semsemdiver wrote:
how do you know this information?

Time and experience. Make mistakes, learn from them. Ask others, as you did here. Read the source materials on C++ as well as good books written by experts along with online tutorials.

Regarding books.... multiple:
https://isocpp.org/wiki/faq/how-to-learn-cpp#buy-several-books

Online tutorials:
There a tutorial here at CPlusPlus, though it is a bit outdated (very little C++14, no C++17/20):
http://www.cplusplus.com/doc/tutorial/

Another good tutorial that is kept up-to-date:
https://www.learncpp.com/

A technical reference of C++, though it can be a bit 'dense' for a beginner:
https://en.cppreference.com/w/cpp

Most printed books tend to be outdated, even recent ones. Online resources can be easier to keep up-to-date.

C++ is continuing to evolve. C++11 changed the language at a fundamental level from what it was before. C++14/17 augmented, added and removed some features. C++20 is close to being officially released, adding more fundamental changes. What works with an earlier standard is not guaranteed to work with later standards. Or has been considerably improved. Random number generation with C++11, for instance.

Above all else, keep writing code. Push the boundaries of what you know.

Become a 'sponge.' Expect to fail and make mistakes.

On a side note, nice to see you didn't fall into the potential using namespace std; trap. It defeats the purpose of having namespaces.
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
c++ is better than many languages as far as weird stuff to memorize goes, but this is just one of those things that you need to commit to memory. How did I know? Because I had the same problem at some point, and got help from someone to fix it, and they said the same thing... memorize this as an oddity that has to be dealt with.

Hi "Furry Guy", thanks a mil for your advice i appreciate it.
C++ is very big, flexible, powerful and complex than most other languages but i love it, i feel confidence when i write code with it but i don't know how to improve my skills in programming with C++ i don't know what to read in C++ books, there are many out there, there are many libraries should i study all of them ? or should i after learning the basics take a specialization and read and learn every thing about it like windows app programming or mobile app? or if i have a good basics learning will i become good in every thing?
bro i feel like i'm sailing in a sea without a compass :( .. anyway thanks a lot for your advice i will hit all the links that you provided to me.

thanks again.
semsemdiver
Unlike a snake (or dinosaur like a T-Rex) humans can't eat an entire cow whole. We have to cut it up into smaller pieces. The same goes for learning C++. Take it one small piece at a time.

The tutorials I linked earlier, especially LearnCPP, do that. You can go from lesson to lesson in order, or bounce around from the site index provided.

About books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

That is very good list.
The current standard is C++17, with C++20 due shortly. Some compilers already support parts of C++20. See https://en.cppreference.com/w/cpp/compiler_support

As well as books, there are various on-line C++ blogs which IMO are worth reading.

https://www.bfilipek.com/
https://www.fluentcpp.com/
http://www.modernescpp.com/index.php
https://mariusbancila.ro/blog/

In addition to books already mentioned, I would also add:
Beginning C++17 : From novice to professional by Ivor Horton (Beginning C++20 is coming Feb 2021)
Professional C++ by Marc Gregoire

Also, Leanpub also has some C++ books See https://leanpub.com/bookstore/book?search=c%2B%2B
Last edited on
"Furry Guy" "seeplus" thank you guys for your help i appreciate it .. actually now i'm studding a second course in c++ i studied one then i felt i still need to know more "all courses are recorded videos" so, i started another one with another instructor and as i sent you this problem about get line it is obvious that there is much more out there because the first instructor had covered most of c++ subjects and i reached with him to inheritance and polymorphism but i hadn't faced this problem before and as u see it is not a logic problem it is understanding problem or technical problem of how to use C++, what to use and what not to use with a particular code and what is better to avoid but you can use " like using std:: instead of using name space" do you understand me? it is an understanding problem about how to use C++ and how does C++ work, i'm wondering what to study after finishing my second course? should i study data structure to get deeper understanding of problem solving? or start a specialize in programming like web development or windows app or .. , or should i study software engineering? please tell me your story and experiences with C++ to learn from it.
thanks again my friends, i appreciate your help.
best regards.
semsemdiver
Last edited on
Topic archived. No new replies allowed.