Problem: Write a pgoramthat uses a structure to store the following information:
Name, IdNum, Test1Score through Test4Score, average, and grade.

function to ask the user for student's name, etc.
The average score should be calculated and stored in the Average member of the structure should be calculated in their own functions.

Course grade should be computed based on the following scale:
91-100 = A
81-90 = B
etc.

I am trying to figure out how to call the information into a function that has the user's input for Test1Score through Test4Score so I can figure out the average.

Can someone assist me? I do not know how to pass data into a function from another function. "I am a newbie!"

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

struct school
{
	string name;
	int IdNum;
	int Test1Score,
		Test2Score,
		Test3Score,
		Test4Score,
		Grade;
	double average;
};
int getInfo(school&);
int getAvg(student, int total);
int main()
{
	school student;

	getInfo(student);
	getAvg(student, total);


	return 0;
}
int getInfo(school &student)
{
	int total = 0;

	cout << "Enter Student's Name: ";
	cin >> student.name;

	cout << "Enter Student's ID: ";
	cin >> student.IdNum;
	
	cin.ignore();
	cout << "Enter Student's score for Test# 1 ";
	cin >> student.Test1Score;

	while(student.Test1Score < 0 || student.Test1Score > 100)
	{
		cout << "Enter grade between 0 & 100: ";
		cin >> student.Test1Score;
	}

	cout << "Enter Student's score for Test# 2 ";
	cin >> student.Test2Score;

	while(student.Test2Score < 0 || student.Test2Score > 100)
	{
		cout << "Enter grade between 0 & 100: ";
		cin >> student.Test2Score;
	}

	cout << "Enter Student's score for Test# 3 ";
	cin >> student.Test3Score;

	while(student.Test3Score < 0 || student.Test3Score > 100)
	{
		cout << "Enter grade between 0 & 100: ";
		cin >> student.Test3Score;
	}

	cout << "Enter Student's score for Test# 4 ";
	cin >> student.Test4Score;

	while(student.Test4Score < 0 || student.Test4Score > 100)
	{
		cout << "Enter grade between 0 & 100: ";
		cin >> student.Test4Score;
	}
	total += student.Test1Score + student.Test2Score + student.Test3Score + 
		     student.Test4Score;

	return total;
}

First off, I'd store the test grades in an array in the struct, not as separate members. That way you can use a loop for the input, saving a lot of repetitive code.

The Grade member should be a char, not an int, if you want to store letter grades.

I'm not completely clear on what your problem is relating to the function(s).
Once your data has been entered, call a function that is passed the struct, and it will access the grade array, calculating the average, storing the average to that data element. You could then call a function that converts average to letter grade, storing that in its data member.

One function can easily pass it argument on to another, especially as you are using pass by reference in your data input function.

Consider

struct T {
  int x;
  double y;
};

void foo( T & arg )
{
    arg.x = 5;
    bar ( arg );
}

void bar ( T & gra )
{
   gra.y = 3.3;
}

The best way is to pass your struct around (you did it by reference into getInfo() which is fine) So change the prototype of getAvg to take a school & also int getAvg(school &, int total); instead of a student (which isn't known about until you made a variable student of type school in main. Just leave the call to getAvg in main() as it is.

I suspect that in time you'll make an array of structs so you can have multiple students but that will require a few name and type changes.

EDIT: V beat me to it

Edited 6 Years Ago by jonsca: n/a

declare struct variable as global it should work.....if it doesnt let me know...the way i see it your function uses this variable but its not declared

declare struct variable as global it should work.....if it doesnt let me know...the way i see it your function uses this variable but its not declared

Go to the board and write one hundred times, "I will not use global variables just to avoid passing them as parameters."

vmanes,

I appreciate the input but we havent learned to put structs into arrays yet so I would get lost trying to do so. I understand about the test score code being reptitive but I do not know how to make a loop for it since the number is in the middle of the variable. any other ideas or anything else on my part to help?

The best way is to pass your struct around (you did it by reference into getInfo() which is fine) So change the prototype of getAvg to take a school & also int getAvg(school &, int total); instead of a student (which isn't known about until you made a variable student of type school in main. Just leave the call to getAvg in main() as it is.

I suspect that in time you'll make an array of structs so you can have multiple students but that will require a few name and type changes.

EDIT: V beat me to it

Jonsca,
I took your advice but I get but I am getting errors that total isnt declared, getAvg doesnt take 2 arguments, among others when I try to change student and school, etc.

vmanes,

I appreciate the input but we havent learned to put structs into arrays yet so I would get lost trying to do so. I understand about the test score code being reptitive but I do not know how to make a loop for it since the number is in the middle of the variable. any other ideas or anything else on my part to help?

I didn't say put the struct in an array, but an array in the struct.

struct foo {
   string name;
   int scores[4];
   double avg;
   char grade;
};

//in your main code
foo student;
....
for( int i = 0; i < 4; i++ )
{
     cout << "enter grade number " << i+1 << ": ";
     cin >> student.scores[i];
}

....

double temp = 0;
for( int i = 0; i < 4; i++ )
{
    temp += student.scores[i];
}
student.avg = temp/4.0;

I took your advice but I get but I am getting errors that total isnt declared, getAvg doesnt take 2 arguments, among others when I try to change student and school, etc.

Probably should repost the code since I don't know where you added and subtracted stuff. Once you get it running it would probably be good to try what vmanes proposed.

Alright this is what I got so far from the help of you 2, thanks by the way. Still cant get it to compile however.

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

struct school
{
	string name;
	int IdNum;
	int scores[4];
	char grade;
	double average;
};
int getInfo(school&);
int getAvg(school);
int main()
{
	school student;

	getInfo(student);
	getAvg(student);

	return 0;
}
int getInfo(school &student)
{
	int total = 0;

	cout << "Enter Student's Name: ";
	cin >> student.name;

	cout << "Enter Student's ID: ";
	cin >> student.IdNum;
	
	cin.ignore();
	for(int i = 0; i < 4; i++)
	{
		cout << "Enter grade for Test " << i + 1 << ": ";
		cin >> student.scores[i];

		while(student.scores[i] < 0 || student.scores[i] > 100)
		{
			cout << "Please a number 0 through 100: ";
			cin >> student.scores[i];
		}
	}

	for(int t = 0; t < 4; t++) 
	{
		total += student.scores[t];
	}

	return total;
}
int getAvg(school student, total)
{
	double average;

	student.average = total /4;

	cout << "Your average is " << student.average << endl;
	
	return student.average;
}

Here are my 3 errors.

1>.\Addendum Problem 1 Course Grade.cpp(62) : error C2061: syntax error : identifier 'total'
1>.\Addendum Problem 1 Course Grade.cpp(66) : error C2065: 'total' : undeclared identifier
1>.\Addendum Problem 1 Course Grade.cpp(70) : warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data

I initialized total in my getInfo function. I am trying to pass that data to getAvg so I can get the average of the the grades.

Your getInfo( ) returns value total. But in main( ), total is not captured, nor are you passing it to the getAvg( ).

getAvg( ) returns the average. Wouldn't it make more sense for the getInfo() to call getAvg(), and store the result in the student's average data member?

int getAvg(school student, total)
{
    ...
}

What type of variable is student ?
What type of variable is total ?

OK I figured it out but now for some reason my get average function is returning some crazy answer after making the division, any idea?
I have tried changing everyting to a double just in case there was an issue there, total = 0;

#include <iostream>
#include <string>
using namespace std; 
struct school
{
	string name;
	int IdNum;	
	int scores[4];
	string grade;	
	double average;
};

int getInfo(school&);
int getAvg(school, int);
string getGrade(school, double);

int main()
{
	school student;
	int total;
	double average;
	string grade;

	total = getInfo(student);
	average = getAvg(student, total);	
	grade = getGrade (student, average);

	cout << "The Student's information has been calculated for the following:\n";
	cout << "Student Name:  " << student.name << endl;
	cout << "ID Number:     " << student.IdNum << endl;
	cout << "Average:       " << student.average << endl;
	cout << "Grade:         " << student.grade << endl;

		
	return 0;
}
int getInfo(school &student)
{	
	int total = 0; 	
	
	cout << "Enter Student's Name: ";	
	getline(cin, student.name);

	cout << "Enter Student's ID: ";	
	cin >> student.IdNum;
	
	for(int i = 0; i < 4; i++)	
	{		
		cout << "Enter grade for Test " << i + 1 << ": ";		
		cin >> student.scores[i]; 		
		
		while(student.scores[i] < 0 || student.scores[i] > 100)		
		{			
			cout << "Please a number 0 through 100: ";			
			cin >> student.scores[i];		
		}	
	} 	
	
	for(int t = 0; t < 4; t++) 	
	{		
		total += student.scores[t];	
	} 	
	
	return total;
}
int getAvg(school student, int total)
{
	student.average = total /4; 	
	
	return student.average;
}
string getGrade(school student, double average)
{
	if(student.average >= 91 && student.average < 100)
	{
		student.grade = "A";
	}

	else if(student.average >= 81 && student.average < 90)
	{
		student.grade = "B";
	}

	else if(student.average >= 71 && student.average < 80)
	{
		student.grade = "C";
	}

	else if(student.average >= 61 && student.average < 70)
	{
		student.grade = "D";
	}

	else if(student.average > 0 && student.average < 60)
	{
		student.grade = "F";
	}

	return student.grade;
}

Enter Student's Name: John Doe
Enter Student's ID: 1234
Enter grade for Test 1: 100
Enter grade for Test 2: 100
Enter grade for Test 3: 50
Enter grade for Test 4: 60
The Student's information has been calculated for the following:
Student Name: John Doe
ID Number: 1234
Average: -9.25596e+061
Grade:
Press any key to continue . . .

Your function signatures should look like the following:

double getAvg(school &, int);
string getGrade(school &, double);

You turned average into a double which was the right thing to do (but see below) but your function was still returning an int. Why you are getting the weird amount (and no grade) was because you need to pass school in by reference to these 2 functions also (since you are modifying student within the method). I did give you a partial glimpse of this back in post #3.

Since total and 4 are integers, essentially what student.average = total /4; does is do the integer arithmetic (with truncation) and then assigns it to the double as such. You need to cast total (or write 4 as 4.0, or both), so student.average = ((double)total)/4.0; will do the trick (I know there are extra parens but just for clarity)

Edited 6 Years Ago by jonsca: n/a

This question has already been answered. Start a new discussion instead.