How come that when i compile this and run it through the loop after picking menu choice 1 and then Y for looping back to main menu that it doesn't display the current data file you're in ?
Ohh okay thank youu, i tried just fileName instead of fileName1 and it works. Also how would i write data into a file through user input ? , this doesn't seem to be it because I'm getting an operand error in the writeData == 'Y' if block.
int case1(string & fileName)
{
string enterData;
ifstream outFile;
ofstream outFile1;
string fileName1;
int createReadOption;
char writeData;
cout << "If you would like to select a file, press (1).\n";
cout << "If you would like to create a file, press (2).\n";
cout << endl;
cin >> createReadOption;
cout << endl;
if (createReadOption == 1)
{
cout << "Name of data file: ";
cin >> fileName;
fileName1 = fileName + ".txt";
cout << endl;
outFile.open(fileName1.c_str());
if (outFile.fail())
{
cout << "The file did not open successfully.\n";
}
else
{
cout << "The file has been opened successfully.\n" << endl;
}
}
elseif (createReadOption == 2)
{
cout << "Select a name for your new data file: ";
cin >> fileName;
fileName1 = fileName + ".txt";
cout << endl;
outFile1.open(fileName1.c_str());
if (outFile1.fail())
{
cout << "The file was not created successfully.\n";
}
else
{
cout << "The file has been created successfully.\n" << endl;
cout << "Would you like to write data to the file now ? [Y/N]: " << endl;
cin >> writeData;
if (writeData == 'Y' || writeData == 'y')
{
cout << "Please type in the Data you would like to enter into the file: ";
outFile1 >> enterData;
}
else
{
}
}
}
else
{
cout << "Please select either one or two";
cin >> createReadOption;
}
return 0;
}
This says "fetch some data from the file, and store it in the string named enterData". Is that what you meant to do, or did you mean to write data into the file?
Ooh i meant to write data, yea i just tried the less than operands and the error went away.
Actually something weird just happened.. when i run this code it compiled right and started but then it pop up an error saying " system. NullReferenceObjection " or something along those lines and would switch to something crazy like 2600 lines of code and say its in debugging mode.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main(string & fileName);
int case1(string & fileName);
int case2(string & fileName);
int case3();
int main(string & fileName)
{
ifstream outFile;
ofstream outFile1;
string fileName1;
char repeatMenu = 0;
do {
system("cls");
cout << "** MENU **" << endl << endl;
cout << "Current Data File: " << fileName;
cout << endl << endl;
int menuChoice;
cout << "(1) Select / Create data file (.txt file extension will be added automatically)\n";
cout << "(2) Display all numbers, total and average\n";
cout << "(3) Display all numbers sorted\n";
cout << "(4) Search for a number and display how many times it occurs\n";
cout << "(5) Display the largest number\n";
cout << "(6) Append a random number(s)\n";
cout << "(7) Exit the program\n";
cout << endl;
cout << "Menu choice: ";
cin >> menuChoice;
if (menuChoice < 1 || menuChoice > 7)
{
do
{
cout << "Menu choice: ";
cin >> menuChoice;
} while (menuChoice < 1 || menuChoice > 7);
}
cout << endl;
switch (menuChoice)
{
case 1: case1(fileName);
break;
case 2: case2(fileName);
break;
}
cout << endl;
cout << "Would you like to return to the main menu ? [Y/N]: ";
cin >> repeatMenu;
} while (repeatMenu == 'Y' || repeatMenu == 'y');
system("pause");
return 0;
}
int case1(string & fileName)
{
string enterData;
ifstream outFile;
ofstream outFile1;
string fileName1;
int createReadOption;
char writeData;
cout << "If you would like to select a file, press (1).\n";
cout << "If you would like to create a file, press (2).\n";
cout << endl;
cin >> createReadOption;
cout << endl;
if (createReadOption == 1)
{
cout << "Name of data file: ";
cin >> fileName;
fileName1 = fileName + ".txt";
cout << endl;
outFile.open(fileName1.c_str());
if (outFile.fail())
{
cout << "The file did not open successfully.\n";
}
else
{
cout << "The file has been opened successfully.\n" << endl;
}
}
elseif (createReadOption == 2)
{
cout << "Select a name for your new data file: ";
cin >> fileName;
fileName1 = fileName + ".txt";
cout << endl;
outFile1.open(fileName1.c_str());
if (outFile1.fail())
{
cout << "The file was not created successfully.\n";
}
else
{
cout << "The file has been created successfully.\n" << endl;
cout << "Would you like to write data to the file now ? [Y/N]: " << endl;
cin >> writeData;
if (writeData == 'Y' || writeData == 'y')
{
cout << "Please type in the Data you would like to enter into the file: ";
outFile1 << enterData;
}
else
{
}
}
}
else
{
cout << "Please select either one or two";
cin >> createReadOption;
}
return 0;
}
int case2(string & fileName)
{
cout << "Hello\n\n";
// i took out the opening file here i used hello and goodbye to see if it worked
cout << "\n\nGoodbye\n\n";
return 0;
}
int case3()
{
system("pause");
return 0;
}