file input and output for a menu

Hello all, I've run into another issue with my classwork and this one has thrown me through a loop. The objective is to have a menu that displays 10 different types of coding languages and a quit, then, when whichever language is selected gets written to a file, and then the menu runs again. Until you choose to quit, where you are supposed to then read the same file that you were writing to, and print that out on the screen. I am still new to c++ and very new to files, so this question might be easier than it looks, but if I could get a little help on this one that'd be greatly appreciated. I have attached what I have done so far, sometimes it writes it, sometimes it reads it, but it never works perfectly. The main step I've gotten hung up on is the printing of the file part, but my input loop doesn't always work either :/. Thanks for the info in advance.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
int menuOfPrograms();
int printPrograms();


//consts for menu
const int CPLUS_CHOICE = 1;
const int C_CHOICE = 2;
const int JAVA_CHOICE = 3;
const int RUBY_CHOICE = 4;
const int PHP_CHOICE = 5;
const int VISUAL_CHOICE = 6;
const int FORTRAN_CHOICE = 7;
const int CSHARP_CHOICE = 8;
const int SCRIPT_CHOICE = 9;
const int ASSEMBLER_CHOICE = 10;
const int QUIT2_CHOICE = 11;

void
files()
{
  fstream myOstream;
  myOstream.open("myPrograms.txt", std::ios_base::app);

  if (!myOstream)
  {
    cout << "This file has a problem :(" << endl;
  }
  else
  {
    // A
    bool menuFlag = true;

    while(menuFlag)
    {
        int choice = menuOfPrograms();
        switch (choice)
        {
            case CPLUS_CHOICE:
            cout << "C++ was the choice\n";
            myOstream << "C++\n";
            break;

            case C_CHOICE:
            cout << "C was the choice\n";
            myOstream << "C\n";
            break;

            case JAVA_CHOICE:
            cout << "Java was the choice\n";
            myOstream << "Java\n";
            break;

            case RUBY_CHOICE:
            cout << "Ruby was the choice\n";
            myOstream << "Ruby\n";
            break;

            case PHP_CHOICE:
            cout << "PHP was the choice\n";
            myOstream << "PHP\n";
            break;

            case VISUAL_CHOICE:
            cout << "Visual Basic was the choice\n";
            myOstream << "Visual Basic\n";
            break;

            case FORTRAN_CHOICE:
            cout << "Fortran was the choice\n";
            myOstream << "Fortran\n";
            break;

            case CSHARP_CHOICE:
            cout << "C# was the choice\n";
            myOstream << "C#\n";
            break;

            case SCRIPT_CHOICE:
            cout << "Java Script was the choice\n";
            myOstream << "Java Script\n";
            break;

            case ASSEMBLER_CHOICE:
            cout << "Assembler was the choice\n";
            myOstream << "Assembler\n";
            break;

            case QUIT2_CHOICE:
            cout << "Printing file contents\n";
            cout << fstream("myPrograms.txt").rdbuf();
            break;
      }
    }
  }
}


Also here is my function for the menu display
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
int menuOfPrograms() //function for menu display, and user choice validation
{
  int choice;
  bool loopFlag = true;
  string msg("Please choose a subject from the menu ");
    cout << "Please choose a subject from the menu \n\n";
	cout << "1. C++\n";
	cout << "2. C\n";
	cout << "3. Java\n";
	cout << "4. Ruby\n";
	cout << "5. PHP\n";
	cout << "6. Visual Basic\n";
	cout << "7. Fortran\n";
	cout << "8. C#\n";
	cout << "9. Java Script\n";
	cout << "10. Assembler\n";
	cout << "11. Quit\n";

	while (loopFlag){
		cout << "Enter your choice (1-11): ";
		cin >> choice;
		loopFlag = validateInput(choice, 1, 11, msg);
	}
	return choice;
}
What I can see from your code is that menuFlag is never changed, hence you have an infinite loop on line 33.

sometimes it writes it, sometimes it reads it, but it never works perfectly.
That's very vague. You did not provide enough code to reproduce the problem.

The first thing you need to figure out is what exactly causes a problem. I.e. what input leads to a wrong output.
ie use the debugger and trace through the code watching the contents of the variables until the observed behaviour is not as expected. Then you have a bug. Repeat until the program works as expected.
dorito200 wrote:

Hello all, I've run into another issue with my classwork and this one has thrown me through a loop. The objective is to have a menu that displays 10 different types of coding languages and a quit, then, when whichever language is selected gets written to a file, and then the menu runs again. Until you choose to quit, where you are supposed to then read the same file that you were writing to, and print that out on the screen. I am still new to c++ and very new to files, so this question might be easier than it looks, but if I could get a little help on this one that'd be greatly appreciated. I have attached what I have done so far, sometimes it writes it, sometimes it reads it, but it never works perfectly. The main step I've gotten hung up on is the printing of the file part, but my input loop doesn't always work either :/. Thanks for the info in advance.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
int menuOfPrograms();
int printPrograms();


//consts for menu
const int CPLUS_CHOICE = 1;
const int C_CHOICE = 2;
const int JAVA_CHOICE = 3;
const int RUBY_CHOICE = 4;
const int PHP_CHOICE = 5;
const int VISUAL_CHOICE = 6;
const int FORTRAN_CHOICE = 7;
const int CSHARP_CHOICE = 8;
const int SCRIPT_CHOICE = 9;
const int ASSEMBLER_CHOICE = 10;
const int QUIT2_CHOICE = 11;

void
files()
{
  fstream myOstream;
  myOstream.open("myPrograms.txt", std::ios_base::app);

  if (!myOstream)
  {
    cout << "This file has a problem :(" << endl;
  }
  else
  {
    // A
    bool menuFlag = true;

    while(menuFlag)
    {
        int choice = menuOfPrograms();
        switch (choice)
        {
            case CPLUS_CHOICE:
            cout << "C++ was the choice\n";
            myOstream << "C++\n";
            break;

            case C_CHOICE:
            cout << "C was the choice\n";
            myOstream << "C\n";
            break;

            case JAVA_CHOICE:
            cout << "Java was the choice\n";
            myOstream << "Java\n";
            break;

            case RUBY_CHOICE:
            cout << "Ruby was the choice\n";
            myOstream << "Ruby\n";
            break;

            case PHP_CHOICE:
            cout << "PHP was the choice\n";
            myOstream << "PHP\n";
            break;

            case VISUAL_CHOICE:
            cout << "Visual Basic was the choice\n";
            myOstream << "Visual Basic\n";
            break;

            case FORTRAN_CHOICE:
            cout << "Fortran was the choice\n";
            myOstream << "Fortran\n";
            break;

            case CSHARP_CHOICE:
            cout << "C# was the choice\n";
            myOstream << "C#\n";
            break;

            case SCRIPT_CHOICE:
            cout << "Java Script was the choice\n";
            myOstream << "Java Script\n";
            break;

            case ASSEMBLER_CHOICE:
            cout << "Assembler was the choice\n";
            myOstream << "Assembler\n";
            break;

            case QUIT2_CHOICE:
            cout << "Printing file contents\n";
            cout << fstream("myPrograms.txt").rdbuf();
            break;
      }
    }
  }
}


Also here is my function for the menu display

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
int menuOfPrograms() //function for menu display, and user choice validation
{
  int choice;
  bool loopFlag = true;
  string msg("Please choose a subject from the menu ");
    cout << "Please choose a subject from the menu \n\n";
	cout << "1. C++\n";
	cout << "2. C\n";
	cout << "3. Java\n";
	cout << "4. Ruby\n";
	cout << "5. PHP\n";
	cout << "6. Visual Basic\n";
	cout << "7. Fortran\n";
	cout << "8. C#\n";
	cout << "9. Java Script\n";
	cout << "10. Assembler\n";
	cout << "11. Quit\n";

	while (loopFlag){
		cout << "Enter your choice (1-11): ";
		cin >> choice;
		loopFlag = validateInput(choice, 1, 11, msg);
	}
	return choice;
}



1) In files(), you never change the value of menuFlag, so your loop will never exit.

2) You should close your file for writing, before opening a new stream on it to read it.
Last edited on
Topic archived. No new replies allowed.