[Homework help]Help with Data sorting from file

Hey y'all, new to the forum and a tad more than halfway through my first C++ course. I am absolutely fried and stuck on a homework question, I have searched for the last 2 days and have been unable to wrap my head around what to do. Assume I know very little, and explanations of why things are supposed to work the way they do would be very helpful. The Homework problem:

1. Create 100 random 'test scores' between 60-100, write to a file(done).

2. Read that file, count the number of test scores from 60-69, 70-79, 80-89, 90-100.

3. Determine high, low average of all scores, Display summary results (number of scores in each range; high, low and average of all scores).

4. Save the results in a separate text file in the same formatting they are displayed on the screen.

I can't seem to wrap my head around how to find all instances of each number in a string. If someone could point me in the right direction of how to proceed, it would be greatly apprecieated.

-Kirby

rough 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
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
  //Project :   Assignment 12
//File    :   Assignment 12.cpp
//Name    :   Kirby Wood
//Date    :   03/27/14
//
// Description :  Write a program to simulate managing test scores.

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


using namespace std;

//Function prototypes
void countFunction();

int main()

{
    // Initialize and name program variables
    int row1, column1, count, grade_sums, value, randomnumbers, indexnum, variable, number;
    
    
    //Initialize arrays
    
    //initialize starting 2 dimensional array
    srand(time(NULL));
    
    
    string filename = "scores.txt"; //put the filename up front
    string filename2 = "scores2.txt"; //second filename after reading, manipulating, and formatting.
    string data;
    ofstream outFile;
    ifstream inFile;
    
    outFile.open(filename.c_str());
    
    if (outFile.fail())
    {
        cout << "The file was not successfully opened" << endl;
        exit(1);
    }
    
    //Set the output file stream formats
    outFile << setiosflags(ios::fixed)
    << setw(5);
    
    //Send data to the file
    //Generate 100 random numbers between 60-100 and save them in a text file
    
    for (row1 = 0; row1 < 10; row1++)
    {//start on row 1
        for (column1=0; column1 < 10; column1++)
        {
            randomnumbers = 60 + rand() % (100 + 1 - 60);
            
            outFile << setiosflags(ios::fixed)
            << setw(5) << randomnumbers;
            
            cout << setiosflags(ios::fixed)
            << setw(5) << randomnumbers;
        }
        outFile << endl;
        cout << endl;
    }
        
    
    
    outFile.close();
    cout << endl << "The file " << filename
    << " has been successfully written." << endl << endl;
    
    
    // Read the text file and do the following:
    // Count the number of test scores between 60-69, 70-79, 80-89, 90-100.
    // Determine the high, low, and average score of all test scores.
    // Display summary results (number of test scores in each range; high, low and average of all scores.)
    
    // Read the text file
    inFile.open(filename.c_str());
    
    if (inFile.fail()) //check for successful open
    {
        cout << "\nThe file was not successfully opened"
        << "\n Please check that the file currently exists."
        << endl;
        exit(1);
    }
    
    // Read and display the file's contents
    while (getline(inFile,data))
        cout << data << endl;
    
    //Count the number of test scores between 60-69
    
    
    
    
    return 0;
}

//function declarations and definitions 
For part 2 you could have a while loop that reads from the file, but instead of reading as a string you read as integer, then you simply use if statements to add to a counter for each score range
+1 ccsdude
Topic archived. No new replies allowed.