#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>

using namespace std;

bool fileCheck(const char *filename)
{
  ifstream ifile("item.txt");
  return ifile;
}
int show(int p)
{
      
      srand( time(NULL) ); 
      int random_integer; 
      random_integer = ( rand ( ) % p ) + 1;
      return random_integer;
}



int main () {
    SetConsoleTitle("Random Item Generator");
    
    //Menu
    cout << "Please select an option from the menu below" << endl;
    cout << endl;
    cout << " -Menu List- " << endl;
    cout << endl;
    cout << "1. Add Item" << endl;
    cout << "2. Display All Items" << endl;
    cout << "3. Display Random Item" << endl;
    cout << "4. Quit" << endl;
    int a = 0;
        
    bool firstTime = true;
    
    while (a != 4) {
          
    if (firstTime == false) {
       cout << endl;
       cout << "Please select another option from the menu above" << endl;
       }
	   
	   cin >> a;
       
    		if (a == 1) {
					if (fileCheck("item.txt") == false) {
					string itemName;
					cout << "Please type the item that you would like to add." << endl;
					ofstream myfile;
					myfile.open ("item.txt");
					cin >> itemName;
					myfile << itemName << endl;
                    myfile.close();     
					}
                  
					else {
					string itemName;
					cout << "Please type the item that you would like to add. (Please use an underscore in place of any spaces)" << endl;
					ofstream myfile ( "item.txt", ios::app );
					cin >> itemName;
					myfile << itemName << endl;
					myfile.close();
					}
            }
              
            else if (a == 2) {
                   string fileData;
                   ifstream itemFile ("item.txt");
                   if (itemFile.is_open()) {
                   cout << " -List of Items- " << endl;
                   cout << endl;                    
                    while (! itemFile.eof() ) {
                          getline (itemFile,fileData);
                          cout << fileData << endl;
                          }
                          itemFile.close();
                          }

                   else {
                        cout << "Unable to open file" << endl;
                        }
               }
               else if (a == 3) {
                    
                    ifstream itemFile ("item.txt");
                    string line;
                    vector<string> lines;
                    int counter;          
                    while (! itemFile.eof() ) {
                    getline(itemFile,line, '\n');
                    lines.push_back( line );
                    counter++;

                     }
                     
                    cout << "Random Item: " << lines[show( counter )] << endl;
                    itemFile.close();
                    
                    }
                      firstTime = false;
          }        
          
    return 0;
}

This program that I have been writing will output a menu to the user, let the user pick from the menu and then depending on the option give or take information from the user. I was using a switch instead of if, elseif statements, but an error came up after I wrote the second case. I decided that it would be less of a hassle to just write it like this. What I am having trouble with is the vector statement under "else if (a == 3) {}". It will output the randomized item from the list, but only part of the time. I will run it other times and it will run, but as soon as I hit the 3 to select from the menu it tells me that windows has discovered an error, and then it will close my program soon after. Any ideas?

Thank you,
Derek

Recommended Answers

Two things I notice without testing it.
1. change these two lines:

while (! itemFile.eof() ) {
    getline (itemFile,fileData);

to this:

while (getline (itemFile,fileData))  {

Getline will automatically return 0 at the end of the find, so no need for the horrible eof() function.

…
Jump to Post
int counter;

You need to initialize it to 0.

Jump to Post

All 5 Replies

Two things I notice without testing it.
1. change these two lines:

while (! itemFile.eof() ) {
    getline (itemFile,fileData);

to this:

while (getline (itemFile,fileData))  {

Getline will automatically return 0 at the end of the find, so no need for the horrible eof() function.

same goes for lines 95 & 96

2: This line: srand( time(NULL) ); should only be executed ONCE in your entire program. So move it right after int main() A tip: You need to indent your code better because it's barely readable at this moment. If you're using VS you can use ctrl-a, ctrl-k, ctrl-f (in that order), but other IDE's have similair options to auto-indent your code.

[edit]
in your show function you do: ( rand ( ) % p ) + 1; . The +1 can result in accessing an element from your vector which is out of range, so I'd change it to ( rand ( ) % p ) ; if I were you :)

Well one thing i would suggest would be in every case , ur opening the file and then closing it , that generates redundant code.

else if (a == 2) {
string fileData;
ifstream itemFile ("item.txt");
itemFile.close();
else if (a == 3) {
ifstream itemFile ("item.txt");
itemFile.close();

u could perform that action once in for all , after all the job is done and the user wants to quit fro the program , u can close the file.

#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>

using namespace std;

bool fileCheck(const char *filename)
{
  ifstream ifile("item.txt");
  return ifile;
}
int show(int p)
{
      
      int random_integer; 
      random_integer = ( rand ( ) % p ) ;
      return random_integer;
}



int main () {
    SetConsoleTitle("Random Item Generator");
    srand( time(NULL) );
    //Menu
    cout << "Please select an option from the menu below" << endl;
    cout << endl;
    cout << " -Menu List- " << endl;
    cout << endl;
    cout << "1. Add Item" << endl;
    cout << "2. Display All Items" << endl;
    cout << "3. Display Random Item" << endl;
    cout << "4. Quit" << endl;
    int a = 0;
        
    bool firstTime = true;
    
    while (a != 4) {
          
    if (firstTime == false) {
       cout << endl;
       cout << "Please select another option from the menu above" << endl;
       }
	   
	   cin >> a;
       
    		if (a == 1) {
					
				ofstream itemFile;
				if (fileCheck("item.txt") == false) {
				string itemName;
				cout << "Please type the item that you would like to add." << endl;
				itemFile.open ("item.txt");
				cin >> itemName;
				itemFile << itemName << endl;     
				}
                  
				else {
				string itemName;
				cout << "Please type the item that you would like to add.";
				cout << "(Please use an underscore in place of any spaces)" << endl;
				ofstream itemFile ( "item.txt", ios::app );
				cin >> itemName;
				itemFile << itemName << endl;
				}
					itemFile.close();
            }
              
            else if (a == 2) {
                   string fileData;
                   ifstream itemFile ("item.txt");
                   if (itemFile.is_open()) {
                   cout << " -List of Items- " << endl;
                   cout << endl;                    
                       while (getline (itemFile,fileData)) {
                          cout << fileData << endl;
                          }
                          itemFile.close();
                     }

                   else {
                        cout << "Unable to open file" << endl;
                        }
               }
			   
               else if (a == 3) {
                    
                    ifstream itemFile ("item.txt");
                    string line;
                    vector<string> lines;
                    int counter;          
                    while (getline (itemFile,line)) {
                    lines.push_back( line );
                    counter++;

                     }
                    cout << "Random Item: " << lines[show( counter - 1 )] << endl;  
                    itemFile.close();                 
                    }
                      firstTime = false;
          }        
          
    return 0;
}

it ends up doing the same thing... even with the fixes. and for some reason when i add it in here, it makes it look messy, I'm sorry. Lol in my compiler it is perfect lol

int counter;

You need to initialize it to 0.

commented: Nice catch, I should've seen that :) +12

oh wow. IT works perfect lol. Thank you.

Be a part of the DaniWeb community

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