Please help my getgpa function is not reading data

Hello guys, my GPA function is not reading data from the courses. I can't find a solution. I'll be glad if any of you help me.


#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


//Structure named Course that holds the students courses variables;
//one string, character,short, and one pointer to the next course.
//This structure also has a constructor that initializes the pointer
//equal to null.
typedef struct Course // structure definition
{
string coursenum;
char grade;
short numcredits;

//Pointer to the next course
Course *nextCourse;

//Constructor
Course(){
//initializing pointer equal to null
nextCourse = NULL;
}

}*Cours; //

///Structure named Student that holds the students information variables;
//one string, integer,short,float, one pointer to the next student structure,
//and one pointer named CoursePtr of type Course that points to the course
//structure. This structure also has a constructor that initializes the pointers
//equal to null.
typedef struct Student // structure definition
{
string name;
int id;
short numcourses;
float gpa;

//Course Pointer that points to the course structure
Course *CoursePtr;

//Pointer to the next student
Student *next;

//Constructor
Student(){
//initializing pointer equal to null
CoursePtr = NULL;
next = NULL;
}

}*Stud; //

//Function Prototypes

//Integer function that has two parameters one of type Stud and the other one type
// ifstream, and it is passed by reference. Gets all the student data.
int getdata(Stud , ifstream&);

//Function that has two parameters one of type Stud and the other one type
// Cours. This function performs tasks to get rating to the GPA cla .
float getGPA(Stud , Cours);

//This is a simple void function that displays the header to a file
void displayHeader();

//Integer function that has a parameter of type Stud and print all the students data,
//including courses, and gpa to a file
int Print(Stud); //Recursive Function

int main()
{
//Dynamic allocate memory
//Stud Stdata is head first pointer
Stud stData = new Student;

//new is creating new structure

Cours crsData = new Course;

//Variable allows input from file
ifstream fin;

//Opens reading file
fin.open(
"C:\\Users\\jhonj\\Desktop\\pr1data.txt"
);

//Test to make sure the file is available
if(!fin)
{cout << "Error opening data file!" << endl;}
else
{
cout << "Preparing to get data" << endl;

//Function Calls

//Function gets student and courses data
getdata(stData, fin);
cout << "Preparing to get GPA" << endl;
//Function gets gpa
getGPA(stData, crsData);
cout << "Preparing to print" << endl;
//Function displays header
displayHeader();
//Function prints output to a file
Print(stData);
}

return 0;
}

//Integer function that has two parameters. One is of type Stud and the other
//one of type ifstream passed by reference.
//This function gets the students and courses via pointer.
//At the end the function, it calls itself recursively.
int getdata(Stud stdata, ifstream &fin)
{
cout << "Preparing to enter stud info";
//Create test variable for if statement
string test;
fin >> test;

//If statement to test the end of file
if(test == "eof")
{
return 0;
}


stdata->name = test;
cout << "Preparing to print stud name" << endl;
cout << stdata->name;

fin >> stdata->id;
fin >> stdata->numcourses;

//creating a variable of type Cours
Cours crsdata;

//
stdata->CoursePtr = new Course;

crsdata = stdata->CoursePtr;

//Linked list
cout << "Preparing to enter loop" << endl;

//for loop to get the course data
for (int i = 0; i < stdata->numcourses; i++)
{
//
if(crsdata->nextCourse == NULL)
{crsdata->nextCourse = new Course;}

cout << "Preparing to get coursenum" << endl;
fin >> crsdata->coursenum;
cout << crsdata->coursenum << endl;
cout << "Preparing to get grade" << endl;
fin >> crsdata->grade;
cout << crsdata->grade << endl;
cout << "Preparing to get numcredits" << endl;
fin >> crsdata->numcredits;
cout << crsdata->numcredits << endl;
crsdata = crsdata->nextCourse;
}

getGPA(stdata, crsdata); //call to getGPA function


stdata->next = new Student;
stdata = stdata->next;

getdata(stdata, fin); //Recursive call

}

//Void function that has two parameters. One is of type Stud and the other
//one of type Cours.
//This function gets the gpa.
float getGPA(Stud stdata, Cours crsdata)
{
//Constant definition
const int A = 4;
const int B = 3;
const int C = 2;
const int D = 1;
const int F = 0;

int tempgrade = 0;
static int totalPoints = 0;
static int totalCredits = 0;


//for loop to calculate gpa
for(int i = 0; i < stdata->numcourses; i++)
{
//switch statement to set the grade
cout << crsdata->grade;
switch (crsdata->grade)
{
case 'A': tempgrade += (crsdata->numcredits * A);
break;
case 'B': tempgrade += (crsdata->numcredits * B);
break;
case 'C': tempgrade += (crsdata->numcredits * C);
break;
case 'D': tempgrade += (crsdata->numcredits * D);
break;
case 'F': tempgrade += (crsdata->numcredits * F);
break;
}
cout << "preparing to print tempgrade: " << tempgrade << endl;
//calculation to get the totalcredits
totalCredits += crsdata->numcredits;
cout << "Preparing to print credits: " << totalCredits << endl;
//calculation to get the total points
totalPoints += tempgrade;
cout << "Preparing to print points: " << totalPoints << endl;
}

//calculation to get the gpa
return stdata->gpa = (totalPoints/totalCredits);
cout << "Preparing to print GPA: " << stdata->gpa << endl;
}

//This is a simple void function that displays the header in the file
void displayHeader()
{
//Outputting header to the file
cout << "\t\t&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~";
cout << "\n Passaic County Community College";
cout << "\n CIS 161 Students expectation report";
cout << "\n";
cout << "\n Fall -2017 Semester";
cout << "\n\t\t&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~" << endl;
}

//Integer function that will print the student information, courses, and gpa
//having one parameter of type Stud.
int Print(Stud stdata)
{
//if statement to test that there is data in the student structure
//the next time
if(stdata->next == NULL)
{
return 0;
}

//cout << "\nPreparing to print stud info" << endl;

cout << "Student: \t" << stdata->name << endl;
cout << "ID#: \t\t" << stdata->id << endl;
cout << "# of courses: \t" << stdata->numcourses << endl;

//creating a variable of type Cours
Cours crsdata;
//
crsdata = stdata->CoursePtr;

//Linked list
//cout << "\nPreparing to enter loop" << endl;

//for loop to output the course data
for(int i = 0; i < stdata->numcourses; i++)
{
//cout << "Inside course loop" << endl;
cout << "Course: \t" << crsdata->coursenum << endl;
cout << "Grade: \t\t" << crsdata->grade << endl;
cout << "Credits: \t" << crsdata->numcredits << endl << endl;
//pointing to the next student course
crsdata = crsdata->nextCourse;
//cout << i << endl;
}

cout << "GPA: \t\t" << stdata->gpa << endl << endl;

//pointing at the next student structure
stdata = stdata->next;

Print(stdata); //recursive call
}


It reads data from a data.txt file
inside this file I have

James-Davis 1234 2 CIS250 A 3 CIS240 B 3
Juan-Garcia 5899 2 CIS260 B 3 MA120 A 4
eof
float getGPA(Stud stdata, Cours crsdata) {
each student has its own course list, ¿why does this function ask for two parameters?
you also call `getGPA()' in `main()' and in `getdata()', ¿why?

in `main()' the only reference to the courses is Cours crsData = new Course;, so you are passing an empty list
in `getdata()' you've got
1
2
3
4
for(int i = 0; i < stdata->numcourses; i++) {
   //...
   crsdata = crsdata->nextCourse;
}
so you are passing the end node of the list

so both invalid values passed to a function that shouldn't even ask for that parameter.
go back to the drawing board.
Thanks for your comments. I tried to fix my program according to your comments, but I'm still not getting data.

Now my program looks like this:


#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


//Structure named Course that holds the students courses variables;
//one string, character,short, and one pointer to the next course.
//This structure also has a constructor that initializes the pointer
//equal to null.
typedef struct Course // structure definition
{
string coursenum;
char grade;
short numcredits;

//Pointer to the next course
Course *nextCourse;

//Constructor
Course(){
//initializing pointer equal to null
nextCourse = NULL;
}

}*Cours; //

///Structure named Student that holds the students information variables;
//one string, integer,short,float, one pointer to the next student structure,
//and one pointer named CoursePtr of type Course that points to the course
//structure. This structure also has a constructor that initializes the pointers
//equal to null.
typedef struct Student // structure definition
{
string name;
int id;
short numcourses;
float gpa;

//Course Pointer that points to the course structure
Course *CoursePtr;

//Pointer to the next student
Student *next;

//Constructor
Student(){
//initializing pointer equal to null
CoursePtr = NULL;
next = NULL;
}

}*Stud; //

//Function Prototypes

//Integer function that has two parameters one of type Stud and the other one type
// ifstream, and it is passed by reference. Gets all the student data.
int getdata(Stud , ifstream&);

//Function that has two parameters one of type Stud and the other one type
// Cours. This function performs tasks to get rating to the GPA cla .
float getGPA();

//This is a simple void function that displays the header to a file
void displayHeader();

//Integer function that has a parameter of type Stud and print all the students data,
//including courses, and gpa to a file
int Print(Stud); //Recursive Function

int main()
{
//Dynamic allocate memory
//Stud Stdata is head first pointer
Stud stData = new Student;

//new is creating new structure

//Cours crsData = new Course;

//Variable allows input from file
ifstream fin;

//Opens reading file
fin.open(
"C:\\Users\\jhonj\\Desktop\\pr1data.txt"
);

//Test to make sure the file is available
if(!fin)
{cout << "Error opening data file!" << endl;}
else
{
cout << "Preparing to get data" << endl;

//Function Calls

//Function gets student and courses data
getdata(stData, fin);
cout << "Preparing to get GPA" << endl;
//Function gets gpa
getGPA();
cout << "Preparing to print" << endl;
//Function displays header
displayHeader();
//Function prints output to a file
Print(stData);
}

return 0;
}

//Integer function that has two parameters. One is of type Stud and the other
//one of type ifstream passed by reference.
//This function gets the students and courses via pointer.
//At the end the function, it calls itself recursively.
int getdata(Stud stdata, ifstream &fin)
{
cout << "Preparing to enter stud info";
//Create test variable for if statement
string test;
fin >> test;

//If statement to test the end of file
if(test == "eof")
{
return 0;
}


stdata->name = test;
cout << "Preparing to print stud name" << endl;
cout << stdata->name;

fin >> stdata->id;
fin >> stdata->numcourses;

//creating a variable of type Cours
Cours crsdata;

//
stdata->CoursePtr = new Course;

crsdata = stdata->CoursePtr;

//Linked list
cout << "Preparing to enter loop" << endl;

//for loop to get the course data
for (int i = 0; i < stdata->numcourses; i++)
{
//
if(crsdata->nextCourse == NULL)
{crsdata->nextCourse = new Course;}

cout << "Preparing to get coursenum" << endl;
fin >> crsdata->coursenum;
cout << crsdata->coursenum << endl;
cout << "Preparing to get grade" << endl;
fin >> crsdata->grade;
cout << crsdata->grade << endl;
cout << "Preparing to get numcredits" << endl;
fin >> crsdata->numcredits;
cout << crsdata->numcredits << endl;
//crsdata = crsdata->nextCourse;
}

//getGPA(); //call to getGPA function


stdata->next = new Student;
stdata = stdata->next;

getdata(stdata, fin); //Recursive call

}

//Void function that has two parameters. One is of type Stud and the other
//one of type Cours.
//This function gets the gpa.
float getGPA()
{
//Constant definition
const int A = 4;
const int B = 3;
const int C = 2;
const int D = 1;
const int F = 0;

int tempgrade = 0;
static int totalPoints = 0;
static int totalCredits = 0;

Stud stdata;
Cours crsdata;

cout << endl << "grade: " << crsdata->grade << endl;
//for loop to calculate gpa
for(int i = 0; i < stdata->numcourses; i++)
{
//switch statement to set the grade

switch (crsdata->grade)
{
case 'A': tempgrade += (crsdata->numcredits * A);
break;
case 'B': tempgrade += (crsdata->numcredits * B);
break;
case 'C': tempgrade += (crsdata->numcredits * C);
break;
case 'D': tempgrade += (crsdata->numcredits * D);
break;
case 'F': tempgrade += (crsdata->numcredits * F);
break;
}
cout << "preparing to print tempgrade: " << tempgrade << endl;
//calculation to get the totalcredits
totalCredits += crsdata->numcredits;
cout << "Preparing to print credits: " << totalCredits << endl;
//calculation to get the total points
totalPoints += tempgrade;
cout << "Preparing to print points: " << totalPoints << endl;
}

//calculation to get the gpa
return stdata->gpa = (totalPoints/totalCredits);
cout << "Preparing to print GPA: " << stdata->gpa << endl;
}

//This is a simple void function that displays the header in the file
void displayHeader()
{
//Outputting header to the file
cout << "\t\t&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~";
cout << "\n Passaic County Community College";
cout << "\n CIS 161 Students expectation report";
cout << "\n";
cout << "\n Fall -2017 Semester";
cout << "\n\t\t&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~&~" << endl;
}

//Integer function that will print the student information, courses, and gpa
//having one parameter of type Stud.
int Print(Stud stdata)
{
//if statement to test that there is data in the student structure
//the next time
if(stdata->next == NULL)
{
return 0;
}

//cout << "\nPreparing to print stud info" << endl;

cout << "Student: \t" << stdata->name << endl;
cout << "ID#: \t\t" << stdata->id << endl;
cout << "# of courses: \t" << stdata->numcourses << endl;

//creating a variable of type Cours
Cours crsdata;
//
crsdata = stdata->CoursePtr;

//Linked list
//cout << "\nPreparing to enter loop" << endl;

//for loop to output the course data
for(int i = 0; i < stdata->numcourses; i++)
{
//cout << "Inside course loop" << endl;
cout << "Course: \t" << crsdata->coursenum << endl;
cout << "Grade: \t\t" << crsdata->grade << endl;
cout << "Credits: \t" << crsdata->numcredits << endl << endl;
//pointing to the next student course
crsdata = crsdata->nextCourse;
//cout << i << endl;
}

cout << "GPA: \t\t" << stdata->gpa << endl << endl;

//pointing at the next student structure
stdata = stdata->next;

Print(stdata); //recursive call
}
|| foo.cpp: In function ‘float getGPA()’:
foo.cpp|190 col 40| warning: ‘crsdata’ is used uninitialized in this function [-Wuninitialized]
||   cout << endl << "grade: " << crsdata->grade << endl;
||                                ~~~~~~~~~^~~~~
foo.cpp|192 col 29| warning: ‘stdata’ is used uninitialized in this function [-Wuninitialized]
||   for(int i = 0; i < stdata->numcourses; i++) {
||                      ~~~~~~~~^~~~~~~~~~


¿how do you expect to compute the gpa of a student if you never pass that student to the function?
how can i pass that student to the function?
Registered users can post here. Sign in or register to post.