Writing final program for intro programming class - switch statement wont switch (among other things)

Write your question here.
Hey everyone. I have a final project due for my C++ class - the assignment reads as follows : Here is what the program needs to do:

Write a program which allows the user to read a list of numbers from a data file. If the user specifies a data file which does not currently exist, then the program will create it. Assume there will never be more than 50 numbers in the data file.
The program should present the user with a menu containing the following options:
Current data file being processed.
(1) Select / create data file (.txt file extension will be added automatically)
(2) Display all the numbers in the data file, along with their total and average
(3) Display all the numbers in the data file sorted from smallest to largest
(4) Search for a number in the data file and display how many times the number occurs in the file
(5) Display the largest number in the data file
(6) Append one or more random numbers to the data file
(7) Exit the program
Prompt the user to make a selection from the menu and use input validation to ensure the user makes a valid selection (1 - 7).
Use a switch statement to process each of the above options. Each case statement should call a function which carries out the required action.
When asking the user which data file they wish to process, ask the user to only enter the part of the filename preceding the .txt extension and automatically add the .txt extension for the user. Create an empty data file if the file does not already exist.
Options (2) - (6) should test whether a data file has been specified before calling the function. If a data file has not yet been specified, the option should not call the function and the display should be refreshed.
When a data file is first created it will be empty. The functions which process the data file will need to handle the special case when the file exists, but does not contain any numbers. To make the program easier to write, you may opt to ignore this special case and assume that the user will immediately add numbers to the file once it has been created.
When adding a number, append it to the end of the data file.


_____________________________________________________________________-

I am only trying to do parts 1 and 2 for now, I was struggling with passing a filename to a function as an argument but I think I may have mastered this, here is the code I have so far:


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
 
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int menu();
string createFile();
void displayNumTotalAverage(string);


int main(int choice, string FN)
{
	string FN;

	cout << "** MENU **" << endl << endl;
	cout << "Current Data File: " << FN << endl << endl;
	menu();
	displayNumTotalAverage(FN);

	if (choice == 1){
		cout << "Choice 1";
		createFile();
	}
	else 
	{
		if (choice == 2)
		{

			cout << "Choice 2: Display all numbers, total, and average";
			displayNumTotalAverage(FN);
		}

	else
	{
		if (choice == 3)
		{
	
		cout << "Choice 3";
	}

	else{

	if (choice == 4){
		cout << "Choice 4";
	}
	else
	{
		if (choice == 5){
			cout << "Choice 5";
		}
		else
		{
			if (choice == 6){
				cout << "Choice 6";
			}
			else
			{
				exit(1);
			}


			system("PAUSE");
			return 0;

		}

		int menu()
		{

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

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

			cout << "you have choosen option" << choice;


			system("PAUSE");
			return choice;
		}

		string createFile()
		{
			cout << "I AM THE CREATE / SELECT FILE OPTION" << endl;

			ifstream inFile;
			cout << "Name of data file: ";
			cin >> FN;
			inFile.open(FN + ".txt");

			if (inFile){
				cout << "File successfully found" << endl;
				return FN;
			}

			else
			{
				cout << "File not found, creating file.";
				inFile.open(FN);
			}
			return FN;

		}

		void displayNumTotalAverage(string FN)
		{
			ifstream inFile;
			int num;
			int total;
			cout << "In the displayNumTotalAverage function" << FN;
			double average;
			bool containsNum = false;
			inFile.open(FN + ".txt");
			if (inFile)
			{
				while (inFile >> num)
				{
					cout << num << endl;
				}
				inFile.close();
			}
			else
			{
				cout << "Error opening file" << FN << "." << endl;
			}
		}
	
LOL Oh yeah that mess of nested if/else statements HAD been a switch statement but I couldnt get that to work correctly so I was trying to get it to work with this....
This is another earlier version of the same program:::

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

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

//Function Prototypes
void menu();
void createFile();
void displayNumTotalAverage();
void displaySortedNums();
void searchNum();
void displayLargestNum();
void appendRandomNum();
void exit();




int main()
{
	menu();
	string myFileName;
	//createFile();
	//makeRandomNum();

	system("PAUSE");
	return 0;
}
void menu()
{
	int choice;
	do
	{
		cout << "** MENU **\n\n(1)  Select / create data file (.txt file extension will be added automatically)"
			<< "(2)  Display all numbers, total, and average\n(3)  Display all numbers sorted\n(4)  "
			<< "Search for a number and display how many times it occurs\n(5)  Display the largest number\n"
			<< "(6)  Append a random number(s)\n(7)  Exit the program\n\nMenu Choice:";
		cin >> choice;
		while (choice < 1 || choice > 7)
		{
			cout << "Menu Choice: ";
			cin >> choice;
		}
		switch (choice)
		{
		case 1:
			cout << "CHOICE 1";
			createFile();
			break;
		case 2:
			cout << "CHOICE 2";
			break;
		case 3:
			cout << "CHOICE 3";
			break;
		case 4:
			cout << "CHOICE 4";
			break;
		case 5:
			cout << "CHOICE 5";
			break;
		case 6:
			cout << "CHOICE 6";
			break;
		}

	} while (choice != 7);

}
		void createFile()
		{
			cout << "I AM IN THE CREATEFILE FUNCTION - Option 1" << endl;
			string myFileName;
			ifstream inFile;
			cout << "Name of data file: ";
			cin >> myFileName;
			inFile.open(myFileName + ".txt");
			if (inFile)
			{
				cout << myFileName;
			}
			else
				cout << "File not found, creating file.";

		system("PAUSE");
			return;
		}

		void displayNumTotalAverage()
		{
			cout << "I AM THE DISPLAYNUMTOTALAVERAGE - Option 2" << endl << endl << endl;
			ifstream inFile;
			int num;
			string myFileName;
			cout << "Enter File Name: ";
			cin >> myFileName;

			inFile.open(myfilename + ".txt");

			system("PAUSE");
			return;
		}


		void displaySortedNums()
		{
			cout << "I AM THE displaySortedNums Function - Option 3" << endl;
			system("PAUSE");
			return;
		}

		void searchNum()
		{
			cout << " I am the searchNum function - option 4" << endl;
			system("PAUSE");
			return;
		}

		void displayLargestNum()
		{
			cout << "I am the displayLargestNum Function - option 5" << endl;
			system("PAUSE");
			return;

		}

		void appendRandomNum()
		{
			cout << "i am in the appendRandomNum function - option 6" << endl;
			system("PAUSE");
			return;
		}

		void exit()
		{
			cout << " I am the exit function - option 7" << endl;
			system("PAUSE");
			return;
		}
	

_________________________________________________________________

SO yeah I have been considering changing my major but nothing good ever came from easy stuff so I guess I will preserver. Thanks in advance to anyone who is willing to help me. I have about 3 more weeks to get the assignment together!
Last edited on
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 <cstdlib>
#include <string>

using namespace std;

//Function Prototypes
void menu();
string createFile();
void displayNumTotalAverage(string);
void displaySortedNums();
void searchNum();
void displayLargestNum();
void appendRandomNum(string);
void exit();




int main()
{
	menu();
	string myFileName;
	//createFile();
	//makeRandomNum();

	system("PAUSE");
	return 0;
}
void menu()
{
	int choice;
	string myFileName;
	do
	{
		cout << "** MENU **\n\n(1)  Select / create data file (.txt file extension will be added automatically)"
			<< "(2)  Display all numbers, total, and average\n(3)  Display all numbers sorted\n(4)  "
			<< "Search for a number and display how many times it occurs\n(5)  Display the largest number\n"
			<< "(6)  Append a random number(s)\n(7)  Exit the program\n\nMenu Choice:";
		cin >> choice;
		while (choice < 1 || choice > 7)
		{
			cout << "Menu Choice: ";
			cin >> choice;
		}
		switch (choice)
		{
		case 1:
			cout << "CHOICE 1";
			createFile();
			break;
		case 2:
			cout << "CHOICE 2";
			displayNumTotalAverage(myFileName.c_str());
			break;
		case 3:
			cout << "CHOICE 3";
			break;
		case 4:
			cout << "CHOICE 4";
			break;
		case 5:
			cout << "CHOICE 5";
			break;
		case 6:
			cout << "CHOICE 6";
			appendRandomNum(myFileName.c_str());
			break;
		}

	} while (choice != 7);

}
string createFile()
{
	cout << "I AM IN THE CREATEFILE FUNCTION - Option 1" << endl;
	string myFileName;
	ifstream inFile;
	cout << "Name of data file: ";
	cin >> myFileName;
	myFileName = "C:\\Users\\Stacie\\Documents\\Notes\\Code" + myFileName;
	inFile.open(myFileName + ".txt");
	if (inFile)
	{
		cout << myFileName;
	}
	else
		cout << "File not found, creating file.";

	system("PAUSE");
	return myFileName;
}

void displayNumTotalAverage(string myFileName)
{
	ifstream inFile;
	cout << "I AM THE DISPLAYNUMTOTALAVERAGE - Option 2" << endl << endl << endl;
	inFile.open("C:\\Users\\Stacie\\Documents\\Notes\\Code" + myFileName + ".txt");
	int num;
	int total;
	cout << "In the displayNumTotalAverage function" << myFileName;
	double average;
	bool containsNum = false;
	inFile.open(myFileName + ".txt");
	if (inFile)
	{
		while (inFile >> num)
		{
			cout << num << endl;
		}
		inFile.close();
	}
	else
	{
		cout << "Error opening file" << myFileName << "." << endl;
	}

	system("PAUSE");
	return;
}


void displaySortedNums()
{
	cout << "I AM THE displaySortedNums Function - Option 3" << endl;
	system("PAUSE");
	return;
}

void searchNum()
{
	cout << " I am the searchNum function - option 4" << endl;
	system("PAUSE");
	return;
}

void displayLargestNum()
{
	cout << "I am the displayLargestNum Function - option 5" << endl;
	system("PAUSE");
	return;

}

void appendRandomNum(string myFileName)
{
	cout << "i am in the appendRandomNum function - option 6" << endl;
	int num = 0;
	int count = 0;
	ofstream outFile;
	outFile.open(myFileName + ".txt", ios::app);
	cout << "How many random numbers: ";
	cin >> count;
	for (int i = 0; i < count; i++)
		outFile << rand() % 10 << endl;
	outFile.close();
	cout << endl << "Number(s) Added" << endl << endl;

	system("PAUSE");
	return;
}

void exit()
{
	cout << " I am the exit function - option 7" << endl;
	system("PAUSE");
	return;
}


so this is the latest version...it sort of runs...a little.. PLEASE someone run it and tell me what I can do to fix the probelms!!! Thanks
Morning,
Only had a quick look, but i see this on line 34:
string myFileName;
and then if the user selects option 2 you call this:
displayNumTotalAverage(myFileName.c_str());

In other words you don't actually set a filename before passing into your displayNumTotalAverage() function.
(you also don't need to call c_str() either).

edit:
i see your createFile function returns the file name. perhaps you meant to do something like this on line 51:
myFileName = createFile();

which would set your file name BUT it still doesnt stop the user from selecting option 2 before option 1.
Last edited on
Topic archived. No new replies allowed.