Menu oriented project problems

How come that when i compile this and run it through the loop after picking menu choice 1 and then Y for looping back to main menu that it doesn't display the current data file you're in ?

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

using namespace std;	

int main();
int case1(string & fileName);
int case2();

int main()
{
	string fileName;
	string fileName1;
	char repeatMenu = 0;

	do {

		system("cls");

		cout << "** MENU **" << endl << endl;

		cout << "Current Data File: " << fileName1;

		cout << endl << endl;

		int menuChoice;

		cout << "(1) Select / Create data file (.txt file extension will be added automatically)\n";
		cout << "(2) Display all numbers, total and average\n";
		cout << "(3) Display all numbers sorted\n";
		cout << "(4) Search for a number and display how many times it occurs\n";
		cout << "(5) Display the largest number\n";
		cout << "(6) Append a random number(s)\n";
		cout << "(7) Exit the program\n";

		cout << endl;

		cout << "Menu choice: ";
		cin >> menuChoice;

		if (menuChoice < 1 || menuChoice > 7)
		{
			do
			{
				cout << "Menu choice: ";
				cin >> menuChoice;
			} while (menuChoice < 1 || menuChoice > 7);
		}

		cout << endl;

		switch (menuChoice)

		{
		
		case 1:		case1(fileName);
			break;

		}

		cout << endl;
		cout << "Would you like to return to the main menu ? [Y/N]: ";
		cin >> repeatMenu;

	} while (repeatMenu == 'Y' || repeatMenu == 'y');

	system("pause");
	return 0;
}

int case1(string & fileName)
{
	fstream outFile;
	string fileName1;

	cout << "Name of data file: ";
	cin >> fileName;
	fileName1 = fileName + ".txt";
	cout << endl;

	outFile.open(fileName1.c_str());

	if (outFile.fail())
	{
		cout << "The file did not create/open successfully.\n";
	}
	else
	{
		cout << "The file has been created successfully and/or opened successfully.\n" << endl;
	}

	return 0;
}

I'm going to guess that you mean this line of code:
cout << "Current Data File: " << fileName1;

You never set this variable, fileName1, to anything. The variable fileName1 within the function case1 exists only within the function case1.

Last edited on
Ohh okay thank youu, i tried just fileName instead of fileName1 and it works. Also how would i write data into a file through user input ? , this doesn't seem to be it because I'm getting an operand error in the writeData == 'Y' if block.

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
int case1(string & fileName)
{

	string enterData;
	ifstream outFile;
	ofstream outFile1;
	string fileName1;
	int createReadOption;
	char writeData;

	cout << "If you would like to select a file, press (1).\n";
	cout << "If you would like to create a file, press (2).\n";
	cout << endl;
	cin >> createReadOption;
	cout << endl;

	if (createReadOption == 1)
	{
		cout << "Name of data file: ";
		cin >> fileName;
		fileName1 = fileName + ".txt";
		cout << endl;

		outFile.open(fileName1.c_str());

		if (outFile.fail())
		{
			cout << "The file did not open successfully.\n";
		}
		else
		{
			cout << "The file has been opened successfully.\n" << endl;
		}
	}

	else if (createReadOption == 2)
	{
		cout << "Select a name for your new data file: ";
		cin >> fileName;
		fileName1 = fileName + ".txt";
		cout << endl;

		outFile1.open(fileName1.c_str());

		if (outFile1.fail())
		{
			cout << "The file was not created successfully.\n";
		}
		else
		{
			cout << "The file has been created successfully.\n" << endl;
			cout << "Would you like to write data to the file now ? [Y/N]: " << endl;
			cin >> writeData;

			if (writeData == 'Y' || writeData == 'y')
			{
				cout << "Please type in the Data you would like to enter into the file: ";
				outFile1 >> enterData;

				
			}

			else
			{
				
			}
		}
	}

	else
	{
		cout << "Please select either one or two";
		cin >> createReadOption;
	}
	return 0;
}
outFile1 >> enterData;

This says "fetch some data from the file, and store it in the string named enterData". Is that what you meant to do, or did you mean to write data into the file?
Ooh i meant to write data, yea i just tried the less than operands and the error went away.

Actually something weird just happened.. when i run this code it compiled right and started but then it pop up an error saying " system. NullReferenceObjection " or something along those lines and would switch to something crazy like 2600 lines of code and say its in debugging mode.

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <fstream>
#include <string>

using namespace std;	

int main(string & fileName);
int case1(string & fileName);
int case2(string & fileName);
int case3();

int main(string & fileName)
{

	ifstream outFile;
	ofstream outFile1;
	string fileName1;
	char repeatMenu = 0;

	do {

		system("cls");

		cout << "** MENU **" << endl << endl;

		cout << "Current Data File: " << fileName;

		cout << endl << endl;

		int menuChoice;

		cout << "(1) Select / Create data file (.txt file extension will be added automatically)\n";
		cout << "(2) Display all numbers, total and average\n";
		cout << "(3) Display all numbers sorted\n";
		cout << "(4) Search for a number and display how many times it occurs\n";
		cout << "(5) Display the largest number\n";
		cout << "(6) Append a random number(s)\n";
		cout << "(7) Exit the program\n";

		cout << endl;

		cout << "Menu choice: ";
		cin >> menuChoice;

		if (menuChoice < 1 || menuChoice > 7)
		{
			do
			{
				cout << "Menu choice: ";
				cin >> menuChoice;
			} while (menuChoice < 1 || menuChoice > 7);
		}

		cout << endl;

		switch (menuChoice)

		{
		
		case 1:		case1(fileName);
			break;
		case 2:		case2(fileName);
			break;

		}

		cout << endl;
		cout << "Would you like to return to the main menu ? [Y/N]: ";
		cin >> repeatMenu;

	} while (repeatMenu == 'Y' || repeatMenu == 'y');

	system("pause");
	return 0;
}

int case1(string & fileName)
{

	string enterData;
	ifstream outFile;
	ofstream outFile1;
	string fileName1;
	int createReadOption;
	char writeData;

	cout << "If you would like to select a file, press (1).\n";
	cout << "If you would like to create a file, press (2).\n";
	cout << endl;
	cin >> createReadOption;
	cout << endl;

	if (createReadOption == 1)
	{
		cout << "Name of data file: ";
		cin >> fileName;
		fileName1 = fileName + ".txt";
		cout << endl;

		outFile.open(fileName1.c_str());

		if (outFile.fail())
		{
			cout << "The file did not open successfully.\n";
		}
		else
		{
			cout << "The file has been opened successfully.\n" << endl;
		}
	}

	else if (createReadOption == 2)
	{
		cout << "Select a name for your new data file: ";
		cin >> fileName;
		fileName1 = fileName + ".txt";
		cout << endl;

		outFile1.open(fileName1.c_str());

		if (outFile1.fail())
		{
			cout << "The file was not created successfully.\n";
		}
		else
		{
			cout << "The file has been created successfully.\n" << endl;
			cout << "Would you like to write data to the file now ? [Y/N]: " << endl;
			cin >> writeData;

			if (writeData == 'Y' || writeData == 'y')
			{
				cout << "Please type in the Data you would like to enter into the file: ";
				outFile1 << enterData;

				
			}

			else
			{
				
			}
		}
	}

	else
	{
		cout << "Please select either one or two";
		cin >> createReadOption;
	}
	return 0;
}

int case2(string & fileName)
{
	cout << "Hello\n\n";
// i took out the opening file here i used hello and goodbye to see if it worked
	cout << "\n\nGoodbye\n\n";

	return 0;
}

int case3()
{


	system("pause");
	return 0;
}
Last edited on
Bump i need a solution fast, this project is due in a few days and i can't find what's wrong.
Topic archived. No new replies allowed.