struct help

I have the following program below I have the output but i dont know how to get it. The out put gives the names Jane Doe and Joe Public along with their GPA's per semester. I just need help explaining (detailed) what happens in each line. Especially what the period is between words. Your help is much appreciated

struct StudRec
{
string name;
float semgpas[14];
int enrolled;
:
StudRec jane,joe;
int i;
jane.name = "Jane Doe";
for (i=0; i < 12; i++)
jane.semgpa[i] = 4.00 - i/100.0;
jane.enrolled = 2004;
joe.enrolled = jane.enrolled + 2;
joe.name = "Joe Public";
for (i=6; i>=0; i--)
joe.semgpas[6-i] = jane.semgpas[i];
cout<<fixed<<setprecision(2);
cout<<"Student Name: "<<joe.name<<endl;
cout<<"Semester-by-semester gpas"<<endl;
for (i=0; i < 7; i++)
cout<<joe.semgpas[i]<<" ";
cout<<"Year enrolled: "<<joe.enrolled<<endl<<endl;
cout<<"Student Name: "<<jane.name<<endl;
cout<<"Semester-by-semester gpas"<<endl;
for (i=0; i < 12; i++)
cout<<jane.semgpas[i]<<" ";
cout<<"Year enrolled: "<<jane.enrolled<<endl<<endl;
Last edited on
This declares a structure with a bunch of member objects. It's a way of keeping data organized nicely.
1
2
3
4
5
6
struct StudRec
{
    string name;
    float semgpas;
    ...
}


Here you are declaring two objects that have the type of the structure. They are named jane and joe.
StudRec jane,joe;

The dot will let you access an element in the structure. If you want to set the name, then do something like:
jane.name = "Jane Doe";

For more information see:
http://cplusplus.com/doc/tutorial/structures/
closed account (4z0M4iN6)
Maybe Stewbond, you should write your example correct?

Correct would be (don't forget the semicolon at the end):

1
2
3
4
5
6
struct StudRec
{
    string name;
    float semgpas;
    ...
};


And if you want to declare jane and joe, you have to write:

struct StudRec jane, joe;

But you could also write:

StudRec jane,joe;

If you define a typedef:

1
2
3
4
5
6
typedef struct _StudRec
{
    string name;
    float semgpas;
    ...
} StudRec;

dadabe wrote:
struct StudRec jane, joe;

In C++ we often leave out the struct keyword here.


dadabe wrote:
1
2
3
4
5
6
typedef struct _StudRec
{
    string name;
    float semgpas;
    ...
} StudRec;

This is how you do it in C. In C++ it's not needed.
Last edited on
I guess the question was misunderstood. I was given this program. I did not write it. I need it to be broken down to me step by step what is going on because as of now I am lost. I cant modify the program I just need to interpret the output. I have it already but I want to know how we get the the output because I am confused
closed account (4z0M4iN6)
I fear, nobody understands this code, especially the colon after "int enrolled;" and the code after the colon. You should look, wether your example is really the same, what you posted here.
it is we just need to understand the concept. We dont need to get an output from a compiler. We just need to get the general idea
Topic archived. No new replies allowed.