Hi :)

I have this assignment for my first year programming module and one of the questions asks for a void function using string parameters. This confused the hell out of me, but I came up with this idea.

Would someone please, please, please have a look at it and let me know if there are any major problems with it?

I've copied the full three part question but only included the full answer for the third part.

BTW, I know my language seems babyish but this is my first year programming.

Thanks a bunch :)

Question:

QUESTION 5: void FUNCTIONS WITH DIFFERENT TYPES AND NUMBERS OF PARAMETERS
Poppy is the manager of VeryVeryNiceCakes home industry that specialises in cakes. She gives two quotations to
every customer that places an order: one for delivering and the other for pick-up by the customer. The quotations
also depend on the sort of cake and the number of cakes. You have to write a C++ program to help Poppy with this.
The program is developed in three steps. You have to submit printouts for 5c only.

Question 5a: Two reference parameters
Write a void function inputAndValidate where the detail of one order are input and validated. The function should have two reference parameters, namely a parameter storing the sort of cake and a parameter storing the number of cakes. The sort has to be one of the following: chocolate, carrot, custard, fruit or coffee. This parameter is thus of type string. The second parameter is of type int.
The first parameter has to be validated by using a do..while loop: the program should keep on displaying a prompting message until the user enters one of chocolate, carrot, custard, fruit or coffee.
We give the main function below. Test your program to make sure that it works correctly but do not submit any
printouts.

// Assignment 2 Question 5a
#include <iostream>
#include <string>
using namespace std;
// The required function inputAndValidate should be inserted here.
int main( )
{
string sort;
int number;
inputAndValidate(sort, number);
cout << number << " " << sort << " cake(s). " << endl;
return 0;
}

Question 5b: Two value parameters and two reference parameters
The amounts are calculated as follows:
R80 for one fruit cake and R35 per cake for the other sorts of cake,
R100 delivery fee for the complete order if fewer than 10 cakes are ordered,
R8 per cake delivery fee if 10 or more cakes are ordered.

Now write a void function calculateAmounts that calculates two amounts – one amount that is due if the client should pick up the order at the shop and another amount that is due if the order should be delivered. This function has two value parameters supplying the sort of cake and the number of cakes to the function. Furthermore, there are two reference parameters, namely the two amounts that should be calculated and be available to the main function. We give the main function below. You will see that we declare several global constants. You should use them in the function that you write. Test your program but do not submit any printouts.

Question 5c: Final version
Declare another global constant NR_ORDERS and assign the value 7 to it. Now write a new main function containing a for loop going from 1 to NR_ORDERS. The two functions written in 5a and 5b should be called inside the loop and the two amounts should be displayed every time.

My Answer:

//Assignment 2 - Question 5
#include <iostream>
#include <string>
using namespace std;

//Declare global constants.
const int BASIC_PRICE = 35;
const int FRUIT_PRICE = 80;
const int DELIVER_1 = 100;
const int DELIVER_2 = 8;
const int NR_ORDERS = 7;

// void inputAndValidate, calculateAmounts functions.
int calculateAmounts(string & sort, int & number, int & amountSelf, int & amountDeliver)
{
    //First nested if statement for delivered orders.
    if (sort!="fruit" && number>=10)
        amountDeliver = number * BASIC_PRICE + DELIVER_2 * number;
    else if (sort=="fruit" && number>=10)
        amountDeliver = number * FRUIT_PRICE + DELIVER_2 * number;
    else if (sort!="fruit" && number<10)        
        amountDeliver = (number * BASIC_PRICE) + DELIVER_1;
    else if (sort=="fruit" && number<10)
        amountDeliver = (number * FRUIT_PRICE) + DELIVER_1;

    //Second if statement for picked up orders.
    if (sort!="fruit")
        amountSelf = number * BASIC_PRICE;
    else    
        amountSelf = number * FRUIT_PRICE;
}

void inputAndValidate(string & sort, int & number)
{
    do
    {
        cout << "Enter the type of cake ordered in lower case. " << endl;
        cout << "(chocolate, carrot, custard, fruit, coffee): ";
        cin >> sort;
    }
    while(sort!="chocolate" && sort!="carrot" && sort!="custard" && sort!="fruit" && sort!="coffee");

    cout << "Enter the number of cakes ordered: ";
    cin >> number;
}    

int main( )
{
    //Declare int and string variables.
    string sort;
    int number, amountSelf, amountDeliver;

    //for loop for no. of orders.
    for (int i = 1; i <= NR_ORDERS; i++)
    {
        inputAndValidate(sort, number);
        calculateAmounts(sort, number, amountSelf, amountDeliver);

        cout << "If the customer picks up the order: R " << amountSelf;
        cout << endl;
        cout << "If the order should be delivered: R " << amountDeliver;
        cout << endl;
        cout << endl;
    }
    return 0;
}

Does it compile? Does it run? Does it perform and behave like you would expect? If so, good job!

Your posting skills could be improved, though. Look at the anouncements or get a bright light to read the water marks in the message box used to post code. They talk about using code tags to maintain the formatting (indentation, etc) of the code you write when you post it to the board.

Yeah it does run and seems to work but I'm sure there must be a better way to write it.

Sorry about missing out the code tag thing, I must have clicked right into the box. I didn't see it before now. :$ I remember that for the next time.

Thanks

I'm sure there must be a better way to write it.

It looks OK to me. :) I only have one gripe, and it is a very small one:

for (int i = 1; i <= NR_ORDERS; i++)

The idiom for counted loops is an exclusive range of [0..N) instead of an inclusive range of [1..N] to avoid off-by-one errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.