Thread: Another class assignment, please help.

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    60

    Another class assignment, please help.

    Here is my assignment and the code I have written so far. Please tell me what else needs to be done. Also, I do not understand sentinal controlled loops.


    CSCI 1010 Programming Assignment 7



    Purpose:
    Write a C++ program that uses strings, reference parameters from Chapter 9 and while loop from Chapter 7 with a sentinel value (see pages 163-165).
    Due Date: The program is due by 8 p.m. on Monday 11/6/06.
    Assignment:
    White a C++ program that will input a student’s name and 3 test grades. It will compute the average grade for the student, write out the student’s name, three test scores, and average grade. The program must use the following 3 functions in addition to the main function:

    1. a void function to read in the student’s name as a string and pass it back as a reference parameter.

    2. a double function that reads in the three test scores ,computes the average and passes back the 3 test scores via reference parameters and the average as the value of the double function.

    3. a void function that displays the output data (student’s name, 3 test grades and average grade) with appropriate labels.

    The program must use a sentinel controlled loop. The program should continue processing students until and uppercase DONE is entered for the student’s name. When DONE is entered, no test scores are to be read or processed. The program is to end.

    D. Sample Run where the bold data was entered by the user



    Enter student’s name or DONE to quit: John Smith

    Enter 3 test scores: 85 90 77

    Student: John Smith

    Three test scores: 85 90 77

    Average grade: 84.00



    Enter student’s name or DONE to quit: Jim Mirth

    Enter 3 test scores: 95 90 79

    Student: Jim Mirth

    Three test scores: 95 90 79

    Average grade: 88.00



    Enter student’s name or DONE to quit: DONE






    Code:
    #include <iostream>
    using namespace std;
    
    void name (string & student);
    //Void function to read name and pass back as ref.
    
    double avg (double & gradea, double & gradeb, double & gradec);
    //Function 2 - computes average and passes back scores via reference par.
    
    void displaystuff (string student, double gradea, double gradeb, double gradec, double averagex);
    //Function 3 - Display the Information.
    
    int main ()
            {//int main open
            string name;
            double gradeone, gradetwo, gradethree, average;
            cout << "Enter Student Name or DONE to Quit:" << endl;
            cin. ignore (80, '\n');
            getline (cin, name);
                    while (name != DONE)
                    {//while loop open
                    cout << "Enter 3 Test Scores:" << endl;
                    cin >> gradeone >> gradetwo >> gradethree;
                    average = avg (gradeone, gradetwo, gradethree);
                    displaystuff (name, gradeone, gradetwo, gradethree, average);
    
                    cout << "Enter Student Name or DONE to Quit:" << endl;
                    cin. ignore (80, '\n');
                    getline (cin, name);
                    }//while loop close
            }//int main close
    
    void name (string & student)
    
    double avg (double & gradea, double & gradeb, double & gradec)
            {//function open
            double averaged, added;
            added = gradea + gradeb + gradec;
            averaged = added/3;
            return averaged;
            }//function closed
    
    void displaystuff (string student, double gradea, double gradeb, double gradec, double averagex)
            {//void function open
            cout << "Student: " << student << endl;
            cout << "Three Test Scores: " << gradea << " " << gradeb << " " << gradec << endl;
            cout << setiosflags (ios::fixed|ios::showpoint) << setprecision (2);
            cout << "Average Grade: " << averagex << endl;
            }//void function close

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    2. a double function that reads in the three test scores ,computes the average and passes back the 3 test scores via reference parameters and the average as the value of the double function.
    I think you should ask the test scores in this function, not in main, otherwise the references have no point.

    name is a std::string. You must compare it against another string. Literal strings go between double quotes ("DONE").

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    i cant do std::string. she hasnt covered it in that way. she requires using namespace std; but thank you for the rest of your post.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    anon is referring to the qualified name of C++ strings, out of habit (I believe). It helps in any conversation since it immediatly establishes we aren't talking about c-style strings.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your name() function doesn't have a body.

    To use std::strings (or C++ style strings, if you prefer), you need to include the header file <string>; likewise, I think setiosflags() is in <iomanip> (Input/Output Manipulation).

    Code:
    int main ()
            {//int main open
            string name;
            double gradeone, gradetwo, gradethree, average;
            cout << "Enter Student Name or DONE to Quit:" << endl;
            cin. ignore (80, '\n');
            getline (cin, name);
    You're ignore()ing before any input functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    i have no idea what any of you just said. im in intro to programming and the prof teaches us bare minimum information.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void name (string & student)
    This function doesn't have a body, so the code won't even compile

    > string name;
    This is the same name as your function.
    Pick more meaningful identifiers, and make sure they don't clash.

    > while (name != DONE)
    As already stated
    while (name != "DONE" )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    AKA make it like this:
    Code:
    void name (string& student)
    {
         cout << "Enter the student's name or DONE to quit:" << endl;
         getline(cin, student);
         cin.ignore(80,'\n');
    }
    Then in main take out this:
    Code:
    cout << "Enter Student Name or DONE to Quit:" << endl;
    cin. ignore (80, '\n');
    getline (cin, name);
    And then replace it with this:
    Code:
    name(name);
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by manutd
    AKA make it like this:
    Code:
    void name (string& student)
    {
    [...]
    And then replace it with this:
    Code:
    name(name);
    No, no, no. Read Salem's 2nd advice
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I know, I just wanted to stick with what he wrote. I he wants to improve his code, that's his job. It probably should be void getstudent (string& student) and studentname.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    Have I made any progress?

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void getname (string & student);
    //Void function to read name and pass back as ref.
    
    double avg (double & gradea, double & gradeb, double & gradec);
    //Function 2 - computes average and passes back scores via reference par.
    
    void displaystuff (string student, double gradea, double gradeb, double gradec, double averagex);
    //Function 3 - Display the Information.
    
    int main ()
            {//int main open
            string name;
            double gradeone, gradetwo, gradethree, average;
            getname (name);
            while (name != DONE)
                    {//while loop open
                    avg (gradeone, gradetwo, gradethree);
                    average = avg (gradeone, gradetwo, gradethree);
                    displaystuff (name, gradeone, gradetwo, gradethree, average);
                    getname (name);
                    }//while loop close
            }//int main close
    
    void getname (string & student)
            {//function open
            cout << "Enter the student's name or DONE to quit:" << endl;
            getline(cin, student);
            cin.ignore(80,'\n');
            }//function close
    
    double avg (double & gradea, double & gradeb, double & gradec)
            {//function open
            cout << "Enter 3 Test Scores:" << endl;
            cin >> gradea >> gradeb >> gradec;
            double averaged, added;
            added = gradea + gradeb + gradec;
            averaged = added/3;
            return averaged;
            }//function closed
    
    void displaystuff (string student, double gradea, double gradeb, double gradec, double averagex)
            {//void function open
            cout << "Student: " << student << endl;
            cout << "Three Test Scores: " << gradea << " " << gradeb << " " << gradec << endl;
            cout << setiosflags (ios::fixed|ios::showpoint) << setprecision (2);
            cout << "Average Grade: " << averagex << endl;
            )

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    1. You need to #include <string>

    2. You don't need 2 calls to avg().

    3. You should probably rename avg(), it's kind of misleading because the real thing it's doing is reading the scores in; the averaging is more of a secondary function.

    4. You might want to make the average also a reference parameter and return it that way; it gets kind of confusing when you're returning some data through references and another piece through return values; usually on functions that return data by references/pointers, the return value is just a success/failure code (or void if it can't fail).

    5. You can combine some statements; for example:
    averaged = (gradea + gradeb + gradec) / 3;
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  13. #13
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    This is not correct:
    Code:
    while (name != DONE
    It should be this:
    Code:
    while (name != "DONE" )
    This has already been stated many times. Also, I would like displaystuff to be something like showstudentinfo. But thats me.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    disregard what was previously here, thanks for hte help im done.
    Last edited by WinterInChicago; 11-06-2006 at 06:31 PM.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    i wish i could edit my first post. i think one of my classmates snatched my code off here and i dont want that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. i need help with errors in my assignment for class
    By azrael13302473 in forum C++ Programming
    Replies: 1
    Last Post: 07-04-2002, 06:02 PM