The following is an excerpt from a complete program that runs without any errors. It has 2 file dependencies which it uses to access and store information.

The program, in its entirety, displays a menu with 4 options the user can choose from.

  1. View Characters
  2. View Items
  3. Add Character
  4. Exit

The problem that I am currently having happens when the user is prompted for the character name. In the current situation, you must replace any spaces with an underscore(_).

I have known about the getline() function for a while and I have used it on many of my simple programs. The difference is that I am requesting more than one input. I felt that there must be a simple solution, maybe something I was overlooking.

I thought I found the solution with this article from your site.
http://www.daniweb.com/tutorials/tutorial71858.html
I thought I just needed to use the cin.ignore(numeric_limits<streamsize>::max(), '\n'); line of your code and add the #include <limits> header.

I tried implementing it in multiple ways, but to no avail.

Would someone be able to help me with my input problem?
It would be very appreciated.

Also, if you need the whole file just let me know.

Thank you,
Derek O. Tinkey

//Option 3
		else if (input == 3) {
			
			//Seeds the random function based on the time at compilation
			srand ( time(0) );

			//Set the number of sides that the die will have
			const int dieRoll = 6;
			char charName[50];
			int itemNum;
			vector<string> itemData;
			
			//Constitution and Intelligence are figured by adding 3 die rolls together
			int constitution = (random(dieRoll) + random(dieRoll) + random(dieRoll));
			int intelligence = (random(dieRoll) + random(dieRoll) + random(dieRoll));
			int health = (2 * constitution) + 10;
			
			/*If the file doesn't exist it creates a file, prompts you 
			for some information about the character you wish to create,
			generates a character based on said information, prints the 
			information to the screen, and then stores the information
			in a text file.*/

			if (fileCheck("characters.txt") == false) {
				ofstream charFile;
				charFile.open ("characters.txt");
				
				cout << "Character Name: ";
				cin >> charName;
				cout << "Number of Items: ";
				cin >> itemNum;

				ifstream itemFile ("item.txt");
                string line;
				string itemList;
                vector<string> lines;

                int counter = 0;
				
				//Asserts that the number of items is less than 					
				while (itemNum > 5) {
					cout << "You may only have up to 5 items." << endl;
					cin >> itemNum;	
				}
				
				while (itemNum > 0) {
					
					while (getline (itemFile,line)) {
						lines.push_back( line );
						counter++;
					}
					
					string randomLine = lines[random(counter - 1)];
					itemData.push_back(randomLine);
					
					itemNum--;
				}

				cout << endl;
				cout << " -Character Information- " << endl;
				cout << endl;
				cout << "Name: " << charName << endl;
				charFile << "Name: " << charName << endl;
				cout << endl;
				cout << " -Stats- " << endl;
				cout << endl;
				cout << "Intelligence: " << intelligence << endl;
				charFile << "Intelligence: " << intelligence << endl;
				cout << "Constitution: " << constitution << endl;
				charFile << "Constitution: " << constitution << endl;
				cout << "Health: " << health << endl;
				charFile << "Health: " << health << endl;
				cout << "Items: ";
				charFile << "Items: ";

				int count = itemData.size();

				while (count > 0) {
					if (count != 1) {
						cout << itemData[count - 1] << ", ";
						charFile << itemData[count - 1] << ", ";
					}

					else {
						cout << itemData[count - 1] << "." << endl;
						charFile << itemData[count - 1] << "." << endl;
					}
					count--;
				}
				
				charFile.close();
				itemFile.close();
			}
			
			/*Otherwise, the program prompts you for some information 
			about the character you wish to create, generates a character
			based on said information, prints the information to the screen,
			and appends the information to the text file that has previously
			been made*/

			else {
				ofstream charFile;
				charFile.open("characters.txt", ios::app );
				charFile << endl;
				cout << "Character Name: ";
				cin >> charName;
				cout << "Number of Items: ";
				cin >> itemNum;
				
				ifstream itemFile ("item.txt");
				string line;
				vector<string> lines;
				
				int counter = 0; 

				//Asserts that the number of items is less than 5
				if (itemNum > 5) {
					cout << "You are only alloted 5 items." << endl;
					cout << "Please enter a new amount." << endl;
					cin >> itemNum;
					
					while (itemNum > 5) {
						cout << "You may only have up to 5 items." << endl;
						cin >> itemNum;				
					}
				}

				while (itemNum > 0) {
					
					while (getline (itemFile,line)) {
						lines.push_back( line );
						counter++;
					}
					
					string randomLine = lines[random(counter - 1)];
					itemData.push_back(randomLine);

					itemNum--;
				}
				
				cout << endl;
				cout << "-Character Information-" << endl;
				cout << endl;
				cout << "Name: " << charName << endl;
				charFile << "Name: " << charName << endl;
				cout << endl;
				cout << "-Stats-" << endl;
				cout << endl;
				cout << "Intelligence: " << intelligence << endl;
				charFile << "Intelligence: " << intelligence << endl;
				cout << "Constitution: " << constitution << endl;
				charFile << "Constitution: " << constitution << endl;
				cout << "Health: " << health << endl;
				charFile << "Health: " << health << endl;
				
				cout << "Items: ";
				charFile << "Items: ";
				
				int count = itemData.size();
				
				while (count > 0) {

					if (count != 1) {
						cout << itemData[count - 1] << ", ";
						charFile << itemData[count - 1] << ", ";
					}
					
					else {
						cout << itemData[count - 1] << "." << endl;
						charFile << itemData[count - 1] << "." << endl;
					}
					count--;
				}
				charFile.close();
				itemFile.close();
			}
		}

Recommended Answers

All 6 Replies

Replacing line 29 with cin.getline(charName,50); would be the best way to go.
The cin.ignore helps in the situation where there are newlines ('\n', resulting from pressing enter after your input) left in the input stream.
Since you would have the cin.getline statement first and then the cin >> there won't be a problem with an excess newline being left in the input stream since cin.getline reads in and disposes of the new line character (versus having the cin >>statement first which can leave an extra '\n' in the stream to get picked up by a subsequent getline thereby ending the getline before it starts).

First let me thank you for taking your time to reply to my question.
But their is a slight glitch when I replace the cin >> charName; with the cin.getline(charName, 50).

The error I have is when I choose option 3 it completely skips over the first prompt and goes on to the second one. More specifically it prints them both on the same line and in that situation you can't answer the first question

How are you getting the input for the variable "input"? If it's a cin >> as I suspect it is, now you would need the cin.ignore() after that cin statement or before the cin.getline on line 29. You can use that variant of ignore() you had before but since it's probably 1 newline gunking up the works the cin.ignore() with no arguments will probably suffice.

How are you getting the input for the variable "input"? If it's a cin >> as I suspect it is, now you would need the cin.ignore() after that cin statement or before the cin.getline on line 29. You can use that variant of ignore() you had before but since it's probably 1 newline gunking up the works the cin.ignore() with no arguments will probably suffice.

Yes, that was exactly what I needed. One more thing, how would I change the input method in a way that would allow me to use an unlimited amount of characters without underscores? I realized that you would need to change the declaration of the intial variable from the char charName[50] to string charName and probably the cin.getline(charName, 50) to getline(cin, charName), but would you need anything else to make sure there aren't any errors?

Yes, that was exactly what I needed. One more thing, how would I change the input method in a way that would allow me to use an unlimited amount of characters without underscores? I realized that you would need to change the declaration of the intial variable from the char charName[50] to string charName and probably the cin.getline(charName, 50) to getline(cin, charName). but would you need anything else to make sure there aren't any errors?

The way you described should work just fine. You'll still need the cin.ignore before the getline(cin,charName) statement.

Ok, thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.