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.
- View Characters
- View Items
- Add Character
- 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();
}
}