Thread: help me please

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    12

    help me please

    I have to write a program to compute the class's average and standard deviation from a file which contains a collection of student idds and corresponding scores for my computer class. I am to assign each stedent a letter grade as follows:

    100-90 = A
    89-80 = B
    79-70 = C
    69-60= D
    BELOW 60 = F

    I am to have no less than 7 students and no more than 25 and i have to save the average

    This is what i have come up with so far but I've never did a file and stuff before
    can someone help me to make this correct please

    This is my file containing the ids and scores
    student.dat

    Code:
    #include <fstream.h>
    #include <stdlib.h>
    
    int main()
    {
    const int MAXID =  4;
    const MAXNUMGRADE = 3;
    ofstream out;
    int i = 0, id[MAXID], numgrade[MAXNUMGRADE];
    
    out.open("student.dat");
    if(out.fail())
      {
    	cout << "The output file was not successfully opened" << endl;
    	exit(1);
      }
    do{
    cout << "\nEnter Student ID: ";
    cin >> id;
    
    cout << "Enter student number grade: ";
    cin >> numgrade;
    }whle(i>7 || i<25); // get and write records while greater than 7 and less than 25
    
    // write the file
    out << id << "          " << numgrade << endl;
    
    out.close();
    cout << "\n\nEnd of data input.";
    cout << "\nThe file has been written.\n";
    
    return 0;
    }

    and this is the program that reads student.dat

    students.cpp



    Code:
    #include <fstream.h>
    #include <stdlib.h>
    
    int main()
    {
    const int MAXID =  4;
    const MAXNUMGRADE = 3;
    ofstream out;
    int i = 0, id[MAXID], numgrade[MAXNUMGRADE], total =0;
    char lettergrade;
    float average = 0;
    
    in.open("student.dat", ios::in);
    if(in.fail())
    {
    	cout << "\nThe input file was not successfully opened"
      	        << "\n Please check that the file currently exists."
    	        << endl;
    	exit(1);
    }
    
    cout << "\nStudent ID                     Number Grade       LetterGrade\n";
    cout << "**********************************************";
    
    while( (ch =in.peek()) != EOF)
    {
    in >> id >> numgrade;
    
    if (numgrade >= 90) strcpy(lettergrade,"A");
    else if (numgrade >= 80) strcpy(lettergrade,"B");
    else if (numgrade >= 70) strcpy(lettergrade,"C");
    else if (numgrade >= 60) strcpy(lettergrade,"D");
    else strcpy(lettergrade,"F");
    
    cout << id << "                          " << numgrade << "              " <<  lettergrade << endl;
    }
    for(i=1;i++)
    {
    in >> numgrade;
    numgrade = total;
    total = total + numgrade;
    average = total/i;
    
    cout << " Class average is " << average << endl;
    
    return 0;
    }

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I haven't read through the second piece of code yet, but a quick glance through the first portion caught this...
    Code:
    }whle(i>7 || i<25); // get and write records while greater than 7 and less than 25
    Red - Typo, I assume you cut and paste the code rather than type it all manually into the post.

    Blue - A definate logic error. I hilited the logic error and a portion of the comment that should point you in the right direction.

    It would also help if you were to point out what kind of a problem you are having.

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Code:
    int i = 0, id[MAXID], numgrade[MAXNUMGRADE];
    
    out.open("student.dat");
    if(out.fail())
      {
        cout << "The output file was not successfully opened" << endl;
        exit(1);
      }
    do{
    cout << "\nEnter Student ID: ";
    cin >> id;
    
    cout << "Enter student number grade: ";
    cin >> numgrade;
    Notice the arrays in blue, and then the way you tried to use them with cin in red. Big no, no. That doesn't even compile. Only char arrays can be inputed in that fasion. Definately cutting and pasting job. You don't even have iostream included in either of those sources. No wonder your having problems.
    Last edited by homeyg; 02-08-2005 at 06:21 PM.

Popular pages Recent additions subscribe to a feed