Ok so my teacher wants me to create a menu driven program that allows the user to Create entries, Display entries, modify entries, and Delete entries. The thing is, i don't even know where to start. We went over this a bit in class and it was confusing. And when i went to look in the text book it really did not give me any idea on creating something like this. I am also fairly new to coding. Any help and examples would be greatly appreciated.
#include <iostream>
int main()
{
bool menuRunning = true;
while(menuRunning == true)
{
int userChoice;
std::cin >> userChoice;
switch(userChoice)
{
case 1:
// Create entry function here
break;
case 2:
// Modify entry function here
break;
case 3:
// View entry function here
break;
case 4:
// Exit the menu, or quit the program
menuRunning = false;
default:
std::cout << "Invalid choice" << std::endl;
}
}
return 0;
}
Ok, so this is what i currently have and everything is working fine. The file creates perfectly and i am on the right track. Now i need to be able to display any file created.
In order to read files, you must open the file for reading. I'll give you a hint and you have to research how to use it. It's similar to cout and cin.
1 2 3 4 5 6 7 8 9 10
ifstream infile; // Creates an object called infile to read files.
infile.open(filename.c_str()); // opens the filename
if(!infile) // if the file fails to open or doesn't exist...
{
cout << "unable to open file\n";
}
else // if it is able to open the file
{
// your code here
}
Okay so i used the layout and applied it to my code and bingo everything still works and it reads perfectly. Now onto the part where i can actually write to a created file. Feel like i should have did this before the display one but who cares it works!!! :D Thanks again for helping me.
Okay, so now i have this and i am stuck. When i run the program and try to edit a file it finds the file but then skips the getline for me to edit its contents. Problem at line 61. Any idea on what i am doing wrong?
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
int input;
char choice;
string filename;
ofstream f;
ifstream infile;
do{
int option;
cout << "***Menu***" << endl;
cout << "1.Create a File" << endl;
cout << "2.Display a File" << endl;
cout << "3.Edit a File" << endl;
cout << "4.Delete a File" << endl;
cin >> option;
switch (option)
{
case 1:
cout << "What would you like to name your file?" << endl;
cin >> filename;
f.open ( filename.c_str() );
f.close();
cout << "Successfully created: " << filename << endl;
break;
case 2:
cout << "What is the name of the file you would like to display?" << endl;
cin >> filename;
infile.open(filename.c_str());
if(!infile)
{
cout << "Unable to open file\n";
}
else
{
cout << "Reading file: " << filename << endl;
while (getline(infile,filename)){
cout << "This file says: " << filename << endl;
f.close();
}
}
break;
case 3:
cout << "What is the name of the file you would like to edit?" << endl;
cin >> filename;
f.open(filename.c_str());
if (!infile)
{
cout << "unable to open file\n";
}
else
{
cout << "Found File...Opening..." << endl;
cout << "What would you like to write into this file?" << endl;
getline (cin,filename);
f.close();
}
break;
case 4:
cout << "Deleted File " << endl;
break;
default:
cout << "invalid entry" << endl;
}
cout<<"Would you like to continue? (Y/N)" <<endl;
cin >> choice;
}while(choice == 'y' || choice == 'Y');
return 0;
}
ok so i kinda got it to work now, it puts text into the file but the thing is it can only put one word without any spaces. i tested it and it seems that if i put many words with spaces it just ends the program or it jumps into an unlimted loop.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
int input;
char choice, text[200];
string filename;
ofstream f;
ifstream infile;
do{
int option;
cout << "***Menu***" << endl;
cout << "1.Create a File" << endl;
cout << "2.Display a File" << endl;
cout << "3.Edit a File" << endl;
cout << "4.Delete a File" << endl;
cin >> option;
switch (option)
{
case 1:
cout << "What would you like to name your file?" << endl;
cin >> filename;
f.open ( filename.c_str() );
f.close();
cout << "Successfully created: " << filename << endl;
break;
case 2:
cout << "What is the name of the file you would like to display?" << endl;
cin >> filename;
infile.open(filename.c_str());
if(!infile)
{
cout << "Unable to open file\n";
}
else
{
cout << "Reading file: " << filename << endl;
while (getline(infile,filename)){
cout << "This file says: " << filename << endl;
f.close();
}
}
break;
case 3:
cout << "What is the name of the file you would like to edit?" << endl;
cin >> filename;
f.open(filename.c_str(),ios::out | ios::in );
if (!infile)
{
cout << "unable to open file\n";
}
else
{
cout << "Found File...Opening..." << endl;
cout << "What would you like to write into this file?" << endl;
cin >> text;
f << text << endl;
f.close();
}
break;
case 4:
cout << "Deleted File " << endl;
break;
default:
cout << "invalid entry" << endl;
}
cout<<"Would you like to continue? (Y/N)" <<endl;
cin >> choice;
}while(choice == 'y' || choice == 'Y');
return 0;
}
Now it does not compile, it says no matching function. and when i use getline it skips right over it in this one
1 2 3 4 5 6 7 8 9 10 11 12 13
cin >> filename;
f.open(filename.c_str(),ios::out | ios::in);
if (!infile)
{
cout << "unable to open file\n";
}
else
{
cout << "Found File...Opening..." << endl;
cout << "What would you like to write into this file?" << endl;
getline (cin,filename);
f.close();
}
wait wtf, i did what i saw this other guy do and it worked lol I put two getlines after one another and it works haha. i just tried this like 5 minutes ago and it didnt work hmmm so i gues my text DID have to be a string..wow
else
{
cout << "Found File...Opening..." << endl;
cout << "What would you like to write into this file?" << endl;
cin.ignore();
getline(cin, text);
f << text << endl;
f.close();
}
cout << "Found File...Opening..." << endl;
cout << "What would you like to write into this file?" << endl;
getline (cin,text);
getline (cin,text);
f << text << endl;
f.close();
yeah "text" was not a string with "cin.ignore()". I testing out cin.ignore to see if it had an effect....Nope. I am now on the final stretch of inserting a delete function...