This is due tomorrow, Please help.

Pages: 12
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.
Last edited on
This is an example of a menu driven program. In fact, it's one that I use a lot in my programming assignments.


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
#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 thanks, i got the basics down now. Now i need to know how do you allow the user to create a file and read it, etc.
In order to create and read files, you have to #include <fstream> . I assume you may already be familiar with the basics of that library.

If not, you may want to check this link

http://www.cplusplus.com/reference/fstream/fstream/
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.

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char choice;
    string filename;
    ofstream f;
    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();
      break;
      case 2:
      cout << "What is the name of the file you would like to display? " << endl;
      break;
      case 3:
      cout << "Editing File " << endl;
      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;
}
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
}
Last edited on
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.

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
61
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    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;
         }
     }
      break;
      case 3:
      cout << "Editing File" << endl;
      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;
}
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?

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <string>

using namespace 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;
}
When you edit the file, are you adding text to the end of the file or redefining all the contents of the text file? Where are you at so far?
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.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <string>

using namespace 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;
}
Look at your line 61. Instead of cin, you should be using getline, as getline allows you to get everything including whitespaces.

replace it with this
 
getline(f, text);

and see what happens next.
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();
      }
Last edited on
lol my bad, is should have read the code more.
first, change your char text[200] to a string type. it should look something like
string text;

EDITED:
then replace line 61 with
1
2
cin.ignore(); // need this here, or else getline will skip
getline(cin, text);
Last edited on
Yeah it still skips right over the getline
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
What do you have? I have this.

1
2
3
4
5
6
7
8
9
      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();
      }
This somehow works

1
2
3
4
5
6
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();
was your variable text not a string before with the cin.ignore()?
Last edited on
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...

Check second page
Last edited on
so what happens now if you replace the first getline with cin.ignore(). will it still skip?
Pages: 12