Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
For easy reference, this assignment is described below by using number indexes from 1 to 14.

This is a standard C++ programming assignment using a command line g++ compiler or the embedded one in zyBooks. No GUI forms and no Visual Studio. No external files and no databases are used.
There is only one object-oriented program to complete in this assignment. All code should be saved in a file named ExamScoresUpdate.cpp, which is the only file to submit for grading. No .h files used.
The .cpp file contains main() and two classes, ExamScores and DataCollector. There is no any relationships, including inheritance between these two classes. Their definition and usage are described later starting from item 10.
If you submit a .cpp file which cannot compile by g++ due to syntax errors, you will receive no more than 20% or 10 points for this assignment.
This program, i.e., main(), first lets user enter two sets of scores, set1 and set2, one set a time and one score per line until -999 is entered. Scores are all integers, positive or negative, and can be zero. Each set may contain no score or any number of scores, and they may contain same or different numbers of scores.
Without including exception handling, which will be discussed in Chapter 9, this program assumes user only enters integer scores or 0 or -999. Therefore, input data validation is not necessary.
After the first two sets of scores are entered, the program updates every score of set1 by adding the corresponding score of set2 and display the result. For example, if set1 and set2 each contains three scores, say, { 70, 90, 50 } and { 2, -1, 100 }, respectively, the update (i.e., set1 + set2) changes set1 to { 72, 89, 150 }. See the upper half of the left-hand-side figure below.
It then lets user enter the 3rd set of scores, set3, and uses it to update scores of set1 again and display the result. For example, if set3 is { 1, 0, 33 }, the update (i.e., set1 + set3) changes the above set1 to { 73, 89, 183 }. See the lower half of the left-hand-side figure below.
An important rule for each update is both sets must contain equal number of of scores. If not, an error message is displayed as "<error> Cannot update--score sets have unequal sizes!" See the the upper half of the right-hand-side figure below, where an error occurs because set2 has only one score while set1 has three.
Since we want this to be an object-oriented program with classes and main() and we expect no stand alone functions to be included. Therefore, to support the process of main(), two classes are required, which are ExamScores and DataCollector.
The ExamScores class defines only one private data member, which is a vector of integers. When scores of set1 and set3 are entered, they are supposed to be stored in this data member of two ExamScores objects separately. However, scores of set2 are held directly in a vector of integers in main().
The ExamScores class defines no constructors but five public member functions:
GetScores: it returns the private data member of ExamScores, which is a vector of integers.
EnterScore: it takes an input parameter of integer and adds it to the private data member of ExamScores with no return value.
Update: it takes an input parameter of a vector of integers and, if the vector of integers of the private data member of ExamScores and the input vector have identical size, it adds each integer of the input vector to the integer at the same position of the private data member of ExamScores. If both vectors have unequal sizes, it displays "<error> Cannot update--score sets have unequal sizes!" (see the right-hand-side figure below). It has no return value.
Update: an overloading function of the above Update. It uses an input parameter of an ExamScores object to perform the same as the above Update.
Display: it displays vector of integers of the private data member of ExamScores, one number per line. The display begins and ends with a line of dashes like '---------' and a simple header of 'Scores:' is printed before the first score.
For grading purpose, no other member functions should be included.
The DataCollector class is defined to facilitate user interaction for data input and store them in an ExamScores object or a vector. It has no data members at all but two public overloading member functions called AskUser(), one returns an ExamScores object and the other returns a vector of integers. Because different return types cannot make overloading functions, these two AskUser() must use either different types or different numbers of input parameters. This is a part intended for your own design. Whichever way you choose to make them overloaded, the input parameter(s) is supposed to be used to display the header line like "Exam scores of set 1" or "Exam scores of set 2". After this line, AskUser() starts a loop to prompt user "Enter an integer score or '-99' to end:" to collect score input and store them in an ExamScores object or a vector of integers. The loop ends when user enters -999 and AskUser() returns its ExamScores object or vector of integers.
As a part of requirements of the main(), input of set1 and set3 must use AskUser() which returns an ExamScores object, and input of set2 must use the other AskUser() to return a vector.
To help you to design and test this program, a .exe file for Windows is attached below with two samples of screenshot. You can download the .exe to your Windows and double click it to test run the program.

What I have tried:

C++
/*
Brandon Jones
ExamScoresUpdate program
*/
#include <iostream>
#include <vector>

using namespace std;

// ExamScores

class ExamScores
{
private:
    // the private data member as a vector integer
    // Set1 and set 3 are stored in this member
    vector<int> Exm_scores;

public:

    // return the private data member of ExamScores
    vector<int> GetScores()
    {
        return Exm_scores;
    }

    // Takes input parameter of integers and adds it to private data member
    void EnterScore(int Exm_score)
    {
        // push back is used here to add new element at the end of the vector
        Exm_scores.push_back(Exm_score); // should 999....??????
    }

    // used to test and compare the size of the input parameter vectors and the input vectors
    void Update(vector<int> set)
    {
        // testing and comparing size of both
        if(set.size() == Exm_scores.size())
        {
            // if the size are identical the program starts to add integers of the input vector
            // to integers of same position in the ExamScores.
            for(unsigned int i=0;i<Exm_scores.size();i++)
                Exm_scores.at(i) += set[i];
        }
        // the else here is used if both are not identical
        else
        {
            // if they are not identical a error message is printed to the user!!!
            cout<<"<error> Cannot update--score sets have unequal sizes!"<<endl;
        }
    }

    // this uses the same function above for the upate but it is testing for overloading within the ExamScore
    void Update(ExamScores set)
    {

        if(Exm_scores.size() == set.Exm_scores.size())
        {
            for(unsigned int i=0;i<Exm_scores.size();i++)
                Exm_scores.at(i) += set.Exm_scores.at(i);
            }
            else
            {
                cout<<"<error> Cannot update--score sets have unequal sizes!"<<endl;
            }
    }

    // displaying the Exm_scores results
    void Display()
    {
        cout<<"\n--------------\n Scores:";

        for(unsigned int i=0;i<Exm_scores.size();i++)

        cout<<Exm_scores.at(i)<<"\n"<<endl;
    }
};

// DataCollector
class DataCollector
{

public:

    // function returning ExamScores
    ExamScores AskUser(string header,bool YesorNoExam)
    {
        ExamScores set;
        int Dat_score;


        cout<<header<<endl;

        do{
            // asking user to integer integer
            cout<<" Enter an integer score or '-999' to end:\n ";
            cin>>Dat_score;

            // if statement used to test integer range
            if(Dat_score == -999)
                break;

            set.EnterScore(Dat_score);
            }
            while(Dat_score != -999);

            return set;

    }

    //function return vector of integers
    vector<int> AskUser(string header)
    {
        vector<int> set;
        int Dat_score;

        cout<<header<<endl;

        do{
            cout<<" Enter an integer score or '-999' to end: ";
            cin>>Dat_score;

            if(Dat_score == -999)
                break;

            set.push_back(Dat_score);
            }
            while(Dat_score != -999);

            return set;
    }

};

int main()
{
    DataCollector DataCol;
    ExamScores first_set,third_set;

    // the vector in main that is used to hold members of set 2
    vector<int> second_set;

    first_set = DataCol.AskUser("Exam scores of set 1",true);
    third_set = DataCol.AskUser("Exam Scores of set 3",true);
    second_set = DataCol.AskUser("Exam Scores of set 2");

        cout<<"Set 1 :";

        first_set.Display();
        first_set.Update(second_set);

        cout<<"Set 1 + Set 2: ";

        first_set.Display(); //updated version

        cout<<"Set 3: ";

        third_set.Display();

        cout<<"Set 1 + Set 3: ";

        first_set.Update(third_set);
        first_set.Display();

        return 0;

}

// end of program
Posted
Updated 1-Apr-18 13:24pm
v2
Comments
Patrice T 1-Apr-18 20:23pm    
What make you think it is an infinite loop ?
Where in code ?
KarstenK 2-Apr-18 3:55am    
Have you the debugger tool?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900