Let me start off by thanking the individuals who have helped me before on other projects, you are awesome. Now on to my new issue.

We are learning STRUCT in our new assignment. I had an issue before that was resolved by using a call by reference instead of value. I have my whole program worked out except for one little part.

The project is a basic GPA record for a student, the information being read in from a .txt file. I have figured out all parts of the assignment on my own accept for one.

The information in the .txt file is structured like this, with each grade being paired with an 'hours worth':

FirstName LastName IDnumber Year Grade Hour Grade Hours Grade Hours Grade Hours etc..
so the first entry looks like this:

joe paul 91934 Junior 3.0 4 4.0 3 3.0 2 3.0 3

I know I need a loop to read into my struct arrays the grade and hour, and it needs to terminate when a new line character is reached, I am just not sure how to implement it. I will post the section of my code I am having trouble with, then my total code below that as a reference. If it matters, I will copy and paste the text data file below that.

As of this point I am just trying to get it to read the first line, I am not worried about looping through the entire txt yet, which I am fairly confident I can do from and my knowledge of an earlier project.

dataIn >> arg.gradepoint[counter];

while(      )
{
     dataIn >> arg.hours[counter];
     counter++;
     total = total+arg.gradepoint[counter];
     dataIn >> arg.gradepoint[counter];
}

all code:

Toggle Code View
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

ifstream dataIn;
ofstream dataOut;

struct record {
       string fName;
       string lName;
       int idNumber;
       string year;
       float gradepoint[8];
       int hours[8];
       float avg;
       };

void testFill(struct record &arg);//will change function name after testing
void testPrint(struct record &arg);//will change function name after testing
void fixCapital(struct record &arg);

int main(int argc, char *argv[])
{
    struct record student;
    
    
    testFill(student);
    testPrint(student);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void testPrint(struct record &arg){
     cout << "Name: " << arg.fName << " " << arg.lName << endl;
     cout << "Id Number: " << arg.idNumber << endl;
     cout << "Year: " << arg.year << endl;
     
     for(int i = 0; i < 8; i++){
         cout << "Grade: " << arg.gradepoint[i] << " ";
         cout << "Hours: " << arg.hours[i] << endl;   
             }
     
     cout << "Average: " << arg.avg << endl;
     }

void testFill(struct record &arg){
     int counter = 0;
     float total = 0;
     char g;
     
     
     dataIn.open("grades2.txt");
     dataIn >> arg.fName;
     dataIn >> arg.lName;
     fixCapital(arg);
     
     dataIn >> arg.idNumber;
     
     dataIn >> arg.year;
     
     dataIn >> arg.gradepoint[counter];
     while(    )
     {    
          dataIn >> arg.hours[counter];
          counter++;
          total = total+arg.hours[counter];
          dataIn >> arg.gradepoint[counter];
     }
     
     
     arg.avg = total / counter;
     
     }



void fixCapital(struct record &arg)
{
     int len = arg.fName.length();
     for(int i = 0; i < len; i++)
     {
             arg.fName[i] = tolower((unsigned char)arg.fName[i]);
             }
     
     int len2 = arg.lName.length();
     for(int i = 0; i < len2; i++)
     {
             arg.lName[i] = tolower((unsigned char)arg.lName[i]);
             }
     
     arg.fName[0] = toupper((unsigned char)arg.fName[0]);
     arg.lName[0] = toupper((unsigned char)arg.lName[0]);
     
    
      
}
 
Home :: Send a comment :: Discuss
            
©2007 - 2011 Veign, LLC All Rights Reserved :: Designed & Developed by Veign

Online Password Generator :: Online Image Resizer

grades2.txt

----------------------------------

joe paul 91934 Junior 3.0 4 4.0 3 3.0 2 3.0 3
hIC motoe 18382 Senior 4.0 4 3.0 3 2.0 4 4.0 5 4.0 1
SUE BLUE 82341 Junior 2.0 2 3.0 3 4.0 4 4.0 1 4.0 3
Watts knew 11313 Sophmore 1.0 4 0.0 5 0.0 3 1.0 3 1.0 4
whatcha macllit 82842 Senior 4.0 4 4.0 4 4.0 5 4.0 3 4.0 1
lAST wON 72734 Junior 4.0 4 4.0 2 4.0 3 3.0 3 2.0 2 4.0 2
jim paul 44224 Junior 3.0 4 4.0 3 3.0 2 3.0 3
hIC wicy 17382 Senior 4.0 4 3.0 3 2.0 4 4.0 5 4.0 1
HICKy clury 83611 Junior 2.0 2 3.0 3 4.0 4 4.0 1 4.0 3
Matt knows 12413 Sophmore 1.0 4 0.0 5 0.0 3 1.0 3 1.0 4
whichway macit 87232 Senior 4.0 4 4.0 4 4.0 5 4.0 3 4.0 1.0
joe smith 91884 Junior 3.0 4 4.0 3 3.0 2 3.0 3.
Moe town 13482 Senior 4.0 4 3.0 3 2.0 4 4.0 5 4.0 1
Hatch chew 81141 Junior 2.0 2 3.0 3 4.0 4 4.0 1 4.0 3
eli agwknew 14513 Sophmore 1.0 4 0.0 5 0.0 3 1.0 3 1.0 4
which waigh 99842 Senior 4.0 4 4.0 4 4.0 5 4.0 3 4.0 1
first barron 78324 Junior 4.0 4 4.0 2 4.0 3 3.0 3 2.0 2 4.0 2
jim slider 45424 Junior 3.0 4 4.0 3 3.0 2 3.0 3
mami yourami 12382 Senior 4.0 4 3.0 3 2.0 4 4.0 5 4.0 1
iron aaron 88321 Junior 2.0 2 3.0 3 4.0 4 4.0 1 4.0 3
thirons thorns 16003 Sophmore 4.0 4 1.0 5 4.0 3 4.0 3 4.0 4

If you know there is a fixed number of grades/courses, then you actually won't need to check for the newline.

If you do want to read until the newline, then you need two steps. First, you read the entire line into a string, and then, you use the string to read in all the other variables (using a stringstream that acts as a fstream but on a string, found in #include <sstream>). Here is how to add this:

string s;
getline(dataIn, s); //read the entire line until the newline character, into string s.
stringstream ss(s); //create a stringstream to read from the string s.

while( (counter < 8) && //while not at the end of counter. 
       (ss >> arg.gradepoint[counter] >> arg.hours[counter]) ) //and stream is not empty.
{
     total = total + arg.gradepoint[counter];
     counter++;
};

Edited 5 Years Ago by mike_2000_17: n/a

This article has been dead for over six months. Start a new discussion instead.