input files?

I need to write a program that gets data from a specific line in an input file that has more than one line.
each line has the following information:
Firstname Lastname lab1 lab2 lab3 lab4 lab5 lab6 lab7 lab8 lab9 lab10 hw1, hw2 hw3 hw4 hw5 hw6 hw7 hw8 midterm final

Here is what I have:

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


#include <iostream>

using namespace std;

int main() {
    
	
    int averagelab;
    int averagehw ;
    
    
    int	HW1, HW2, HW3, HW4, HW5, HW6, HW7, HW8;
    
    
    

    
    cin >> HW1 >> HW2 >> HW3 >> HW4 >> HW5 >> HW6 >> HW7 >> HW8 ;
    
    averagehw = ( HW1 + HW2 + HW3 + HW4 + HW5 + HW6 + HW7 + HW8 ) / 8 ;
    
    
    int LAB1, LAB2, LAB3, LAB4, LAB5, LAB6, LAB7, LAB8, LAB9, LAB10;
    
   
    
    cin >> LAB1 >> LAB2 >> LAB3 >> LAB4 >> LAB5 >> LAB6 >> LAB7 >> LAB8 >> LAB9 >> LAB10 ;
    
    
    
    averagelab = ( LAB1 + LAB2 + LAB3 + LAB4 + LAB5 + LAB6 + LAB7 + LAB8 + LAB9 + LAB10 ) / 10 ;
    
    
    
    
    
    int MIDTERM ;
    
	

    
    cin>> MIDTERM ;
    
    
    int FINAL ;
    

    
    cin>> FINAL ;
    
    cout << "    Average Homework Grade is:" << averagehw <<endl;
    
    cout<<"    Average Lab Grade is:" << averagelab <<endl ;
    
    cout<<"    Average Course Grade is:" << (averagelab*.20+averagehw*.10+MIDTERM*.30+FINAL*.40 )<<endl ;
    
    int LETTERGRADE ;
    
    LETTERGRADE = (averagelab*.20+averagehw*.10+MIDTERM*.30+FINAL*.40 );
    
    if (LETTERGRADE <= 100 && LETTERGRADE > 90)
        cout << "    Letter Grade For the Course is an A"<<endl;
    else if (LETTERGRADE <= 90 && LETTERGRADE > 85)
        cout << "    Letter Grade For the Course is an A-"<<endl;
    if (LETTERGRADE <= 85 && LETTERGRADE > 80)
        cout << "    Letter Grade For the Course is a B+"<<endl;
    else if (LETTERGRADE <= 80 && LETTERGRADE > 75)
        cout << "    Letter Grade For the Course is a B"<<endl;
    if (LETTERGRADE <= 75 && LETTERGRADE > 70)
        cout << "    Letter Grade For the Course is a B-"<<endl;
    else if (LETTERGRADE <= 70 && LETTERGRADE > 65)
        cout << "    Letter Grade For the Course is a C+"<<endl;
    if (LETTERGRADE <= 65 && LETTERGRADE > 60)
        cout << "    Letter Grade For the Course is a C"<<endl;
    else if (LETTERGRADE <= 60 && LETTERGRADE > 55)
        cout << "    Letter Grade For the Course is a C-"<<endl;
    else if (LETTERGRADE <= 55 && LETTERGRADE > 50)
        cout << "    Letter Grade For the Course is a D+"<<endl;
    else if (LETTERGRADE <= 50 && LETTERGRADE > 45)
        cout << "    Letter Grade For the Course is a D"<<endl;
    else if (LETTERGRADE <= 45 && LETTERGRADE > 40)
        cout << "    Letter Grade For the Course is a D-"<<endl;
    else if (LETTERGRADE <= 40 && LETTERGRADE > 0)
        cout << "    Letter Grade For the Course is an F"<<endl;
    
    
    
   
    
}




as you can see I'm using cin but what I want is that program to get that specific info from a file. I found a few examples online using fstream but none of them seemed to be close to my situation.

Thanks,
Last edited on
What problems with file streams do you have? They are literally work like any other input stream.
1
2
std::ifstream input("MyFile.txt");
//Now just replace every std::cin with input 
I find things like this easier when using a data structure.

Firstname Lastname lab1 lab2 lab3 lab4 lab5 lab6 lab7 lab8 lab9 lab10 hw1, hw2 hw3 hw4 hw5 hw6 hw7 hw8 midterm final


All of this could be on the same line of a file seperated by a space or tab:

Records.txt

Titan Rolland 01 02 03 04 05 06 07 08 09 10 101 102 103 104 105 106 107 108 999 909
Your Name 11 12 13 14 15 16 17 18 19 111 112 113 114 115 116 117 118 9999 9009


As you can see each record has the same variables, something you can contain together:

1
2
3
4
5
6
7
struct Record
{
std::string fname, lname;
int lab1 /*...*/ lab10;
int hw1 /*...*/ hw9;
int midterm, final;
};


You would read in the file something similar to this:

1
2
3
4
5
6
7
8
9
10
Record MyRecord[2];
std::ifstream ifs("Record.txt");

int CurrentRecord; CurrentRecord = 0;

while(ifs != EOF)
{
ifs >> MyRecord[CurrentRecord].fname /*...*/ >> MyRecord[CurrentRecord].final;
CurrentRecord++;
}


That's the basic gist of it. When it comes to like data such as the example you are using, it is always better to contain it rather than having variables scattered about the place, makes it a lot easier to use and read.

The other advantage of programming like this is that you can create a function to read in files and display records with a single function, rather than having to type the same operations over and over and over again.

EDIT:

Another advantage is searching through records, as the data is all contained, most likely in an array of records, you could create a function such as:

1
2
3
4
5
6
7
8
9
10
void PrintRecord(std::string fn)
{
    for(int i = 0; i < MaxRecords; i++)
    {
        if(MyRecord[i].fname != fn) continue; // Skip over to the next record if the name is not the same
        cout << MyRecord[i]; // This will require overloading the ostream operator <<
        return; // EDIT 2
    }
    std::cout << "No record found\n"; // If it goes through the loop no such record exists with that name
}
Last edited on
Topic archived. No new replies allowed.