Reading from a File ; HIGHEST #?

Hello! In this program, I am allowing the user to input grades that will be written to a file. Afterward, I need to read from that file and determine the highest and lowest grades (as well as an average and so forth). I have been stuck on how to do this. I am completely unaware on how to set up a loop to read from the file and determine which number from the file is the highest or lowest. Can anyone help? Thanks!

P.S. I cannot use vectors or arrays!

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

using namespace std;

int main()
{
	int userInput, userGrade, count = 1, userIn;

	ofstream outFile;

	outFile.open("data.txt");

	cout << "How many scores would you like to read? : ";
	cin >> userInput;

	cout << "Enter your grade for assignment 1: ";
	cin >> userGrade;
	outFile << "assignment 1 ";
	outFile << userGrade;
	outFile << endl;

	
	for (count = 2; count <= userInput; count++)
	{

		if (userGrade < 0 || userGrade > 100)
		{
			cout << "Invalid score. Please enter again: ";
			cin >> userGrade;
		}
		else
		{
			cout << "Enter your grade for assignment " << count << ": ";
			cin >> userGrade;
		}
	}

	for (count = 2; count <= userInput; count++)
	{
		if (userGrade < 0 || userGrade > 100)
		{
			outFile << "Invalid score. Please enter again: ";
			outFile << userGrade;
			outFile << endl;
		}
		else
		{
			outFile << "assignment " << count << " ";
			outFile << userGrade;
			outFile << endl;
		}
	}

	
	outFile.close();

	ifstream inFile;

	int base = 0;
	userIn = 0;
	int cnt = 0;

	inFile.open("data.txt");

	
	
	cout << "The highest number is: " << base << endl;

	inFile.close();
	return 0;
}
Is this the first time that your class has "compute sum/min/max"?
What have you done with loops before?
We've only just went over loops over the past week or so. We aren't generally using things that make it easier on us. What you see in my code is generally what we have learned so far, other than something I was planning on using , while (inFile >> num) in order to constantly read from a file.

What I was thinking about was using a while loop to read from the file and have variables for highest and lowest as well as a number placeholder and determine if one variable is higher than another and assign the higher variable to that. But Every time I tried it, it wouldn't work..
Show us a non-working version and explain the problems you see with it.

Hi! I also changed the code to be more efficient.

Here is the new 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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
	int userInput, userGrade, count = 1, userIn;

	ofstream outFile;

	outFile.open("data.txt");

	cout << "How many scores would you like to read? : ";
	cin >> userInput;

	while (count <= userInput)
	{
		cout << "Enter your grade for assignment " << count << ": ";
		cin >> userGrade;
		outFile << "assignment " << count << " ";
		outFile << userGrade << endl;

		if (userGrade >= 0 && userGrade <= 100)
		{
			count++;
		}
	}


Here is what I am trying to do in order to find the Highest and lowest integers from the file. Note that I need to use getline in order to grab the information for the file, then grab the numbers from it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

	string whole;
	int number, highest, lowest;
	highest = 100;
	lowest = 0;

	inFile.open("data.txt");

	while (inFile >> whole)
	{
            //I am lost here. I forgot that I have to use a string
            // in order to grab the whole line, but I somehow
           // need to be able to grab just the integer to store into number
	}

	inFile.close();
Note: the homework started in earlier thread: http://www.cplusplus.com/forum/beginner/252397/
More than one thread on same problem (aka doubleposting) is not efficient.


If you do know for certain that each line of input contains exactly one word and two integers, then you can read in that much:
1
2
3
4
5
6
7
string word;
int id;
int value;
while ( inFile >> word >> id >> value )
{
  // use the data
}


An another option is to use stringstream:
1
2
3
4
5
6
7
8
9
10
11
12
string line;
while ( std::getline( inFile, line ) )
{
  std::istringstream iss( line );
  string word;
  int id;
  int value;
  if ( iss >> word >> id >> value )
  {
    // use the data
  }
}



PS. What is the point of having word "assignment" on every line of the file? It looks uselessly redundant.
Hello! Sorry for Double Posting.

Also, the word "assignment" is required for this assignment.

I had no idea that you could read like that (in the first example). Thank you for that. That will be helpful in the future.

I also just figured out how to output the highest and lowest. You got my brain going.

Thank you very much. You have been extremely helpful.
Last edited on
Topic archived. No new replies allowed.