Thread: newbie here.....need someone to look at my code

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1

    Arrow newbie here.....need someone to look at my code

    could someone take a look at my code and tell me what is wrong with it. i cannot figure out why the program gives me an error message. it is supposed to print a table with the male and female students and their grades. any help is appreciated.


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

    using namespace std;

    struct StudRec
    {
    int id;
    char gender;
    float score;
    };

    void ReadData (ifstream, StudRec &);
    void DisplayTable (int, int, float, float);

    void main ()
    {
    ifstream inData;
    StudRec student;
    int countBoys = 0.0, countGirls = 0.0;
    float sumBoys = 0.0, sumGirls = 0.0;
    float maleavg = 0.0, femaleavg = 0.0;

    inData.open("A:\ScoreByGender.txt");

    if (!inData)
    cerr << "File cannot be found." << endl;

    while (!inData.eof()){
    ReadData(inData, student);
    cout << endl << endl;
    }
    if (student.gender = 'M'){
    countBoys ++;
    sumBoys += student.score;
    }
    if (student.gender = 'F'){
    countGirls ++;
    sumGirls += student.score;
    }

    maleavg = sumBoys / countBoys;
    femaleavg = sumGirls / countGirls;

    DisplayTable (countBoys, countGirls, maleavg, femaleavg);
    }

    void ReadData (ifstream inData, StudRec &student)
    {
    inData >> student.id >> student.gender >> student.score;
    }

    void DisplayTable (int cntboys, int cntgrls, float mlavg, float fmavg)
    {
    cout << setw(15) << "number of students" << setw(15) << "average"
    << endl << setw(15) << "------------------" << setw(15)
    << "-------" << endl << "Boys" << setw(15) << setprecision(0)
    << cntboys << setw(15) << mlavg << endl << "Girls" << setw(15)
    << cntgrls << setw(15) << fmavg << endl;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    int countBoys = 0.0, countGirls = 0.0;

    ints are whole numbers..... no decimal places...

    while (!inData.eof()){

    might be better as a do/while loop. You are checking for eof before attempting to read data.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  3. newbie reading code
    By franziss in forum C++ Programming
    Replies: 9
    Last Post: 08-25-2005, 12:18 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM