reading words from a text file

why doesn't the file reading statement in the code below return "Harry" & "Houdini"? It seems that, based on the info I have found, it should return the 1st 2 words.

......CODE......
//
// main.cpp
// GradeCalculator
//
// Created by Andrew on 6/4/13.
//
//

#include <iostream>
#include <fstream>
#include <string>
//#include "Assignment4.h"

using namespace std;
//--------build structure
struct Grade
{
string StudentName;
int NoOfHomeWorkGrades;
double HomeworkGPA;
double HomeworkWeight;
int NoOfProjectGrades;
double ProjectGPA;
double ProjectWeight;
int NoOfExamGrades;
double ExamWeight;
double ExamGPA;
double TotalGPA;
char LetterGrade;
string Comment;
}GradeStructure;


//----------function declarations
void GetInFileName(string&);
void GetOutFileName(string&);
int ReadFile(double&);
string GetComment(char&);
void GetLetterGrade(double&);
int OpenInFile(ifstream&);
int OpenOutFile(ofstream&);
//string GetOutFileName(string&);
int WriteData(char&, string&);
int CalculateGrade();

//-----------Main Body
int main()
{
double TotalGPA;
ifstream InDataFile;
ofstream OutDataFile;
string InFileName;
string OutFileName;
string Quote;
//char LetterGrade;
cout << "Welcome to the GradeCaculator Deluxe." << endl;
ReadFile(TotalGPA);
GetLetterGrade(TotalGPA);//this function calls "GetComment()"
//WriteData(LetterGrade, Quote);
//---------create structure--------------------
//system ("pause");
return 0;
}//end main
//-----------GetFileName
//-----------Get file name to read from
void GetInFileName(string& InFileName)
{
cout << "Enter the name of the file to gather info from (including extension): ";
cin >> InFileName;
//reference variables do not need a return
}//end GetInFileName
//----------GetOutFileName
//----------Get name of output file
void GetOutFileName(string& OutFileName)
{
cout << "Enter the name of the file to write info to (including extension): ";
cin >> OutFileName;
}//end GetOutFileName
//----------OpenInFile
//----------open the user specified file to read from
int OpenInFile(ifstream& InDataFile)
{
string InFileName;
GetInFileName(InFileName);

InDataFile.open(InFileName.c_str());

if (!InDataFile)
{
cout << "File Failed to open...Program terminated" << endl;
return 1;
}//end if
//ReadFile(TotalGPA);
return 0;

}//end OpenInFile
int OpenOutFile(ofstream& OutDataFile)
{
string OutFileName;
GetOutFileName(OutFileName);

OutDataFile.open(OutFileName.c_str());

if (!OutDataFile)
{
cout << "File Failed to open...Program terminated" << endl;
return 1;
}//end if
//ReadFile(TotalGPA);
return 0;
}//end OpenInFile

//---------ReadFile
//---------read the user specified file
int ReadFile (double& TotalGPA)
{
ifstream InDataFile;
int RowNumber;
string FirstName;
string LastName;
int StudentId;
int NoOfClasses;
string Grade;
//float TotalPtsAllowed;
int index;
//float TotalScore;
//int Grades;
//double Weight;
//double Average;
int Assignments[3];
//int GradeArray[50];
//double WeightArray[3];

OpenInFile(InDataFile);

RowNumber = 0;
while (RowNumber < 6)
{//start outter loop
InDataFile >> FirstName >> LastName >> StudentId >> NoOfClasses >> Grade;
index = 0;
//TotalScore = 0;
// Assignments[index] = NoOfAssignments;
// cout << Assignments[index] << endl;
//cout << Assignments[0] << Assignments[1] << Assignments[2] << endl;

// while(index < NoOfAssignments)
//{//start (inner loop)
// InDataFile >> Grades;
//index++;

//TotalScore = TotalScore + Grades;
//}//end while (inner loop)
//InDataFile >> Weight;
//cout << TotalScore << " " << TotalPtsAllowed << endl;
//Average = TotalScore / TotalPtsAllowed;
//Average = (TotalScore / TotalPtsAllowed) * Weight;
//TotalGPA = TotalGPA + Average;
RowNumber++;

}//end while (outer loop)
cout << InDataFile << FirstName << LastName << StudentId << NoOfClasses << Grade;
//cout << "Total GPA is:: " << TotalGPA << endl;
// GradeStructure.NoOfHomeWorkGrades = Assignments[0];
// GradeStructure.NoOfProjectGrades = Assignments[1];
// GradeStructure.NoOfExamGrades = Assignments[2];
// cout << GradeStructure.NoOfHomeWorkGrades << endl;
// cout << GradeStructure.NoOfProjectGrades << endl;
// cout << GradeStructure.NoOfExamGrades << endl;

return 0;
InDataFile.close();
}//end ReadFile
//-----------WriteData
//-----------write the data to user specified file
int WriteData (char& LeterGrade, string& Quote)
{
//char LetterGrade;
//string Quote;
ofstream OutDataFile;

OpenOutFile(OutDataFile);
OutDataFile << "NAME: A.C. Millard" << endl;
OutDataFile << "Your Letter Grade is ::" << LeterGrade << endl;
OutDataFile << Quote << endl;
return 0;
}//end WrtieData
//----------GetComment
//----------Create witty comment based on the letter grade
string GetComment (char& LetterGrade)
{
//char LetterGrade;
//LetterGrade=GetLetterGrade();
string Quote;
//cout << LetterGrade << endl;
switch(LetterGrade)
{
case 'A' : Quote = "The Fonz says...Exactamundo!";
break;
case 'B' : Quote = "The Fonz says..Aaaaeeeyyy!";
break;
case 'C' : Quote = "The Fonz says he was wrrroooogh about you.";
break;
case 'D' : Quote = "The Fonz says...Step into my office.";
break;
case 'F' : Quote = "The Fonz says Whoa!";
break;
}
cout << Quote << endl;
WriteData(LetterGrade, Quote);
return Quote;
}//end GetComment
//---------GetLetterGrade
//---------Get the letter grade based on the average
void GetLetterGrade(double& TotalGPA)
{
//cout << TotalGPA << endl;
//double TotalGPA;
char LetterGrade;

if (TotalGPA > 3.50)
LetterGrade = 'A';
else
{
if (TotalGPA >= 2.40 && TotalGPA < 3.50)
LetterGrade = 'B';
if (TotalGPA >= 1.40 && TotalGPA < 2.40)
LetterGrade = 'C';
if (TotalGPA >= 0.040 && TotalGPA < 1.40)
LetterGrade = 'D';
if (TotalGPA < .40)
LetterGrade = 'F';
}//end else
cout << "Your Letter Grade is:: " << LetterGrade << endl;
GetComment(LetterGrade);
//return LetterGrade;
}//end GetLetterGrade

//----------CalculateGrade
//----------perform Grade Calcualtions
int CalculateGrade()
{
int Weight;
int Grade;
int TotalScore;
int TotalPtsAllowed;
Grade=(TotalScore / TotalPtsAllowed) * Weight;
return Grade;


Harry Houdini 1212 3 MAG101 A PRES201 A ESC301 A
Henry Ford 2211 2 MFG110 A SAL202 B
Albert Einstein 1155 1 CALC100 F
Alan Turing 9082 4 CSC110 A CSC201 A CSC210 A CSC205 A
Melvil Dewey 1876 2 LIBM102 C SUPV202
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

struct student
{
    std::string first_name ;
    std::string last_name ;
    int id ;
    int num_grades ;

    struct grade
    {
        std::string class_name ;
        char grade ;
    };

    std::vector<grade> grades ;
};

int main()
{

    // create a test file
    {
        std::ofstream( "test_file.txt" ) <<
            "Harry Houdini 1212 3 MAG101 A PRES201 A ESC301 A\n"
            "Henry Ford 2211 2 MFG110 A SAL202 B\n"
            "Albert Einstein 1155 1 CALC100 F\n"
            "Alan Turing 9082 4 CSC110 A CSC201 A CSC210 A CSC205 A\n"
            "Melvil Dewey 1876 2 LIBM102 C SUPV202 B\n" ;
    }

    std::vector<student> students ;

    // read the file
    {
        std::ifstream file( "test_file.txt" ) ;

        student s ;
        while( file >> s.first_name >> s.last_name >> s.id >> s.num_grades )
        {
            s.grades.clear() ;
            student::grade g ;
            for( int i = 0 ; ( i < s.num_grades ) && ( file >> g.class_name >> g.grade ) ; ++i )
               s.grades.push_back(g) ;

            students.push_back(s) ;
        }
    }

    // print out what was read in
    {
        int rec_num = 0 ;

        for( const student& s : students )
        {
            std::cout << ++rec_num << ". " << s.first_name << ' ' << s.last_name 
                      << " id: " << s.id << " grades(" << s.num_grades << "): " ;

            for( const student::grade& g : s.grades )
                std::cout << "{ " << g.class_name << ' ' << g.grade << " } " ;

            std::cout << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/1eda433415f5dae0
ok, thanks for the help. trying to read it and understand it. I get the following error....."[ERROR] range-based for loops are not allowed in c++98 mode"
Compile with -std=c++11 (or if the implementation is reasonably current -std=c++14)

For instance: g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors my_program.cpp
thanks, that did the trick. Can you give me a brief explanation as to how this works? I see that you created a structure....understand...I figured out what the double colon does.....understand (which is pretty cool)......but I'm not really sure what the for loop means with the colon in it. so I'm not really sure why your code captures the whole word and mine didn't....

I really appreciate you helping this old dog learn some new tricks.
Last edited on
1
2
3
4
5
6
7
8
for( const student& s : students ) // for each student 's' in the vector students
{
    // ...
    for( const student::grade& g : s.grades ) // for each grade 'g' in the vector s.grades
    {
         // ...
    }
}

overview: http://www.stroustrup.com/C++11FAQ.html#for
details: http://en.cppreference.com/w/cpp/language/range-for
Topic archived. No new replies allowed.