While Loops

Im trying to use a while loop here for a total of four times. Unfortunately, its not working correctly. At first, I type in my name and how many boxes were sold. WHen it goes to a second loop. It does show "name" on the console, but the cursor is not there. Instead it goes to boxes sold. How do I fix this issue?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//declare a counter for loop
	int counter;
	counter=0;
//Ask for the user name and the number of boxes sold
	while (counter<=4)
	{
	cout<<"\n\tName: ";
	getline(cin,name);
	//Ask user how many boxes were sold
	cout<<"\n\tBox Sold: ";
	cin>>boxessold;
	counter++

	
Last edited on
mixing getline and stream extraction without knowing how they work is a recipe for disaster.
You can add std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); after line 11.
And do not forget to #include <limits>
Thank you for your response :) Can you explain to me what these function even mean please :) I'm totally new to this! :)
when you are writing hello in console your input actually is "hello\n" where \n is a newline symbol.
So your input for this program cam be:
Mike\n
22\n
Getline gets ann symbols from currennt symbol up to first \n discarding it, so after it input left in stream would be:
22\n
operator >> will skip all whitespaces, then get all characters it could until whitespace character or first "bad" character. So input left would be:
\n
Do you see problem now? First symbol encountered by getline will be \n so it takes it and discards returning an empty string.

function ignore will skip some characters from input stream. '\n' will tell that it should stop skipping after it skips a newline and
std::numeric_limits<std::streamsize>::max() tells that there is no limit on how many symbols it should skip
Last edited on
I understood that part now :D Let me make sure if im correct on this. When a user types in "cin>>name" The console will print out only the first part of name and store the last name for a later use.

Now if I use getline, I thought I'm suppose to get every character on that line. According to you, it will get every character on that line until its a new line? correct?

lastly, is there another method of doing this :) We have learned about the ignore functions in our class, but nothing about limits or the function you provided.
You can write just .ignore(100500, '\n');
Using that function is just the correct and robust method of doing it
numeric_limits is a template whch, well, stores different limits on dofferent types.
It has a max() member function which will give you maximum value that type can hold.
for example numeric_limits<unsigned char>::max() will return 255.
streamsize is a special type defined in iostream library which holds size of stream. So std::numeric_limits<std::streamsize>::max() is a maximum possible size of stream. If you will read reference on ignore() you will notice that if passed in ignore that will make stream not bother with maximum number of ignored symbols.
Starting to grasp the concept now. Now, suppose I want to output the name on the console. What it does now is that it gives me only the first name. Is there any intermediate solution to that?

while (counter<4)
{
cout<<"\n\tName: ";
cin>>name;
cin.ignore(10500,'n');
cout<<"\n\tname is: "<<name;
//Ask user how many boxes were sold
cout<<"\n\tBox Sold: ";
cin>>previousbox;

boxessold= boxessold+previousbox;

counter++;
}
No, there is no solution. You have to use getline for that. And it is easier to use >> for integer inputs. So best way would do as you already did and insert an ignore between last >> and first getline
Topic archived. No new replies allowed.