One of the things i need to do in my switch statement is open/create a file if it doesn't exist, I have it all coded but I'm just not sure if it does what it is supposed to do.
Also if i have any other minor/major errors in any other cases if you pointed them out if would help a lot, in most of the case functions I've just wrote cout's of what I'm going to do in them but I'm mostly worried about case1 for now, also in case 5 i found some code to do what is needed for that case which is displaying the largest number, but i just have to re-write it for my program.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
string case1();
int case2();
int bubbleSort();
int case4();
int case5();
int case6();
void exit();
int main()
{
char repeatMenu = 0;
string fileName = "";
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 << endl;
switch (menuChoice)
{
case 1: fileName = case1();
break;
case 2: fileName = case2();
break;
case 3: fileName = bubbleSort();
break;
case 4: fileName = case4();
break;
case 5: fileName = case5();
break;
case 6: fileName = case6();
break;
case 7: exit();
break;
}
cout << endl;
cout << "Would you like to return to the main menu ? [Y/N]: ";
cin >> repeatMenu;
} while (repeatMenu == 'Y' || repeatMenu == 'y');
("pause");
return 0;
}
string case1()
{
string fileName;
fstream fileName1;
cout << "Please select the name of the file you would like to open: ";
cin >> fileName;
fileName = fileName + ".txt";
fileName1.open(fileName, ios::in);
if (fileName1.fail())
{
cout << "The file you selected does not exist.\n";
cout << "Creating file named the filename you selected.\n\n";
fileName1.open(fileName);
}
else
{
cout << "The filename you selected exists and is opened.\n";
}
return fileName;
}
int case2()
{
cout << "Display all the numbers, total and average";
cout << " in the selected filename";
cout << endl << endl;
return 0;
}
int bubbleSort()
{
cout << "Create a bubble sort and display ";
cout << "all the numbers sorted.\n\n";
return 0;
}
int case4()
{
cout << "Sort array and use binary search";
return 0;
}
int case5()
{
//int numbers[];
//int count;
//int highest;
//highest = numbers[0];
//for (count = 1; count < SIZE; count++)
{
// if (numbers[count] > highest)
// highest = numbers[count];
}
system("pause");
return 0;
}
int case6()
{
cout << "Append a random number and open the file using ios::app";
cout << endl << endl;
return 0;
}
void exit()
{
cout << "Bye, have a great day!";
cout << endl << endl;
system("pause");
exit(0);
return;
}
When i put that in case 1 and compile the program and call the case1 function by selecting 1 after input a file name it gives me a invalid null pointer error, even if the file name exists and makes me abort.