# How to calculate In a Structure?

Page 1 of 1

## 2 Replies - 1281 Views - Last Post: 30 March 2010 - 06:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=165234&amp;s=d6325558dcafcc553f6c1cce2136d81f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Djr777

• New D.I.C Head

Reputation: 0
• Posts: 15
• Joined: 30-March 10

# How to calculate In a Structure?

Posted 30 March 2010 - 05:35 PM

Coding a Record keeping program for my College Course assignment it writes the file out into a file in binary..
I have it almost finished. But i cant figure out how to Average the GPA of the Records that the user inputs. Heres my Code.. I would appreciate some help.
The GPA is in a Structure, And i enter different records. And im not exactly sure how Average out each records GPA..
I need to Average the GPa from all the records that the user Creates. And i cant figure out how? I probably have to do it somewhere in the File that i output it to, Which is still am not sure how to do, I would appreciate some help, Not looking for Exact code or whatnot, just dont know where to start. Thanks.

```//
//CSC250 Sec1
//
// This program allows the user to enter in there own records, and edit + view them again.

#include<iostream>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;

//Stucture for the information in a file.

const int SIZE = 40;
struct Recordinfo
{
char name[SIZE];
int idnum;
float gpa;
};

//Function Prototypes
void CreateRecords(fstream &stud);
void UserInfo(fstream &stud);
void ShowRecords(fstream &stud);
void Findgpa(fstream &stud);

int main()
{

fstream stud;

//Function Calls.
CreateRecords(stud);

ShowRecords(stud);
UserInfo(stud);
Findgpa(stud);

system("pause");

return 0;

}//main

void CreateRecords(fstream &stud)
{

int go =7;
int i = 0;

Recordinfo Info;
stud.open("student.dat",ios::out | ios::binary);
if(stud.fail())
stud.open("student.data",ios::out | ios::binary);

cout<<"****CREATE A RECORD****"<<endl;
cout<<""<<endl;
do{

cout<<"Please Enter The Person's Name: ";
cin.getline(Info.name,SIZE);
cout<<""<<endl;
cout<<"Please Enter "<< Info.name<<"'s ID #: ";
cin>>Info.idnum;
cout<<""<<endl;
cout<<"Please Enter " <<Info.name <<"'s GPA: ";
cin>>Info.gpa;
cout<<""<<endl;
stud.seekp(0L,ios::end);
stud.write(reinterpret_cast<char *>(&Info),sizeof(Info));

cout<<"Hit 7 to stop entering, Or any other # to keep Enter Records"<<endl;
cin>>go;
cout<<""<<endl;
cin.ignore();

}while(go!=7);
stud.close();

return;
}

void ShowRecords(fstream &stud)
{
stud.clear();
Recordinfo Info;
int go;
int recordnum=0;
int choices;
long size;
int numgpa;
double avggpa;

//Open file for input to screen
stud.open("student.dat",ios::in | ios::binary);
//Test for file Error.

if(!stud)
{
cout << "Error opening file. Program exiting!";
exit(0);
}

do
{
stud.seekg(0L,ios::end);
size =stud.tellg();

recordnum = size/(sizeof(Info));
stud.seekg(0L,ios::beg);

cout<<"There are " <<recordnum<<" files"<<endl;
cout<<""<<endl;
cout<<"Which File would you like to Search For? 0 is File #1...: ";
cin>>choices;
stud.seekg((choices*sizeof(Info)),ios::beg);
stud.read(reinterpret_cast<char *>(&Info),sizeof(Info));
cout<<""<<endl;
cout<<"**************************************"<<endl;
cout<<""<<endl;
cout << "Name :"<<Info.name<<endl;
cout << "ID #:"<<Info.idnum<<endl;
cout << "GPA :"<<Info.gpa<<endl;
cout<<""<<endl;
cout<<"***************************************"<<endl;
cout<<"Would you like to Search for another, Hit 7 for no, any other # for yes:";
cin>>go;
}while (go!=7);

stud.close();

return;
}

void UserInfo(fstream &stud)
{
int more=0;

cout<<""<<endl;
cout<<"Would you Like to ENTER anymore Records?Press 7 If yes:";
cin>>more;
cin.ignore();
if(more ==7)
CreateRecords(stud);

cout<<""<<endl;

return;
}

void Findgpa(fstream &stud)
{

return;

}
```

Is This A Good Question/Topic? 0

## Replies To: How to calculate In a Structure?

### #2 hlemke

• D.I.C Head

Reputation: 15
• Posts: 57
• Joined: 18-January 10

## Re: How to calculate In a Structure?

Posted 30 March 2010 - 05:58 PM

Not 100% sure if this is what you want, but couldn't you just use an accumulator variable which gets each GPA added to it as you go, and a counter variable that gets one added to it for each new record, and then divide the former by the latter? This would be done in your program, not the file you're writing to, but if I understand your problem correctly, that shouldn't matter.

This post has been edited by hlemke: 30 March 2010 - 05:59 PM

Was This Post Helpful? 1

### #3 Djr777

• New D.I.C Head

Reputation: 0
• Posts: 15
• Joined: 30-March 10

## Re: How to calculate In a Structure?

Posted 30 March 2010 - 06:17 PM

Thanks A LOT hlemke, Even though i feel stupid for not being able to figure that out haha. But seriously Thanks =].
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }