Help converting inputs.txt to outputs.txt

Pages: 123... 7
Hi so i have my code written for the majority of what i am supposed to do but for some reason when i command+R to run my code all i get is like 1 million -1.9000E10 ... i have a txt file with a 137 students ID, Attendance, hw1-7, Exam 1&2 & Final Grade.. Here is my program but for some reason my program doesnt want to even look at my txt file and output what i need it to. Any help would be greatly appreciated. :)

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
#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int TotalStudents;          // Number of students in the file
    string ID;// Students ID
    float Attendance;// Students Attendance
    float hw1;// Homework 1 Score
    float hw2;// Homework 2 Score
    float hw3;// Homework 3 Score
    float hw4;// Homework 4 Score
    float hw5;// Homework 5 Score
    float hw6;// Homework 6 Score
    float hw7;// Homework Score
    float exam1;// Exam 1 Score
    float exam2;// Exam 2 Score
    float finalExam;// Final Exam Score
    float LetterGrade;
    float NumA=0;
    float NumB=0;
    float NumC=0;
    float NumD=0;
    float NumF=0;
    float CourseGradePercentage;
    float fixed;
    ifstream inFile;
    ofstream outFile;
    inFile.open ("sampleGrades.txt");
    outFile.open ("outputGrades");
    inFile>>TotalStudents;//reads in the # of students in the class
    outFile<<"The # of students in the class:"<<TotalStudents<<endl;
    
    for (int x=0; x<TotalStudents; x++)
    {
        inFile >> TotalStudents >> ID >> Attendance >> hw1 >> hw2 >> hw3 >> hw4 >> hw5 >> hw6 >> hw7 >> exam1 >> exam2 >> finalExam;
        
        
        CourseGradePercentage = Attendance*0.10/25 + (hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7)*0.35/700 + exam1*0.15/25 + exam2*0.15/25 + finalExam * 0.25/40;
        cout << fixed << setprecision(2) << CourseGradePercentage;
        
        
        {
            if(LetterGrade>=97 && LetterGrade<=100)
            {
                outFile<<setw(15)<<" A+ ";
                NumA++;
            }
            else if (LetterGrade>=93 && LetterGrade<97)
            {
                outFile<<setw(15)<<" A ";
                NumA++;
            }
            else if (LetterGrade>=90 && LetterGrade<93)
            {
                outFile<<setw(15)<<" A- ";
                NumA++;
            }
            else if (LetterGrade>=87 && LetterGrade<90)
            {
                outFile<<setw(15)<<" B+ ";
                NumB++;
            }
            else if (LetterGrade>=83 && LetterGrade<87)
            {
                outFile<<setw(15)<<" B ";
                NumB++;
            }
            else if (LetterGrade>=80 && LetterGrade<83)
            {
                outFile<<setw(15)<<" B- ";
                NumB++;
            }
            else if (LetterGrade>=77 && LetterGrade<80)
            {
                outFile<<setw(15)<<" C+ ";
                NumC++;
            }
            else if (LetterGrade>=73 && LetterGrade<77)
            {
                outFile<<setw(15)<<" C ";
                NumC++;
            }
            else if (LetterGrade>=70 && LetterGrade<73)
            {
                outFile<<setw(15)<<" C- ";
                NumC++;
            }
            else if (LetterGrade>=60 && LetterGrade<70)
            {
                outFile<<setw(15)<<" D ";
                NumD++;
            }
            else if (LetterGrade>=0 && LetterGrade<60)
            {
                outFile<<setw(15)<<" F ";
                NumF++;
            }
         
        }
        
        outFile << setw(10) << "ID" << setw(14) << "Course Grade" << setw(14) << "Letter Grade" <<endl;
        outFile << setw(10) << ID << setw(14) << CourseGradePercentage << setw(14) << LetterGrade;
    }
    
    outFile << endl << endl;
    outFile << "The # of A's: " << NumA << endl;
    outFile << "The # of B's: " << NumB << endl;
    outFile << "The # of C's: " << NumC << endl;
    outFile << "The # of D's: " << NumC << endl;
    outFile << "The # of F's: " << NumF << endl;
    
    inFile.close();
    outFile.close();
    return 0;
}
Last edited on
Heres my updated version.. i figured i did some stuff wrong.. but i have the txt file that we got attached to our hw assignment and it still wont do anything... please anyone ?

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
#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    ifstream sampleGrades("sampleGrades.txt");
    
    int TotalStudents, CourseGradePercentage;
    sampleGrades >> TotalStudents;
    
    double ID, Attendance, hw1, hw2, hw3, hw4, hw5, hw6, hw7, exam1, exam2, finalExam;
    float LetterGrade;
    float NumA=0;
    float NumB=0;
    float NumC=0;
    float NumD=0;
    float NumF=0;
    float fixed;
    ofstream outFile;

    
    
    for (int i=0; i<TotalStudents; i++)
    {
        sampleGrades>>TotalStudents>>ID>>Attendance>>hw1>>hw2>>hw3>>hw4>>hw5>>hw6>>hw7>>exam1>>exam2>>finalExam;
        
        
        CourseGradePercentage = Attendance*0.10/25 + (hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7)*0.35/700 + exam1*0.15/25 + exam2*0.15/25 + finalExam * 0.25/40;
        cout << fixed << setprecision(2) << CourseGradePercentage;
        
        
        {
            if(LetterGrade>=97 && LetterGrade<=100)
            {
                outFile<<setw(15)<<" A+ ";
                NumA++;
            }
            else if (LetterGrade>=93 && LetterGrade<97)
            {
                outFile<<setw(15)<<" A ";
                NumA++;
            }
            else if (LetterGrade>=90 && LetterGrade<93)
            {
                outFile<<setw(15)<<" A- ";
                NumA++;
            }
            else if (LetterGrade>=87 && LetterGrade<90)
            {
                outFile<<setw(15)<<" B+ ";
                NumB++;
            }
            else if (LetterGrade>=83 && LetterGrade<87)
            {
                outFile<<setw(15)<<" B ";
                NumB++;
            }
            else if (LetterGrade>=80 && LetterGrade<83)
            {
                outFile<<setw(15)<<" B- ";
                NumB++;
            }
            else if (LetterGrade>=77 && LetterGrade<80)
            {
                outFile<<setw(15)<<" C+ ";
                NumC++;
            }
            else if (LetterGrade>=73 && LetterGrade<77)
            {
                outFile<<setw(15)<<" C ";
                NumC++;
            }
            else if (LetterGrade>=70 && LetterGrade<73)
            {
                outFile<<setw(15)<<" C- ";
                NumC++;
            }
            else if (LetterGrade>=60 && LetterGrade<70)
            {
                outFile<<setw(15)<<" D ";
                NumD++;
            }
            else if (LetterGrade>=0 && LetterGrade<60)
            {
                outFile<<setw(15)<<" F ";
                NumF++;
            }
         
        }
        
        outFile<<setw(10)<<"ID"<<setw(14)<<"Course Grade"<<setw(14)<<"Letter Grade"<<endl;
        outFile<<setw(10)<<ID<<setw(14)<<CourseGradePercentage<<setw(14)<<LetterGrade;
    }
    
    outFile<<endl;
    outFile<<"The # of A's: "<<NumA<<endl;
    outFile<<"The # of B's: "<<NumB<<endl;
    outFile<<"The # of C's: "<<NumC<<endl;
    outFile<<"The # of D's: "<<NumC<<endl;
    outFile<<"The # of F's: "<<NumF<<endl;
    
    sampleGrades.close();
    outFile.close();
    return 0;
}
closed account (48T7M4Gy)
If it works it works I s'pose but on first glance I see mountains of repetitition that lends itself to switch, array, perhaps even enum type 'weapons'. else if's are there for some purpose but I reckon they are best avoided - perhaps at all costs.

These have the effect of simplification, clarity and above all efficienet debugging and maintainability.

But that doesn't address the OP problem.

I'd suggest going back to square one with a separate small program and see whether you can just read the file before trying to debug 108 lines. Once you've got the read going you can go back to the production program. I would use the structure of the program in the tutorial:
http://www.cplusplus.com/doc/tutorial/files/
Last edited on
ya i tried making a program with just this and i cant get it to open it...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<fstream>
#include<iomanip>
#include<string>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    ifstream sampleGrades;
    sampleGrades.open("sampleGrades.txt");

if ( !sampleGrades)
{
    cerr << "File sampleGrades.txt could not be opened";
    exit(1);

}}
and it doesnt work at all... i need it to display -->

students ID# Grade (%) Letter Grade


but all i get when i run it is in my output box in xcode just a scientific notation number... what is wrong with my program i thought i had it right :(
closed account (48T7M4Gy)
Lost after edit oops
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("samplegrades.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
o hey kemort :) thanks for helping me again !
here is the 10 out of the 137 lines of my txt file

ID Attendance hw1 hw2 hw3 hw4 hw5 hw6 hw7 exam1 exam2 finalExam
137
1 9 98 79 100 100 85 0 100 24 24 32
2 8 105 105 0 0 0 0 0 23 0 0
3 24 105 95 105 100 96 98 97 25 25 37
4 25 98.7 105 105 105 103.95 104 105 22 21 39
5 21 94.5 98 92 95 90 90 90.3 20 15 30
6 24 95.55 100 99 100 92 60 105 24 21 32
7 17 90 95 82 90.3 68 99 60 23 14 25
8 25 90 102.9 98.7 95.55 100 72 105 21 23 35
9 23 80 93 95 53 78 68 100 19 18 22
10 24 83 96 100 96 55 84 100 19 14 28 
Last edited on
This is what im supposed to do but i wont ask for help on all of it:
--> Write a program that 1) reads the file contents, 2) calculates a numerical and letter grade, 3) prints the student ID, numerical grade, and letter grade to a file, and 4) calculates the average attendance for each letter grade range (A's, B's, C's, D's, and F's).

i tried that code and it was unable to open. I've never seen what opening a file on c++ looks like so im not sure what to expect.
closed account (48T7M4Gy)
OK phil, I have to go away for an hour or so. Maybe someone else might want to chip in in the meantime. But if not I won't forget.

As a first look my first try would be:
a) use the code I put up

b) strtok on each line that comes in using the delimiter space

c) look up strtok() here if you aren't familiar with it - there may be a cin equivalent but I never use it

d) make sure your file ends with a return on the last (and final) line of the txt file- sometimes that does wonders

The point is, if you can't at least read the file, the rest is ... not worth the paper etc

Also the quick look suggests your first line is read in a different way to the rest and that is significant and a bit problematic.

Cheers,
Last edited on
thank you kemort i will try and use the tools you just gave me right now. Talk to you later man have a good one
Last edited on
yah i tried your code, i tried moving the txt file around because maybe xcode isnt processing it idk.. im not sure what strtok is and i havent learned it yet in class so i dont think id be allowed to use it :/ Im going crazy lol.. all it keeps saying is unable to read file or whatever my cout is
#include <cstdlib>
And add these lines at the beginning of main()
1
2
std::system( "pwd" ) ; // http://www.freebsd.org/cgi/man.cgi?query=pwd&format=html
return 0 ;

Build and run the program.

Copy samplegrades.txt to the directory path that was printed out, comment out the two added lines, and the program would be able to find the file.

Once you have verified that the file is opened successfully, proceed to fix the rest of your code.
so here is the program i built and ran just to test it.. although it said that my ifstream inFile wont be exectuted. I got a firectory in the output box. So i should take my sampleGrades.txt file and put it in that directory ? what do you mean by comment out the 2 added lines. I dont know how to debug if thats what you mean. Thank you for the reply JL

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
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main () {
    std::system( "pwd" );
    return 0 ;
    
    
    ifstream inFile;      // This is a declaration of the inFile that holds all the grades
    string fileName;      // This is the filename that the user will enter
    string name;
    
    // Prompts the user to input the file name that will be read and assigns it
    //  to fileName
    
    cout << "Enter the input file name: ";
    cin >> fileName;
    
    // Open the file with the grades
    
    inFile.open(fileName.c_str ());
    
    // Check to make sure the file opened correctly
    
    if (!inFile)
    {
        cout << "File did not open correctly" << endl;
        return 1;
    }}
i got this but i cant find this on my computer .
/Users/PhilthyPhil/Library/Developer/Xcode/DerivedData/SSSSSSS-duehhkrnzcsjvobrlewawhtdcylx/Build/Products/Debug
never mind i found it. so i dragged my txt file to that folder.. im still stuck on what to do next lol sorry
i tried building and running it and it still didnt work :'(
ok so now when i build and run it doesnt say anything.. it doesnt say file could no open. it just doesnt say anything.. does that mean that the file opened ??? or should the actualy txt file open up ?

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
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main () {
    std::system( "pwd" );
    ///Users/PhilthyPhil/Library/Developer/Xcode/DerivedData/SSSSSSS-duehhkrnzcsjvobrlewawhtdcylx/Build/Products/Debug
    
    
    ifstream inFile;      // This is a declaration of the inFile that holds all the grades
    string fileName;      // This is the filename that the user will enter
    string name;
    
    // Prompts the user to input the file name that will be read and assigns it
    //  to fileName
    
    cout << "Enter the input file name: ";
    cin >> fileName;
    
    // Open the file with the grades
    
    inFile.open(fileName.c_str ());
    
    // Check to make sure the file opened correctly
    
    if (!inFile)
    {
        cout << "File did not open correctly" << endl;
        return 0;
    }}
Last edited on
closed account (48T7M4Gy)
C:\Users\me\Programs\Project1\Debug>[/output]
Last edited on
kemort !!! :) it worked. thanks to JL now when i build and run , in my output it shows the whole txt file :) but now when i add to my main program what should it do ? because i need my ID Grade % and Letter grade to be out onto an outputGrades.txt file :) :) thanks !
Pages: 123... 7