Simple first program - Logical Issues?

This is my first program attempt in C++. I'm having some logical issues in a program I wanted to made to test my knowledge on C++ so far after reading up through chapter 3 of "Thinking in C++" - by Bruce Eckel.

It's suppose to be a word processor - I broke up different tasks into functions so that I could have the menu continue to display menu options after each function and only quit the program when I press c.

However, the program's menu function is acting odd - repeating twice some times before it performs the wanted function and when I try to select a new document for the second time, after I already have created a new document and saved it, it just exits.

Can somebody help me see my logical mistakes?


Code:
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Menu.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

int menu() {
  bool quit = false;  // Flag for quitting
  		cout << "Select an option below:" << endl;
		cout << endl;
  while(quit == false) {
	  cout << endl;
		cout << "a.) Create new text document" << endl;
		cout << "b.) View an existing text document" << endl;
		cout << "c.) Exit" << endl;
		cout << endl;
		cout << endl;
		char option;
		cin >> option;
		cout << endl;
    switch(option) {
      case 'a' : return 1;
		         quit = true;
                 break;
      case 'b' : return 2;
		         quit = true;
                 break;
      case 'c' : return 3;
                 quit = true;
                 break;
      default  : cout << "Please use a,b,or c!"
                 << endl;
    }
  }
} ///:~



void newFile() {
	// Create user .txt file and populate with user input
	string userFilename;
	cout << "Please name your new text document." << endl;
	cin >> userFilename;
	cout << endl;
	cout << endl;

	// Create file and accept user input
	ofstream myfile;
	userFilename += ".txt";
	myfile.open (userFilename.c_str());
	string input;
	cout << "Please enter the content to be saved into file." << endl;
	cout << "To save & close file, enter (s)ave." << endl;
	cout << "This program can accept multiple lines." << endl;
	cout << " " << endl;
	while (input != "s") {
		getline (cin, input);
		myfile << input << "\n";
	}
	myfile.close();
	cout << " " << endl;
	
	cout << "Your file has been saved." << endl;
	cout << "What would you like to do now?" << endl;
	cout << " " << endl;
	cout << " " << endl;
}



void viewFile() {
	vector<string> v;
	string userFilename;
	cout << "Enter the filename of the file you want to view: ";
	cin >> userFilename;
	userFilename += ".txt";
	cout << endl;
	cout << endl;
	ifstream in(userFilename.c_str());
	string line;
	cout << "Displaying file: "+ userFilename +"." << endl;
	cout << endl;
	while(getline(in, line)) {
		v.push_back(line);
	}
	for(int i = 0; i < v.size(); i++)
		cout << v[i] << endl;
}





int main() {
	cout << "Rocco Text Processor v1.0" << endl;
	cout << " " << endl;
	cout << " " << endl;
	if (menu() == 1)
		newFile();
	if (menu() == 2)
		viewFile();
	if (menu() == 3)
		cout << "Exiting" << endl;
	
	menu(); // Run menu
}

Well without running it, the first thing that jumps out at me is your main function. You are checking menu() returns before you even show the menu.

Should be:

1
2
3
4
menu(); // run menu
if(menu() == 1)
    newFile();
// rest of if statements 


Also might want to consider making it so the choice is put in a variable and use that to check against rather then the functions return value directly.
1
2
3
4
5
 int choice = menu();
// then do something like 
if(choice == 1)
   newFile();
// etc 
Last edited on by closed account z6A9GNh0
@BHXSpector how could you miss that each time an if statement is evaluated it calls menu() again? ;)

@OP you need to call menu() ONCE at the start of the program and store its return value in a variable, then sue that variable in the if statements. Otherwise each if statement calls the menu() again.
Last edited on
How do I get the return value of menu() into a variable?
Last edited on
ok nevermind about that last quesiton -

1
2
3
4
5
6
7
	int selection = menu(); // run menu
	if (selection==1)
		newFile();
	if (selection==2)
		viewFile();
	if (selection==3)
		cout << "Exiting" << endl;


however, I still don't have the functionality I need. It's completing the function I want but exiting directly after and not re-displaying the menu

Maybe i should throw that entire statement above into a while loop, testing for the exit menu item?
Last edited on

1
2
3
4
5
6
7
8
9
10
11
12
13
    switch(option) {
      case 'a' : return 1;   //this exits your function
		         quit = true;  //this can never execute 
                 break;
      case 'b' : return 2;   //same
		         quit = true; //same
                 break;
      case 'c' : return 3;    //...
                 quit = true;    //..
                 break;
      default  : cout << "Please use a,b,or c!"
                 << endl;
    }



Why don't you just do what your doing in main strait from the menu?

1
2
3
4
5
6
7
8
9
10
    switch(option) {
      case 'a' : newFile();
                 break;
      case 'b' : viewFile();
                 break;
      case 'c' : cout << "Exiting" << endl;
                 quit = true; 
                 break;
      default  : cout << "Please use a,b,or c!"<< endl;
    }



1
2
3
4
int main() {
	cout << "Rocco Text Processor v1.0" << endl;
	menu(); // Run menu
}
Last edited on
@iseeplusplus - This works! I failed to circle back to this reasoning for some reason. The program works great! Thanks for all your help.


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
96
97
98
99
100
101
102
103
104
105
106
// Menu.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}


void newFile() {
	// Create user .txt file and populate with user input
	string userFilename;
	cout << "Please name your new text document." << endl;
	cin >> userFilename;
	cout << endl;
	cout << endl;

	// Create file and accept user input
	ofstream myfile;
	userFilename += ".txt";
	myfile.open (userFilename.c_str());
	string input;
	cout << "Please enter the content to be saved into file." << endl;
	cout << "To save & close file, enter (s)ave." << endl;
	cout << "This program can accept multiple lines." << endl;
	cout << " " << endl;
	while (input != "s") {
		getline (cin, input);
		myfile << input << "\n";
	}
	myfile.close();
	cout << " " << endl;
	
	cout << "Your file has been saved." << endl;
	cout << "What would you like to do now?" << endl;
	cout << " " << endl;
	cout << " " << endl;
}



void viewFile() {
	vector<string> v;
	string userFilename;
	cout << "Enter the filename of the file you want to view: ";
	cin >> userFilename;
	userFilename += ".txt";
	cout << endl;
	cout << endl;
	ifstream in(userFilename.c_str());
	string line;
	cout << "Displaying file: "+ userFilename +"." << endl;
	cout << endl;
	while(getline(in, line)) {
		v.push_back(line);
	}
	for(int i = 0; i < v.size(); i++)
		cout << v[i] << endl;
}




void menu() {
  bool quit = false;  // Flag for quitting
  		cout << "Select an option below:" << endl;
		cout << endl;
  while(quit == false) {
	  cout << endl;
		cout << "a.) Create new text document" << endl;
		cout << "b.) View an existing text document" << endl;
		cout << "c.) Exit" << endl;
		cout << endl;
		cout << endl;
		char option;
		cin >> option;
		cout << endl;
    switch(option) {
      case 'a' : newFile();
                 break;
      case 'b' : viewFile();
                 break;
      case 'c' : cout << "Exiting" << endl;
                 quit = true;
                 break;
      default  : cout << "Please use a,b,or c!"
                 << endl;
    }
  }
} ///:~



int main() {
	cout << "Rocco Text Processor v1.0" << endl;
	cout << " " << endl;
	cout << " " << endl;
	menu();
}
Topic archived. No new replies allowed.