Thread: Please review and comment

  1. #1
    TruBlu
    Guest

    Please review and comment

    Problem:
    Program allows the user to enter grade information for up to 20
    students in a class.
    Name, Exam 1 grade, Exam 2 grade, Homework grade, Final Exam Grade
    For each student, first calculate a final grade , using the formula:
    finalgrade = 0.20 * Exam 1 + 0.20 * Exam 2 + 0.35 * Homework + 0.25 * Final Exam
    then, assign a letter grade on the basis of 90-100=A, 80-89=B, 70-79=c,
    60-69=d, less than 60=F.
    All the information, including the final grade and the letter grade should be
    written and displayed to a file.
    Note: Program should STOP after 20 loops. Ask the user if they want to continue?
    If the counter < 20, prompt the user, store - Ex. - enter a y to continue or an n
    to quit. You can use a do-while loop condition such as: while counter less than 20
    and proceed equal to 'Y'

    Question for you: How would you handle the exit condition?


    Code:

    #include <fstream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    int main()
    {
    const int MAXSTUDENTS = 20;
    const int MAXNAME = 20;
    const int MAXCHARS = 10;
    char firstname [MAXCHARS] = "allgrades.dat";
    int i;
    char lastname[MAXNAME], lettergrade;
    float exam1, exam2, homework, finalexam, finalgrade;
    ofstream outFile;

    outFile.open(firstname);
    if (outFile.fail())
    {
    cout << "\nNot successful opening " << firstname << endl;
    exit(l);
    }
    outFile << setiosflags(ios::fixed)
    << setiosflags(ios::showpoint)
    << setprecision(2);
    for (i = 1; 1 <= MAXSTUDENTS; i++)
    {
    cout << "\nEnter the student's last name: ";
    cin >> lastname;
    cout << "\nEnter exam 1's grade: ";
    cin >> exam1;
    cout << "\nEnter exam 2's grade: ";
    cin >> exam2;
    cout << "\nEnter the student's homework grade: ";
    cin >> homework;
    cout << "\nEnter the final exam grade: ";
    cin >> finalexam;


    finalgrade = 0.20 * examl + 0.20 * exam2 + 0.35 * homework + 0.25 * finalexam;
    if(finalgrade >= 90) lettergrade = 'A';
    else if (finalgrade >= 80) lettergrade = 'B';
    else if (finalgrade >= 70) lettergrade = 'C';
    else if (finalgrade >= 60) lettergrade = 'D';
    else finalgrade = 'F';


    cout << lastname << " " << examl << " " << exam2 << " " homework
    << " " << finalexam << " " << finalgrade << " " << lettergrade << endl;
    outFile << lastname << " " << examl << " " << exam2 << " " homework
    << " " << finalexam << " " << finalgrade << " " << lettergrade << endl;
    }



    return 0
    }

  2. #2
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    #include <fstream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    int main()
    {
    top:
    const int MAXSTUDENTS = 20;
    const int MAXNAME = 20;
    const int MAXCHARS = 10;
    char firstname [MAXCHARS] = "allgrades.dat";
    int i;
    //Char Quit
    char quit;
    char lastname[MAXNAME], lettergrade;
    float exam1, exam2, homework, finalexam, finalgrade;
    ofstream outFile;

    outFile.open(firstname);
    if (outFile.fail())
    {
    cout << "\nNot successful opening " << firstname << endl;
    exit(l);
    }
    outFile << setiosflags(ios::fixed)
    << setiosflags(ios::showpoint)
    << setprecision(2);
    //for(i=1; 1 ....) should be
    for (i = 1; i <= MAXSTUDENTS; i++)
    {
    cout << "\nEnter the student's last name: ";
    cin >> lastname;
    cout << "\nEnter exam 1's grade: ";
    cin >> exam1;
    cout << "\nEnter exam 2's grade: ";
    cin >> exam2;
    cout << "\nEnter the student's homework grade: ";
    cin >> homework;
    cout << "\nEnter the final exam grade: ";
    cin >> finalexam;


    finalgrade = 0.20 * examl + 0.20 * exam2 + 0.35 * homework + 0.25 * finalexam;
    if(finalgrade >= 90) lettergrade = 'A';
    else if (finalgrade >= 80) lettergrade = 'B';
    else if (finalgrade >= 70) lettergrade = 'C';
    else if (finalgrade >= 60) lettergrade = 'D';
    else finalgrade = 'F';


    cout << lastname << " " << examl << " " << exam2 << " " homework
    << " " << finalexam << " " << finalgrade << " " << lettergrade << endl;
    outFile << lastname << " " << examl << " " << exam2 << " " homework
    << " " << finalexam << " " << finalgrade << " " << lettergrade << endl;
    //added stuff from here
    if(i == 20)
    {
    cout << "Quit? [ Y ]-yes [ N ]- no" << endl;
    cin>>quit;
    switch(quit)
    {
    case y:
    case Y: return 0;
    break;
    case n:
    case N: goto top;
    }
    return 0
    }


    try something like that....and use the [ code ] tags w/o the spaces

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code tags people!

    And, I'd avoid using 'goto', if I were you. It can lead to problems diagnosing bugs, and harder to maintain code. A more conventional loop construct would be better.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    GOTOs ARE EVIL!!

    Or at least that's what my professors have told me for four years... The reason we have loops and conditionals is so we don't need gotos.

    Anyway, this is how I do programs like this. First of all, since the problem asked for up to twenty students, I would use a while loop, although you could use a for loop and break out of it if they want to quit before 20 students.

    Code:
    #include <fstream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    int main()
    {
      const int MAXSTUDENTS = 20;
      const int MAXNAME = 20;
      const int MAXCHARS = 10;
      char firstname [MAXCHARS] = "allgrades.dat";
      int i;
      char lastname[MAXNAME], lettergrade;
      float exam1, exam2, homework, finalexam, finalgrade;
      ofstream outFile;
    
      char quit;  // I'll steal gcn_zelda's variable name!
      int count;  // Number of students so far
    
      outFile.open(firstname);
      if (outFile.fail())
      {
        cout << "\nNot successful opening " << firstname << endl;
        exit(l);
      }
      outFile << setiosflags(ios::fixed)
      << setiosflags(ios::showpoint)
      << setprecision(2);
    
      count = 0;
      quit = 'n';
      while ((count < 20) && (quit == 'n'))
      {
        cout << "\nEnter the student's last name: ";
        cin >> lastname; 
        cout << "\nEnter exam 1's grade: ";
        cin >> exam1;
        cout << "\nEnter exam 2's grade: ";
        cin >> exam2;
        cout << "\nEnter the student's homework grade: ";
        cin >> homework;
        cout << "\nEnter the final exam grade: ";
        cin >> finalexam;
    
        finalgrade = 0.20 * examl + 0.20 * exam2 + 0.35 * homework + 0.25 * finalexam;
        if(finalgrade >= 90) lettergrade = 'A';
        else if (finalgrade >= 80) lettergrade = 'B';
        else if (finalgrade >= 70) lettergrade = 'C';
        else if (finalgrade >= 60) lettergrade = 'D';
        else finalgrade = 'F';
    
        cout << lastname << " " << examl << " " << exam2 << " " homework
        << " " << finalexam << " " << finalgrade << " " << lettergrade << endl;
        outFile << lastname << " " << examl << " " << exam2 << " " homework
        << " " << finalexam << " " << finalgrade << " " << lettergrade << endl;
    
        // Before exiting the loop, prompt the user if they want to continue.
        // To avoid making the user enter 'y' a lot of times, you could say 
        // "Enter student's first name or 'N' to quit", or something like that.
        // (Of course then it would make more sense to have them enter 'Q'!)
        cout << "Continue? (y or n): ";
        cin >> quit;
        cout << endl;
        ++count;  // Don't forget to increment count!
    
      }
    
      outFile.close();  // ALWAYS close your files when you're done!!!
    
      return 0;
    
    }
    You would also have to make the user enter a lowercase 'n' or you could convert their entry to lowercase (or uppercase, whatever you like) so you don't have to check both possibilities.

    (Edited for formatting)
    Last edited by codegirl; 06-16-2003 at 07:37 AM.
    My programs don't have bugs, they just develop random features.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Tsk tsk tsk...

    >> #include <fstream.h>
    >> #include <iomanip.h>
    >> #include <stdlib.h>

    You're using the old standard headers. You should use <fstream>, <iomanip>, and <cstdlib>, which are standard and use the std namespace.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Oops, XSquared is right. Since this is obviously an intro to C++ kind of program, have you learned about the std namespace and the "using" statement? If not, it would be good to look it up since that's now standard C++ programming.
    My programs don't have bugs, they just develop random features.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General Code review
    By twinz1978 in forum C Programming
    Replies: 1
    Last Post: 02-28-2006, 02:15 AM
  2. Review my finished assignment
    By damonbrinkley in forum C Programming
    Replies: 11
    Last Post: 06-24-2003, 07:15 AM
  3. Code review: Bulletproof input
    By Brighteyes in forum C Programming
    Replies: 1
    Last Post: 03-30-2003, 08:31 PM