Hello everyone I really need help with this menu :( I have been really struggling with it for the past few days now and cant figure out what I did wrong and what I need to do to press on.
What I need to do is a program that displays 4 options, then when the number is entered that corresponds with the option it does what it is supposed to do. My full code is pasted here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int select
cout << "Select an Option: " << endl;
cout << "1. Add Name to File." << endl;
cout << "2. Display Names on File." << endl;
cout << "3. Clear the File." << endl;
cout << "4. Exit the Program." << endl;
cout << "Selection: ";
cin >> select;
if (select == 1); // Write names to file Names.txt
{
ofstream outputFile;
string name;
outputFile.open("names.txt");
cout << endl;
cout << endl;
cout << "Adding a Name to File." << endl;
cout << "Enter a Name: ";
cin >> name;
outputFile << name << endl;
cout << "Name added successfully!" << endl;
outputFile.close();
cout << "Done." << endl;
}
if (select == 2); // Read names from the file Names.txt
{
ifstream outputFile;
string name;
inputFile.open("names.txt");
cout << "Names from the file names.txt: ";
inputfile >> name;
cout << name << endl;
inputFile.close();
}
if (select == 3); //Clear the file Names.txt
{
}
if (select == 4); //Close the program
{
cout << "Exiting Now..." << endl;
}
}
|
Now onto what I need help with if you haven't figured it out
Option 1 works ok and I am able to write a name to the file, but I cannot add more than one name to the file. When I enter a new name it overwrites the name that is already there. I need to want to put multiple names on the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
if (select == 1); // Write names to file Names.txt
{
ofstream outputFile;
string name;
outputFile.open("names.txt");
cout << endl;
cout << endl;
cout << "Adding a Name to File." << endl;
cout << "Enter a Name: ";
cin >> name;
outputFile << name << endl;
cout << "Name added successfully!" << endl;
outputFile.close();
cout << "Done." << endl;
}
|
Also with this section I would like to have the option to go back the the 4 options (maybe press B to go back to main menu?) and if I choose option 1 again I can add more names to the file Names.txt
My next question is with option 2. When at the main menu I cannot select #2 it just goes instantly into option 1 and IDK why! Without that working properly I cannot move onto the next 2 options.
For option 3 I want it to erase all the names on the file and say "Names have been removed" I cannot figure that out at the moment either since I literally do not know what the code is to clear a file.
And for option for I just want it to give you the option to press 4 to exit and it displays the message "Exiting now..."
Again any help is appreciated I have tried to do as much as I could on my own but alas I have been un-successful