Help with Structures
Apr 25, 2014 at 5:01pm UTC
Hey everyone I need help with structures. In particular this problem I am having. I'm working on a project where take an in_file and compute the average and display the highest average and its number. I compute the average by taking laps(numbers) if one of those numbers is negative instead of displaying the average it should "DQ"(disqualified. Then it must display how many were DQ and how many were qualified. This is my code for far. Any help would be appreciated. Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <fstream>
using namespace std;
struct race{
int num[30];
double lap1;
double lap2;
double lap3;
double lap4;
double lap5;
double lap6;
double lap7;
double lap8;
double average;
double sum;
};
int main()
{
race s[5];
double sum=0;
int num_racers;
bool DQ=false ;
ifstream in_file;
in_file.open("C:\\Temp\\rally.txt" );
in_file>>num_racers;
for (int i=0;i<5;i++)
{
in_file >> s[i].num[30]>>s[i].lap1>>s[i].lap2>>s[i].lap3>>s[i].lap4>>s[i].lap5>>s[i].lap6>>s[i].lap7>>s[i].lap8;
}
for (int i=0; i<num_racers;i++)
{
for (int j=0;j<8;j++)
{
if (s[i].lap>0)
{ DQ=true ;
break ;
}
else
{
s[i].sum+= s[i].lap[j];
}
}
}
for (int i=0;i<5;i++)
{
if (DQ)
{
cout<<s[i].num<<"\t" <<"DQ" <<endl;
}
}
Apr 25, 2014 at 5:51pm UTC
I have looked at my errors and improved the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <iostream>
#include <fstream>
using namespace std;
struct race{
int num[30];
double lap1;
double lap2;
double lap3;
double lap4;
double lap5;
double lap6;
double lap7;
double lap8;
double average;
double sum;
double lap[8];
};
int main()
{
race s[5];
double sum=0;
int num_racers;
bool DQ=false ;
ifstream in_file;
in_file.open("C:\\Temp\\rally.txt" );
in_file>>num_racers;
for (int i=0;i<5;i++)
{
in_file >> s[i].num[i]>>s[i].lap1>>s[i].lap2>>s[i].lap3>>s[i].lap4>>s[i].lap5>>s[i].lap6>>s[i].lap7>>s[i].lap8;
if (s[i].lap1<0||
s[i].lap2<0 ||
s[i].lap3<0 ||
s[i].lap4<0 ||
s[i].lap5<0 ||
s[i].lap6<0 ||
s[i].lap7<0 ||
s[i].lap8<0)
cout<<s[i].num[i]<<" DQ" <<endl;
else
{
s[i].average=(s[i].lap1+s[i].lap2+s[i].lap3+s[i].lap4+s[i].lap5+s[i].lap6+s[i].lap7+s[i].lap8)/8;
cout<<s[i].num[i]<<" " <<s[i].average<<endl;
}
}
in_file.close();
return 0;
}
Topic archived. No new replies allowed.