I can not figure out how to do this program in C++

Hackett

Beta member
Messages
1
Program Description


You are going to write a computer program to manage used car salespeople. If a salesperson sells 10 cars or sells cars that total more than $50,000, (s)he wins a trip to Wisconsin Dells. A salesperson's sales can be updated many times before his/her totals are requested, so the data needs to be accumulated. You will maintain a list of such sellers using commands to A=add a name, O=output totals, U=update values and Q=quit and give prizes to winners in the list.



Requirements


To get minimal credit for the assignment, your solution must completely work (Differences: NONE) on test1. Besides the normal requirements such as follow the programming ground rules and use good, natural decomposition, you must adhere to the following. Expect to lose up to 15 points if you don't, even if your program gives the right answers. If test1 does not completely work by the Due Date you will lose

2.5 points.



1. You must use the following declarations:



const int MAX_NAME = 20;

const int MAX_SELLERS = 5;

const int NOT_FOUND = -1;

const float GOAL_IN_DOLLARS = 50000;

const float GOAL_IN_CARS = 10;



struct Seller

{

float sales; // running total of sales in dollars

int carsSold; // running total of number of cars sold

char name[MAX_NAME + 1]; // name of the seller

};



struct SellerList

{

int num; // number of salespeople in the list

Seller salespeople[MAX_SELLERS];

};



You must use these exactly as is. You can't change them or add any additional fields.

Failure to comply will result in a loss of 5 points. We suggest you copy/paste these into your program!





2. You are only allowed to manipulate a Seller's fields via the following 4 functions:



void InitSeller ( Seller & s, const char name[] );

// Initializes the Seller's name to name.

// Initializes the Seller's sales and carsSold to 0.0 and 0.



void UpdateSales ( Seller & s, float totalDollars, int numCars );

// Adds the money and number of cars to the seller's accumulated sales and number

// of cars.



bool SellerHasName ( const Seller & s, const char name[] );

// Returns true if the seller's name is the same as name; false otherwise.



bool WinsPrize ( const Seller & s );

// Returns true if the seller sold GOAL_IN_CARS cars or GOAL_IN_DOLLARS sales.



void PrintSales ( const Seller & s );

// Print the salesperson's name, dollar sales, and number of cars sold.



You must properly write and comment these functions. None of the first 4 functions are allowed to print anything or read anything (-5 for each if they do). You will lose 3 points each time you access a Seller's fields outside of these functions, up to a maximum of 15 points. To say this another way:



Suppose you have a Seller called s. You cannot say s.sales or s.name or s.carsSold anywhere except in the five functions listed above.



3. You must have a Search or Find function.



It is passed a SellerList and a name. It determines whether or not a salesperson with the specified name is in the SellerList. If it is, it returns the index of where the seller is stored. If there is no seller in the list with that name, it returns NOT_FOUND. There is more than one way this can be done. No printing is allowed in this function. You will lose 3 points if you use "cout" in this function. You will lose 5 points if you don't have this function.



4. You must have several other functions.



Do a good decomposition with several other functions beyond the five required above.

All functions, including main, must be less 30 lines.



Input and Output Description


You will read and process commands from the standard input, quitting if the Q command is entered. Commands consist of a single character, possibly followed by parameters.

The commands you are to implement are of the form:



A name

O name

U name totalDollars numCars

Q



1. A – Add command. Add a seller with the given name to the end of the list if a seller with that name doesn't already exist and the list isn't full. The name will be a contiguous sequence of non-white-space characters. You can assume that it will be at most MAX_NAME characters in length. You don't need to check this.



2. O – Output command. Output the total value of cars sold and the number of cars sold for that seller. If the seller doesn't exist, print an appropriate message.



3. U – Update command. Update the seller with the given sales and number of cars. If the seller doesn't exist, print an appropriate message (and read and discard the data).



4. Q – Quit command. Print out the list of people who sold enough to win the fabulous vacation.



You don't need to check for bad commands. You can assume that the input formats are okay.

You don't need to do any special formatting of the floating-point numbers that are output.



See the sample output for the exact wordings of all the messages.





Sample Input and Output


You can copy/paste the following input into a file and then bring it into HiC using:

Run / Set Input File

Click on Load Input and then select your file. Also, click on the Input(Interactive) radio button.

Then, when you run your program, it will take the input from what was in the file and run the program, so you won't have to type in the input over and over and over!



Test1 Input:



A Alice

A Bob

U Alice 6000 2

O Alice

U Bob 18000 4

U Bob 4600 3

O Bob

A Charlie

U Alice 37000 2

U Charlie 40000 4

U Bob 9500 5

U Charlie 10600 2

Q



Test1 Output:



A Alice

Alice is seller 0

A Bob

Bob is seller 1

U Alice 6000 2

Alice sold 2 cars for 6000 Dollars

O Alice

Alice: $6000(2 cars)

U Bob 18000 4

Bob sold 4 cars for 18000 Dollars

U Bob 4600 3

Bob sold 3 cars for 4600 Dollars

O Bob

Bob: $22600(7 cars)

A Charlie

Charlie is seller 2

U Alice 37000 2

Alice sold 2 cars for 37000 Dollars

U Charlie 40000 4

Charlie sold 4 cars for 40000 Dollars

U Bob 9500 5

Bob sold 5 cars for 9500 Dollars

U Charlie 10600 2

Charlie sold 2 cars for 10600 Dollars

Q

The contest is over. The winners are:

Bob: $32100(12 cars)

Charlie: $50600(6 cars)





Test2 Input:



A Alice

A Bob

O Charlie

U Charlie 10000 3

A Charlie

A Alice

A Dave

U Dave 65000 12

A Edger

A Fred

U Fred 37000 2

U Charlie 40000 4

Q



Test2 Output:



A Alice

Alice is seller 0

A Bob

Bob is seller 1

O Charlie

Cannot output. Charlie is not in the list.

U Charlie 10000 3

Cannot update. Charlie is not in the list.

A Charlie

Charlie is seller 2

A Alice

Alice is already in the list.

A Dave

Dave is seller 3

U Dave 65000 12

Dave sold 12 cars for 65000 Dollars

A Edger

Edger is seller 4

A Fred

Fred not added. List is full.

U Fred 37000 2

Cannot update. Fred is not in the list.

U Charlie 40000 4

Charlie sold 4 cars for 40000 Dollars

Q

The contest is over. The winners are:

Dave: $65000(12 cars)
 
Its not too hard. Their are only two ways you use the seller struct. One way is when you pass the new sales person info into the InitSeller function. (Which by the way shouldn't be a void declaration, but an integer so you could return a value if the seller was successfully added or an appropriate error). The second way is through the seller list struct which contains the array for the seller struct. So to update results for a salesperson with the name of 'Joe' you would have a search function which went through the SellerList struct passing the seller struct and the name ('Joe') to the SellerhasName function. Once the function returned true you would then pass that seller to the updateSales function.

Let me know if that cleared up your confusion. Remember C/C++ is all about encapsulation.
 
Back
Top Bottom