Getting specific data from file and calculating them

Im trying out program this problem:
We, the instructors of CSI course, would like to know how well male and female students perform in test 1.
You receive a file that contains female and male students’ marks. Due to confidentiality, the letter code ‘f’
is used for female students and ‘m’ for male student. Every line entry in the input file represents details of
one student and consists of a letter code followed by a mark. The number of students (class size) is to be
input by the user and has to be a positive integer between 0 and 101 otherwise no output is produced (see
first sample output). Write a program that computes and outputs the number of male and female
students, the average mark for female and male students, and the overall average mark in Test1. Format
your output to 2 decimal places.

this was my approach:
""""
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream indata;

char gender,f,m;
int grade,clsize,nuoF,nuoM;
float avg;

indata.open("lab7Input.txt");

cout<<fixed<<showpoint;
cout<<setprecision(2);

indata>>gender>>grade;

cout<<"Enter class size between 0 and 101: ";
cin>>clsize;
indata>>gender;
indata>>grade;
nuoF=f++;
nuoM=m++;
cout<<"the course has "<<nuoF<<"female students, and"
<<nuoM<<"students."<<endl;
cout<<"The average performance of female student is: ";
if(gender==f)
avg=grade/nuoF;
cout<<avg;
cout<<endl;

cout<<"The average performance of male student is: ";
if(gender==m)
avg=grade/nuoF;
cout<<avg<<endl;

cout<<"The average performance of all students is: ";
avg=grade/(nuoF+nuoM);
cout<<avg<<endl;
indata.close();

return 0;

}
""""

the input file is in this format:

m 23
m 35
f 12
m 24
f 30
etc..

and it keeps on going, not sure whts my error,
the ouput screens is formatted right but the results are screwed up :/


please help out, kinda urgent :P
+++
I need another program with the same function but using switch instead of else if
and in this program i need to make it show as part of the result the highest mark and who got it :/
Last edited on
One thing I don't see in your code is a loop structure of any form.

For example:
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
#include <iostream>
#include <fstream>

int main()
{
     using namespace std;
     
     ifsteam indata;
     int mydata;
     int mydata1;
     
     // open the file.

     indata.open("MyFile.txt")
     while (!indata.eof())  /// watch for the end of file.
     {
           // read the data
           indata >> mydata >> mydata1;
           // process data
           int Enddata = mydata + mydata1;
           cout << "Result= " << Endata << endl;
           
     }
      
}

Last edited on
i tried it ur way in many ways it didnt work out :(
this is my best try at it:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream indata;

char gender,f,m;
indata.open("lab7Input.txt");
float avg=0;
int grade=0,clsize,nuoF=0,nuoM=0,sum=0;f=1;m=1;
indata>>gender;
indata>>grade;

cout<<fixed<<showpoint;
cout<<setprecision(2);



cout<<"Enter class size between 0 and 101: ";
cin>>clsize;
cout<<endl;
while(indata)
{nuoF=nuoF+f;
nuoM=nuoM+m;
continue;
}
cout<<"the course has "<<nuoF<<"female students, and "<<nuoM<<"male students."<<endl;

cout<<"The average performance of female student is: ";
while (!indata.eof()&&(gender=f))
{
{
grade=grade++;
avg=grade/nuoF;
continue;
}
cout<<avg<<endl;
}

cout<<"The average performance of male student is: ";
while (!indata.eof()&&(gender='m'))
{
{ sum=grade++;
avg=sum/nuoM;
cout<<avg<<endl;
continue;
}
cout<<avg<<endl;
}
cout<<"The average performance of all students is: ";
avg=sum/(nuoF+nuoM);
cout<<avg<<endl;

indata.close();

return 0;


}

thx for your previous contribution im greatly thankful
Logic flow looks like this.
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

#include <iostream>
#include <fstream>
#include <iomanip>

int main()
{
     ifstream indata;

     float avg=0;
     int grade=0; 
     int clsize =0;
     int nuoF=0;
     int nuoM=0
     int sum=0;
     int f=1;
     int m=1;

     char gender;
     using namespace std;
     
     bool ok = false;
     while (!ok)
     {
           cout << "Enter the number of students?" << endl;
           cin >> clsize;
           if((clsize > 0)&&(clsize < 102))
           {
                ok = true;
            }
     } 

     indata.open("lab7Input.txt");
     
     // the loop for reading the file..
     for(int count = 1; count <= clsize; count++)
     {
           // read in from the file.
           indata >> gender >> grade;
           // count the gender
           if(gender == 'm')
                 nofM++;
           else
                 nofF++;
           
           // update the running totals.
            sum+= grade;
            avg = sum / clsize;
        
      }
// when I reach this point all my totals should be what I expect them to be.
// ... I am not going to put in the print out of the information. 


From what I read this is what you are tring to do in a nut shell. This will read all elements for the file and process the data for the given problem... I am not trying to do your home work for you.
Last edited on
im not trying to get you to do it i just need the required information to do it ;/

thx so much ill do the rest myself
Topic archived. No new replies allowed.