Need help - various issues

New to the forums, registered because I am taking my first C++ class in college and am having lots of issues with this program i need to write.

Issue #1 - The displayed largest and smallest values are incorrect so i know the code must be wrong

Issue #2 - My do while loop is not ending

Issue #3 - When an invalid value is imputed, it doesnt display that cout message saying its invalid. The program just keeps going with the loop.

Any help would be greatly appreciated as I have been banging my head over this for awhile now.

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()
{
	ifstream inputfile;
	int integer;
	int numberOfintegers = 0;
	double sumOfintegers = 0;
	double averageOfintegers = 0;
	double largestValue = 0;
	double smallestValue = 9999999999;
	string filename;
	char choice;

	cout << "Enter the filename: ";
	cin >> filename;

	inputfile.open(filename);

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

	inputfile >> integer;
	while (inputfile)
	{
		numberOfintegers++;
		sumOfintegers = sumOfintegers + integer;
		inputfile >> integer;
	}

	if (numberOfintegers > 0)
		averageOfintegers = sumOfintegers / numberOfintegers;

	if (integer > largestValue)
		largestValue = integer;

	if (integer < smallestValue)
	{
		smallestValue = integer;
	}

	do
	{
		cout << endl << "Make a selection from the list " << 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 << endl;
		cout << "Enter your choice --> ";
		cin >> choice;

		switch (choice)
		{
		case 'a':
		case 'A': cout << endl; cout << largestValue; cout << endl;
			break;
		case 'b':
		case 'B': cout << endl; cout << smallestValue; cout << endl;
			break;
		case 'c':
		case 'C': cout << endl; cout << sumOfintegers; cout << endl;
			break;
		case 'd':
		case 'D': cout << endl; cout << averageOfintegers; cout << endl;
			break;
		case 'e':
		case 'E': cout << endl; cout << numberOfintegers; cout << endl;
			break;
		case 'f':
		case 'F': cout << endl; cout << "Program is ending "; cout << endl;
			break;
		defaut: cout << "Invalid choice"; cout << endl;
		}
	} while (choice != 'f' || choice != 'F');


	inputfile.close();

	cout << "Press Enter to end --> ";
	cin.ignore();
	cin.get();

	return 0;

}
Last edited on
Issue #2: Look at your condition - the while loop can only end when both parts are false, which means that choice has to be both 'f' and 'F' at the same time, which can never happen. You probably meant to use && instead of ||.

Issue #3: You misspelled default on line 81.
1) Only lines 32-36 are inside the loop. Following condition executed only once and operates on latest integer read.

2) It is bette to loop on input operation: while (inputfile >> integer)

3) Your choice variable is always either not F or not f (as it cannot be both at the same time), therefore condition is always true. Use && instead of || here.

4) defaut: cout << "Invalid choice"; cout << endl; Find the misprint.
Issue #1, take a look at the while loop on lines 31-36:
1
2
3
4
5
6
while (inputfile) // While I am reading the file
{
	numberOfintegers++; // I am counting the number of integers
	sumOfintegers = sumOfintegers + integer; // I am adding the integers as I loop.
	inputfile >> integer; // I am inputing the next integer.
}  // This bracket tells me to not execute additional duties besides the 3 above. 


You should move the closing bracket from line 36 to line 48:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
while (inputfile) // While I am reading the file
{
	numberOfintegers++; // I am counting the number of integers
	sumOfintegers = sumOfintegers + integer; // I am adding the integers as I loop.
	inputfile >> integer; // I am inputing the next integer.

	if (numberOfintegers > 0)
		averageOfintegers = sumOfintegers / numberOfintegers;

	if (integer > largestValue)
		largestValue = integer;

	if (integer < smallestValue)
	{
		smallestValue = integer;
	}

}// This bracket tells me to not execute additional duties besides the operations above.

if (numberOfintegers > 0)
		averageOfintegers = sumOfintegers / numberOfintegers;


I hope it helps. Good luck!

Edited: MiiNiPaa brought up a good point about the average calculation
Last edited on
Calculation of average should be left out of the loop though:
1
2
3
4
5
6
7
8
9
10
11
while (inputfile>> integer) {
    ++numberOfintegers;
    sumOfintegers += integer;

    if (integer > largestValue)
        largestValue = integer;
    if (integer < smallestValue)
        smallestValue = integer;
}
if (numberOfintegers > 0)
    averageOfintegers = sumOfintegers / numberOfintegers;
Thanks everyone for all the help i really appreciate it.
Topic archived. No new replies allowed.