954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

While loop user input

I am attempting to take names and weights from strings and just simply output them. For some reason it can't get out of the while loop, it just sets there waiting for the users to input names and weights. Here's my code

#include <iostream>
#include <string>
#include <vector>


using namespace std;

int main (void)

{
    // creating variables for input
	string name;
	int weight;
	
	    // creating vectors
		vector<string> names;
		vector<int> weights;
	
	cout << "Enter names and weight" << endl;
	while(cin >> name, weight)
	{

		names.push_back(name);
		weights.push_back(weight);
	    
		
	}
	
	// outputs names and weight
	    for( unsigned int n = 0; n < weights.size(); n++ )
    {
        cout << "[" << n << "] " << weights[n] << "   " << names[n] << endl;
    }
  cout << "done." << endl;
 return 0;
 
 }


I am self teaching myself C++ with someone helping me as well, my issue is. I am currently learning from Accelerated C++ book. I am on chapter three.

The problem is it doesn't come out of the while loop. It doesn't know when the user will stop input. How do I do it this way, but make it so the computer knows when the while(cin >> ) stops from the user input.

Democles
Light Poster
33 posts since Jun 2009
Reputation Points: 10
Solved Threads: 1
 

I am attempting to take names and weights from strings and just simply output them. For some reason it can't get out of the while loop, it just sets there waiting for the users to input names and weights. Here's my code

#include <iostream>
#include <string>
#include <vector>


using namespace std;

int main (void)

{
    // creating variables for input
	string name;
	int weight;
	
	    // creating vectors
		vector<string> names;
		vector<int> weights;
	
	cout << "Enter names and weight" << endl;
	while(cin >> name, weight)
	{

		names.push_back(name);
		weights.push_back(weight);
	    
		
	}
	
	// outputs names and weight
	    for( unsigned int n = 0; n < weights.size(); n++ )
    {
        cout << "[" << n << "] " << weights[n] << "   " << names[n] << endl;
    }
  cout << "done." << endl;
 return 0;
 
 }

I am self teaching myself C++ with someone helping me as well, my issue is. I am currently learning from Accelerated C++ book. I am on chapter three. The problem is it doesn't come out of the while loop. It doesn't know when the user will stop input. How do I do it this way, but make it so the computer knows when the while(cin >> ) stops from the user input.


I assume you want this in line 20:

while(cin >> name >> weight)


not this:

while(cin >> name, weight)


What would you like the user to enter when he/she is done? You need to tell them (and the computer) how to signify that input is finished. It's not like reading from a file when you can detect end of file.

cout << "Enter names and weight.  Enter 'Done' when finished" << endl;
	cin >> name;
	while(name.compare ("Done") != 0)
	{
                cin >> weight;
		names.push_back(name);
		weights.push_back(weight);
                cin >> name;
	}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

I assume you want this in line 20:

while(cin >> name >> weight)

not this:

while(cin >> name, weight)

What would you like the user to enter when he/she is done? You need to tell them (and the computer) how to signify that input is finished. It's not like reading from a file when you can detect end of file.

cout << "Enter names and weight.  Enter 'Done' when finished" << endl;
	cin >> name;
	while(name.compare ("Done") != 0)
	{
                cin >> weight;
		names.push_back(name);
		weights.push_back(weight);
                cin >> name;
	}

Is there a way to do without the compare? I am unfamiliar with this function and it wasn't explained. Lets just say the user types Done in the console.

Democles
Light Poster
33 posts since Jun 2009
Reputation Points: 10
Solved Threads: 1
 

the compare() function is just like "==" for std::strings.

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 
Is there a way to do without the compare? I am unfamiliar with this function and it wasn't explained. Lets just say the user types Done in the console.


You can change that line to:

while(name != "Done")


and it'll do the same thing.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

I'm not sure, but I don't think that works. I probably have to use the compare command.

This is my revised code. maybe my for loop is wrong?

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main (void)
{

// creating variables for input
string name;
int weight;



// creating vectors
vector<string> names;
vector<int> weights;

cout << "Enter names and weight, press 'Done' when finished." << endl;

while(name.compare ("Done") !=0)
{
cin >> name >> weight;
names.push_back(name);
weights.push_back(weight);
}


// outputs names and weight
for( unsigned int n = 0; n < weights.size(); n++ )
	{
	 cout << "[" << n << "] " << weights[n] << " " << names[n] << endl;
	}

cout << "All Done." << endl;

return 0;

}
Democles
Light Poster
33 posts since Jun 2009
Reputation Points: 10
Solved Threads: 1
 

Look at my loop again:

cout << "Enter names and weight.  Enter 'Done' when finished" << endl;
	cin >> name;
	while(name != "Done")
	{
                cin >> weight;
		names.push_back(name);
		weights.push_back(weight);
                cin >> name;
	}


Look at your loop:

cout << "Enter names and weight, press 'Done' when finished." << endl;

while(name.compare ("Done") !=0)
{
cin >> name >> weight;
names.push_back(name);
weights.push_back(weight);
}


Note my line 2. I initialize name so it definitely has a value BEFORE the while loop. You don't do this.

I then read in the weight in line 5. I have ALREADY read in the name, so I don't ask for the name again. You ask for both in the same line.

I now have a name and a weight. I push them onto the vectors in lines 6 and 7.

In line 8, I ask for the name again.

Try my original code that I posted in post 2. See if it works for you. Then change the while line to what I posted in post 4 since you didn't want to use compare. That's fine. But whether you use compare is not the issue here. Give it a try and see if that solves your problem. if it doesn't, post the entire code as you did, so we can run the same program.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You