Having an issue with program.

So the question for the program is:
Make a program that opens "random.txt" and have it pick the largest number, the smallest number, give the amount of numbers in the file, give the sum of all the numbers in the file, and give the average of the numbers in the file. There is supposed to be a menu-driven command.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void showChoices();
int large(ifstream& in_stream);
int small(ifstream& in_stream);

int main()
{
	ifstream inputfile;

	int integer;
	int large;
	int small;
	int numberOfIntegers = 0;
	double sumOfIntegers = 0;
	double averageOfIntegers = 0;
	ifstream inp;
	int menuChoice;
	inputfile.open("random.txt");
	

	if(!inputfile)
	{
		cout << "Can't open the file random.txt" << endl;
		system("pause");
		return 0; 
	}

	void showChoices();
	{
		cout << "MENU" << endl;
		cout << "A. Get the largest value " << endl;
		cout << "B. Get the smallest value " << endl;
		cout << "C. Get the sum of the values " << endl;
		cout << "D. Get the average " << endl;
		cout << "E. Get the number of values entered " << endl;
		cout << "F. End this program " << endl;
	}

		switch (menuChoice)
		{
		case 'A': cout << "Get the largest value"; 
			cout << "The largest value is " << large << endl;
			break;
		case 'B': cout << "Get the smallest value";
			cout << "The smallest value is " << small << endl;
			break;
		case 'C': cout << "Get the sum of the values";
			cout << "The sum of the values entered is " << sumOfIntegers << endl;
			break;
		case 'D': cout << "Get the average";
			cout << "The average of the values entered is " << averageOfIntegers << endl;
			break;
		case 'E': cout << "Get the number of values entered";
			cout << "The number of values entered is " << numberOfIntegers << endl;
		case 'F': cout << "End the program";
			break;
		default:
			cout << "Invalid input" << endl;
		}
	
	while (menuChoice != 'A');
	{
		int large();
			{
			   int current, max;
			   ifstream in_stream;
	 
			   in_stream >> current;
			   max = current;
	   while(in_stream >> current)
			  {
		      in_stream >> current;
		      if(current > max)
		      max = current;
			  }
	   return max;
			}
	}

	while (menuChoice != 'B');
	{
		int small();
			{
			   int current, low;
			   ifstream in_stream;
	 
			   in_stream >> current;
			   low = current;
	   while(in_stream >> current)
			   {
			      in_stream >> current;
			      if(current > low)
			      low = current;
			   }
	   return low;
			}
	}

	while (menuChoice != 'C');
	{
		sumOfIntegers = sumOfIntegers + integer;
		inputfile >> integer;
	}

	while (menuChoice != 'D');
	{
		averageOfIntegers = sumOfIntegers / numberOfIntegers;
		inputfile >> integer;
	}

	while (menuChoice != 'E');
	{
		numberOfIntegers++;
		inputfile >> integer;
	}

	while (menuChoice != 'F');
	return 0;

}


Output:
Make a selection from the list
A. Get the largest value
B. Get the smallest value
C. Get the sum of the values
D. Get the average
E. Get the number of values entered
F. End this program

Enter your choice --> (A, B, C, D, E, or F)

Obviously I am doing something wrong because when i try to run the program, i get an error message saying "Debug Error: the variable 'menuChoice' is being used without being intialized". Can someone please help me.
I can't understand what kind of a retarded compiler allows you to define a function inside a function.
Also,
you are declaring menuChoice as an INT but in all comparisions, it is being compared to a CHAR.
Change the type of menuChoice to char. Like this: char menuChoice = '';
The second bold part is the cause of your "error"
PS: I'd like to know the name of the compiler you are using.
Last edited on
you have a lot of issues. that error message is just a warning btw.

you need cin >> menuChoice; somewhere in your code.
since menuChoice is int you won't get a character. change it to char menuChoice;

This and all other according while will loop infinite:
while (menuChoice != 'A'); // In this loop only ; will be executed

on line 31 you declare a function showChoices like you did on line 5.
you cannot define a function within another function. what you mean is probably this:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void showChoices();
int large(ifstream& in_stream);
int small(ifstream& in_stream);

int main()
{
	ifstream inputfile;

	int integer;
	int large;
	int small;
	int numberOfIntegers = 0;
	double sumOfIntegers = 0;
	double averageOfIntegers = 0;
	ifstream inp;
	int menuChoice;
	inputfile.open("random.txt");
	

	if(!inputfile)
	{
		cout << "Can't open the file random.txt" << endl;
		system("pause");
		return 0; 
	}

	showChoices(); // This calls the function showChoices. Note: NO preceding type

		cin >> menuChoice; // You need something like this here
		switch (menuChoice)
		{
		case 'A': cout << "Get the largest value"; 
			cout << "The largest value is " << large << endl; // large has NO valid data here / uninitialized
			break;
		case 'B': cout << "Get the smallest value";
			cout << "The smallest value is " << small << endl; // same for small
			break;
		case 'C': cout << "Get the sum of the values";
			cout << "The sum of the values entered is " << sumOfIntegers << endl; // and so forth
			break;
		case 'D': cout << "Get the average";
			cout << "The average of the values entered is " << averageOfIntegers << endl;
			break;
		case 'E': cout << "Get the number of values entered";
			cout << "The number of values entered is " << numberOfIntegers << endl;
		case 'F': cout << "End the program";
			break;
		default:
			cout << "Invalid input" << endl;
		}
	
	while (menuChoice != 'A'); // Due to the ; the while loop ends here and is infinite
	{ // NOT part of the loop
		int large(); // what are you trying to do here? you declare a function large
			{
			   int current, max;
			   ifstream in_stream;
	 
			   in_stream >> current;
			   max = current;
	   while(in_stream >> current)
			  {
		      in_stream >> current; // ?
		      if(current > max)
		      max = current;
			  }
	   return max;
			}
	}

	while (menuChoice != 'B');
	{
		int small();
			{
			   int current, low;
			   ifstream in_stream;
	 
			   in_stream >> current;
			   low = current;
	   while(in_stream >> current)
			   {
			      in_stream >> current;
			      if(current > low)
			      low = current;
			   }
	   return low;
			}
	}

	while (menuChoice != 'C');
	{
		sumOfIntegers = sumOfIntegers + integer;
		inputfile >> integer;
	}

	while (menuChoice != 'D');
	{
		averageOfIntegers = sumOfIntegers / numberOfIntegers;
		inputfile >> integer;
	}

	while (menuChoice != 'E');
	{
		numberOfIntegers++;
		inputfile >> integer;
	}

	while (menuChoice != 'F');
	return 0;

}

	void showChoices() // this defines a function outside the function main
	{
		cout << "MENU" << endl;
		cout << "A. Get the largest value " << endl;
		cout << "B. Get the smallest value " << endl;
		cout << "C. Get the sum of the values " << endl;
		cout << "D. Get the average " << endl;
		cout << "E. Get the number of values entered " << endl;
		cout << "F. End this program " << endl;
	}
I can't understand what kind of a retarded compiler allows you to define a function inside a function.

If you look a bit more carefully at the code, you'll see the OP is not defining a function inside a function:

1
2
3
4
5
6
7
8
9
10
	void showChoices();
	{
		cout << "MENU" << endl;
		cout << "A. Get the largest value " << endl;
		cout << "B. Get the smallest value " << endl;
		cout << "C. Get the sum of the values " << endl;
		cout << "D. Get the average " << endl;
		cout << "E. Get the number of values entered " << endl;
		cout << "F. End this program " << endl;
	}


The semicolon at the end of the first line of that snippet gives it the syntax of a declaration, not a definition. It's legal - if a bit odd - to declare a function inside another function (but not to define one), so the code is legal C++.

It was almost certainly intended to be a function definition, and if the OP had gotten the syntax correct for a definition then it would be illegal, paradoxically :D

To the OP: You've also put a semicolon at the end of every while statement, before you open the block. This will also cause very different behaviour from what you doubtless intended. It means that the compiler will consider ; to be a single, empty statement, and will therefore consider it to be the entire contents of the loop. When you write:

1
2
3
4
5
	while (menuChoice != 'E');
	{
		numberOfIntegers++;
		inputfile >> integer;
	}


it's equivalent to:

1
2
3
4
5
6
7
8
9
10
	while (menuChoice != 'E')
	{
		// This bit loops
		;
	} // End of the loop
	{
		// This bit does not loop - it executes after the loop has finished.
		numberOfIntegers++;
		inputfile >> integer;
	}

Even if you'd gotten the syntax correct:

1
2
3
4
5
	while (menuChoice != 'E')
	{
		numberOfIntegers++;
		inputfile >> integer;
	}


your loop would run forever once entered. Nothing in that loop changes the value of menuChoice, so that if (menuChoice != 'E') was true at the start of the loop, it would remain true forever, and the loop would run infinitely.
Last edited on
Topic archived. No new replies allowed.