im trying to get a handle on structs and im having a problem

* this is the actual code the other one was something else sorry
this is what i need help on

#include <iostream>
#include <iomanip>
#include<fstream>
using namespace std;
const int size=20;
struct studentType
{
string firstname[size];
string lastname[size];
int testscore[size];
char lettergrade[size];
}

void print(studentType firstname[], studentType lastname[],studentType testscore[],studentType lettergrade[]);
void init(studentType firstname[], studentType lastname[], studentType testscore[], ifstream& infile);
void letgrade(studentType testscore[], studentType lettergrade[]);
void testhi(studentType firstname[],studentType lastname[],studentType testscore[]);

int main()
{
ifstream infile;
infile.open("e:\\input.txt");


init(firstname, lastname, testscore,infile);
letgrade(testscore, lettergrade);
print(firstname, lastname, testscore,lettergrade);
testhi(firstname, lastname, testscore);
return 0;
}

void init(studentType firstname[], studentType lastname[], studentType testscore[], ifstream& infile)
{
for(int i=0;i<size;i++)
{
infile>>studentType.firstname[i]>>studentType.lastname[i]>>studnetType.testscore[i];
if(testscore>100||testscore<0)
cout<<studentType.firstname[i]<<" "<<studentType.lastname[i]<<"invalid test score"<<endl;
}
}
void letgrade(studentType testscore[], studentType lettergrade[])
{
for(int i=0;i<size;i++)
{
if(studentType.testscore>=90)
studentType.lettergrade[i]='a';
else if(studentType.testscore>=80)
studentType.lettergrade[i]='b';
else if(studentType.testscore>=70)
studentType.lettergrade[i]='c';
else if(studentType.testscore>=60)
studentType.lettergrade[i]='d';
else
studentType.lettergrade[i]='f';
}
}

void testhi(studentType firstname[],studentType lastname[],studentType testscore[])
{
string highstudentf;
string highstudentl;
int highscore=0;
for(int i=0;i<size;i++)
{
if (highscore<studentType.testscore[i])
{
highscore= studentType.testscore[i];
highstudentf=studentType.firstname;
highstudentl=studentType.lastname;
}
}
cout<<setw(10)<<"student with high score is "<<highstudentf<<" "<<highstudentl<<" "<<highscore<<endl;
}

Last edited on
1
2
3
4
5
6
7
struct studentType
{
string firstname[size];
string lastname[size];
int testscore[size];
char lettergrade[size];
}; // need semi-colon 


I don't understand why in each studenttype you have an array of strings? I would've had an array of the struct.

e.g.
1
2
3
4
5
6
int main()

 StudentType myStudents[size];

 return 0;
}
thank you
how do i put that in my code though
That was an illustrative piece of code to show you how to start solving your problem. We don't give out homework answers here :)
i got it
i figured it out...
but do i put that in main or in the struct part thats what i was askin
IMO. You almost need to start your entire prog from scratch. You've got your logic wrong.

You want to have an array of StudentTypes, each with 1 of each parameter associated.

1
2
3
4
5
6
7
8
9
10
11
struct StudentType
 string firstName;
 string etc etc
};

int main() {

 StudentType myStudents[20];

  return 0;
}


You have arrays of objects for no reason. Almost as if you are using arrays for the sake of using arrays.
in my main it says that my variables are undeclared ..
is that wrong too or will that fix when i fix everything else
They don't exist.

You've declared a structure. Which is a blue-print to create a type, but nowhere have you actually created a variable of that type.

So:
1
2
3
4
init(firstname, lastname, testscore,infile);
letgrade(testscore, lettergrade);
print(firstname, lastname, testscore,lettergrade);
testhi(firstname, lastname, testscore);


none of those variables have been declared.
how would i go about doing that
Topic archived. No new replies allowed.