• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

simple data structures w/ arrays

I have this little program ive made, its with simple structrues and uses arrays..
the program is compliing fine but does not give me the expected output, ive looked at it for a while now and still cant seem to figure it out. its not writing out to the output file like its supposed to, everytime i try to run it, it gives me an error.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Student
{
      string name;
      long idNum;
      int creditPts;
      int creditHrs;
      double gpa;
};

const int SIZE = 20;
Student record[SIZE];
void ReadRecords(Student[], ifstream&);
double CalculateGpa(int creditPts, int creditHrs);
void WriteOut(Student[], ofstream&);

int main()
{
      
      

ifstream inFile;
inFile.open ("records.txt");
ofstream outFile;
outFile.open("gpa.txt");

ReadRecords(record,inFile);
for(int i=0; i<3; i++)
{
      record[i].gpa=CalculateGpa(record[i].creditPts, record[i].creditHrs);
}
WriteOut(record,outFile);

      return 0;

}

void ReadRecords(Student record[], ifstream &inFile)
{
      int files;
      inFile>>files;
      for (int i=0; i<files; i++)
      {
            getline(inFile, record[i].name);
            inFile>>record[i].idNum;
            inFile>>record[i].creditPts;
            inFile>>record[i].creditHrs;
      }

}

double CalculateGpa(int creditPts, int creditHrs)
{
double gpa;
gpa=creditPts/creditHrs;

return gpa;
}

void WriteOut(Student record[], ofstream& outFile)
{
      for (int i=0; i < SIZE; i++)
      {
            outFile<< "NAME: " << record[i].name << endl;
            outFile<< "GPA : " << record[i].gpa << endl;
      }
}



the input file looks like this:
3
Jason Smith
3433 433 44
Michael Adams
3442 454 33
Chris James
5332 353 23

its supposed to print out the the name and the GPA of all the students.
0
Kal130
Asked:
Kal130
  • 5
  • 2
  • 2
  • +1
1 Solution
 
AxterCommented:
Hi Kal130,
What are you getting, and what are you expecting?

David Maisonave :-)
Cheers!
0
 
Kal130Author Commented:
well when i run this, it just gives in a runtime error
i want it to print out in gpa.txt
NAME: Jason
GPA:  34

for all the entries
0
 
AxterCommented:
Did you try running it in debug mode, and check where you're getting the runtime error at?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Kal130Author Commented:
when i comment out the
for(int i=0; i<3; i++)
{
     record[i].gpa=CalculateGpa(record[i].creditPts, record[i].creditHrs);
}

it will run, but it only gives me NAME and GPA 20 times without the actualy names that have been read in, thats one problem and obviously the other problem is with the CalculateGpa function which i dont know whats wrong w/ it.
0
 
VolatileVoidCommented:
Well, your first mistake is in WriteOut. WriteOut's loop goes from 0 to SIZE (which is 20), but it shouldn't, since you only read in 3 (not 20) names. You need to have another global variable, numRecords. In ReadRecords, numRecords = files.
0
 
VolatileVoidCommented:
Oh, forgot to add. Then you'd change WriteOut to be something like:
void WriteOut(Student record[], ofstream& outFile)
{
     for (int i=0; i < numRecords; i++)
     {
          outFile<< "NAME: " << record[i].name << endl;
          outFile<< "GPA : " << record[i].gpa << endl;
     }
}
0
 
Kal130Author Commented:
when i try this w/ your suggestions, the program still hangs...when i comment out the:
for(int i=0; i<3; i++)
{
     record[i].gpa=CalculateGpa(record[i].creditPts, record[i].creditHrs);
}

the program doesnt hang anymore and ouputs to the file 3 Names and GPA's, but it does not output the actual names and gpa's
0
 
novitiateCommented:
try this


#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Student
{
      string name;
      long idNum;
      int creditPts;
      int creditHrs;
      double gpa;
};

const int SIZE = 20;
Student record[SIZE];
int ReadRecords(Student[], ifstream&);
double CalculateGpa(int creditPts, int creditHrs);
void WriteOut(Student[], ofstream&, int);

int main()
{
      
      
      
      ifstream inFile;
      inFile.open ("c:\\temp\\records.txt");
      ofstream outFile;
      outFile.open("gpa.txt");
      
      int nRecords = ReadRecords(record,inFile);
      for(int i=0; i<nRecords; i++)
      {
            record[i].gpa=CalculateGpa(record[i].creditPts, record[i].creditHrs);
      }
      WriteOut(record,outFile, nRecords);
      
      return 0;
      
}

int ReadRecords(Student record[], ifstream &inFile)
{
      int files;
      inFile>>files;
      for (int i=0; i<files; i++)
      {
            inFile.get();
            getline(inFile, record[i].name);
            cout << record[i].name << endl;
            inFile>>record[i].idNum;
            inFile>>record[i].creditPts;
            inFile>>record[i].creditHrs;
      }

      return files;
      
}

double CalculateGpa(int creditPts, int creditHrs)
{
      double gpa = -1;
      try{
            gpa=creditPts/creditHrs;
      
      }catch(...)
      {
            cout << "Devided by Zero Exception\n";
      }
      return gpa;
}

void WriteOut(Student record[], ofstream& outFile, int nRecords)
{
      for (int i=0; i < nRecords; i++)
      {
            outFile<< "NAME: " << record[i].name << endl;
            outFile<< "GPA : " << record[i].gpa << endl;
      }
}

_novi_
0
 
Kal130Author Commented:
that compiled but it didnt work, it went crazy when i ran it, but the thing is, this is an assignment and the instructor gave us the parameters for the functions we need to use, so i cant really put anything extra, and we dont need a divide by 0 exception.  for the part where doesnt output the names, im not sure if its reading in the entire string becasue when i use cin>>record[i].name; it outputs to the file the first name (not the last)..nothing else though but still more than nothing

0
 
Kal130Author Commented:
actaully i take that back, it did work, it was an error on my part.

thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now