The vending machine C++ code has a problem.

I wanted to call the "tPrice" variable from the "chocoSelect" function to "insertCoin" function, but it does not work.
Same thing as for calling variables "cChoice, qAMB, qSD, qCS, qCMB, currentTotal" from the "chocoSelect" function to the "dispenser" function.

What should I include for the clrscr() to work?

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
    chocoSelect();
    insertCoin();
    dispenser();

    system ("pause");
    return 0;
}

void chocoSelect()
{
    clrscr();

    int cChoice; // Your chocolate selection.
    int qAMB, qSD, qCS, qCMB; // Quantity of All Milk Bar, Sugar Drops, Caramel Surprise, and Chunky Milk Bar respectively.
    double cPrice; // Current price for all chocolates.
    double tPrice = 0.00; // Total price for all chocolates.

    cout << "**************************************" <<endl;
    cout << "            CHOCOLATE MENU            " <<endl;
    cout << "**************************************" <<endl;
    cout << "ITEM \t\t\t Price/each" <<endl;
    cout << "1. All Milk Bar \t $0.25" <<endl;
    cout << "2. Sugar Drops \t\t $0.35" <<endl;
    cout << "3. Caramel Surprise \t $0.50" <<endl;
    cout << "4. Chunky Milk Bar \t $0.75" <<endl<<endl;

    do
    {
    return0: 
    cout << "Make a selection: ";
    cin >> cChoice;

    switch (cChoice)
    {
    case 1: // Choosing "All Milk Bar"
        return1:
        cout << "How many All Milk Bar(s) would you like? (max 5): ";
        cin >> qAMB;

        if (qAMB > 5)
        {
            MoreThan5(); // Calling a function of having a customer entering more than 5.
            goto return1;
        }
        else if (qAMB < 1)
        {
            LessThan1(); // Calling a function of having a customer entering less than 1.
            goto return1;
        }
        else
        {
            cPrice = 0.25 * qAMB;
            tPrice = tPrice + cPrice;
        }

        break;

    case 2: // Choosing "Sugar Drops"
        return2:
        cout << "How many Sugar Drops would you like? (max 5): ";
        cin >> qSD;

        if (qSD > 5)
        {
            MoreThan5(); // Calling a function of having a customer entering more than 5.
            goto return2;
        }
        else if (qSD < 1)
        {
            LessThan1(); // Calling a function of having a customer entering less than 1.
            goto return2;
        }
        else
        {
            cPrice = 0.35 * qSD;
            tPrice = tPrice + cPrice;
        }

        break;

    case 3: // Choosing "Caramel Surprise"
        return3:
        cout << "How many Caramel Surprise(s) would you like? (max 5): ";
        cin >> qCS;

        if (qCS > 5)
        {
            MoreThan5(); // Calling a function of having a customer entering more than 5.
            goto return3;
        }
        else if (qCS < 1)
        {
            LessThan1(); // Calling a function of having a customer entering less than 1.
            goto return3;
        }
        else
        {
            cPrice = 0.50 * qCS;
            tPrice = tPrice + cPrice;
        }

        break;

    case 4: // Choosing "Chunky Milk Bar"
    return4:
        cout << "How many Chunky Milk Bar(s) would you like? (max 5): ";
        cin >> qCMB;

        if (qCMB > 5)
        {
            MoreThan5(); // Calling a function of having a customer entering more than 5.
            goto return4;
        }
        else if (qCMB < 1)
        {
            LessThan1(); // Calling a function of having a customer entering less than 1.
            goto return4;
        }
        else
        {
            cPrice = 0.75 * qCMB;
            tPrice = tPrice + cPrice;
        }

        break;

    default: // If a customer enters other than 1 to 4 will get an error!
        cout << "INVALID SELECTION! Please wait 10 seconds." <<endl;
        system ("pause");
        goto return0;
        break;
    }

    } while (cChoice < 1 && cChoice > 4);

    return;
}

void MoreThan5() // Function for showing a customer entering more than 5.
{
    cout << "You've entered more than 5. Please enter 1 to 5." <<endl<<endl;
}

void LessThan1() // Function for showing a customer entering less than 1.
{
    cout << "You've entered less than 1. Please enter 1 to 5." <<endl<<endl;
}

void insertCoin(double tPrice)
{
    clrscr();

    double purchase = 0.00; // Purchase price
    double currentTotal = 0.00; // Current total inserted
    double change; // Change given
    double choice; // Choose a coin

    purchase = chocoSelect(tPrice);

    cout << "\nINSERT COINS" <<endl;
    cout << "1. 5 cents" <<endl;
    cout << "2. 10 cents" <<endl;
    cout << "3. 20 cents" <<endl;
    cout << "4. 50 cents" <<endl;
    cout << "5. 100 cents" <<endl<<endl;

    do
    {
    cout << "Choose a coin: ";
    cin >> choice;

        switch (choice)
        {
            case 1:
            currentTotal = currentTotal + 0.05;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            case 2:
            currentTotal = currentTotal + 0.10;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            case 3:
            currentTotal = currentTotal + 0.20;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            case 4:
            currentTotal = currentTotal + 0.50;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            case 5:
            currentTotal = currentTotal + 1;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            default:
            cout << "Invalid choice!" <<endl<<endl;
        }

    } while (currentTotal < purchase);

    return;
}

void dispenser(int cChoice, int qAMB, int qSD, int qCS, int qCMB, double currentTotal) // Function for dispensing chocolates from a vending machine
{
    int chocoChoice; // Chocolate selected from the function "chocoSelect".
    int rAMB, rSD, rCS, rCMB; // Remaining number of All Milk Bar(s), Sugar Drops, Caramel Surprise(s), and Chunky Milk Bar(s) dispensed respectively.
    double rTotal; // Remaining total after each item is dispensed.

    rAMB = chocoSelect(qAMB);
    rSD = chocoSelect(qSD);
    rCS = chocoSelect(qCS);
    rCMB = chocoSelect(qCMB);
    rTotal = chocoSelect(currentTotal);
    chocoChoice = chocoSelect(cChoice);

    if (chocoChoice == 1)
    {
        cout << "Your " << rAMB << " All Milk Bar(s) is/are dispensed one at a time." <<endl;

        do
        {
            cout << "All Milk Bar number: " << rAMB <<endl;
            rAMB = rAMB - 1;
            rTotal = rTotal - 0.25;
            cout << "Remaining money: $" << rTotal <<endl;
        } while (rAMB > 0);
    }

    else if (chocoChoice == 2)
    {
        cout << "Your " << rSD << " Sugar Drops is/are dispensed one at a time." <<endl;

        do
        {
            cout << "Sugar Drops number: " << rAMB <<endl;
            rSD = rSD - 1;
            rTotal = rTotal - 0.35;
            cout << "Remaining money: $" << rTotal <<endl;
        } while (rSD > 0);
    }

    else if (chocoChoice == 3)
    {
        cout << "Your " << rCS << " Caramel Surprise(s) is/are dispensed one at a time." <<endl;

        do
        {
            cout << "Caramel Surprise number: " << rAMB <<endl;
            rCS = rCS - 1;
            rTotal = rTotal - 0.50;
            cout << "Remaining money: $" << rTotal <<endl;
        } while (rCS > 0);
    }

    else
    {
        cout << "Your " << rCMB << " Chunky Milk Bar(s) is/are dispensed one at a time." <<endl;

        do
        {
            cout << "Chunky Milk Bar number: " << rCMB <<endl;
            rCMB = rCMB - 1;
            rTotal = rTotal - 0.75;
            cout << "Remaining money: $" << rTotal <<endl;
        } while (rCMB > 0);
    }

    cout << "Thank you for using our machine." <<endl;
    cout << "You have $" << rTotal << "change to colect." <<endl;

    return;
}

Recommended Answers

All 3 Replies

A couple of things I noticed:

Firstly, the compiler has to know how to find the functions before you call them, either have them before main in the file or declare them with prototypes.

Secondly, you're passing data to your functions without telling the function how to receive the data(line 223).

Thirdly, you're trying to use the result of a void function in an assignment statement(line 167).

The only clrscr() function I know of for windows is in the obsolete 'conio.h', which is still supported by some compilers. I think the 'boost' library has a similar function. You can acheive similar results by writing 50 empty lines to the console.

I've revised the code and is clean, but not working.

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
    // Variables from function "chocoSelect".
    int cChoice;
    double tPrice;
    int qAMB, qSD, qCS, qCMB;

    // Variables from function "insertCoin".
    double currentTotal;

    chocoSelect();

    insertCoin(tPrice);

    dispenser(qAMB, qSD, qCS, qCMB, currentTotal, cChoice);

    system ("pause");
    return 0;
}

double chocoSelect()
{
    system ("cls");

    int cChoice; // Your chocolate selection.
    int qAMB, qSD, qCS, qCMB; // Quantity of All Milk Bar, Sugar Drops, Caramel Surprise, and Chunky Milk Bar respectively.
    double cPrice; // Current price for all chocolates.
    double tPrice = 0.00; // Total price for all chocolates.

    cout << "**************************************" <<endl;
    cout << "            CHOCOLATE MENU            " <<endl;
    cout << "**************************************" <<endl;
    cout << "ITEM \t\t\t Price/each" <<endl;
    cout << "1. All Milk Bar \t $0.25" <<endl;
    cout << "2. Sugar Drops \t\t $0.35" <<endl;
    cout << "3. Caramel Surprise \t $0.50" <<endl;
    cout << "4. Chunky Milk Bar \t $0.75" <<endl<<endl;

    do
    {
    return0: 
    cout << "Make a selection: ";
    cin >> cChoice;

    switch (cChoice)
    {
    case 1: // Choosing "All Milk Bar"
        return1:
        cout << "How many All Milk Bar(s) would you like? (max 5): ";
        cin >> qAMB;

        if (qAMB > 5)
        {
            MoreThan5(); // Calling a function of having a customer entering more than 5.
            goto return1;
        }
        else if (qAMB < 1)
        {
            LessThan1(); // Calling a function of having a customer entering less than 1.
            goto return1;
        }
        else
        {
            cPrice = 0.25 * qAMB;
            tPrice = tPrice + cPrice;
        }

        break;

    case 2: // Choosing "Sugar Drops"
        return2:
        cout << "How many Sugar Drops would you like? (max 5): ";
        cin >> qSD;

        if (qSD > 5)
        {
            MoreThan5(); // Calling a function of having a customer entering more than 5.
            goto return2;
        }
        else if (qSD < 1)
        {
            LessThan1(); // Calling a function of having a customer entering less than 1.
            goto return2;
        }
        else
        {
            cPrice = 0.35 * qSD;
            tPrice = tPrice + cPrice;
        }

        break;

    case 3: // Choosing "Caramel Surprise"
        return3:
        cout << "How many Caramel Surprise(s) would you like? (max 5): ";
        cin >> qCS;

        if (qCS > 5)
        {
            MoreThan5(); // Calling a function of having a customer entering more than 5.
            goto return3;
        }
        else if (qCS < 1)
        {
            LessThan1(); // Calling a function of having a customer entering less than 1.
            goto return3;
        }
        else
        {
            cPrice = 0.50 * qCS;
            tPrice = tPrice + cPrice;
        }

        break;

    case 4: // Choosing "Chunky Milk Bar"
    return4:
        cout << "How many Chunky Milk Bar(s) would you like? (max 5): ";
        cin >> qCMB;

        if (qCMB > 5)
        {
            MoreThan5(); // Calling a function of having a customer entering more than 5.
            goto return4;
        }
        else if (qCMB < 1)
        {
            LessThan1(); // Calling a function of having a customer entering less than 1.
            goto return4;
        }
        else
        {
            cPrice = 0.75 * qCMB;
            tPrice = tPrice + cPrice;
        }

        break;

    default: // If a customer enters other than 1 to 4 will get an error!
        cout << "INVALID SELECTION! Please wait 10 seconds." <<endl;
        system ("pause");
        goto return0;
        break;
    }

    } while (cChoice < 1 && cChoice > 4);

    return tPrice;
    return qAMB, qSD, qCS, qCMB;
    return cChoice;
}

void MoreThan5() // Function for showing a customer entering more than 5.
{
    cout << "You've entered more than 5. Please enter 1 to 5." <<endl<<endl;
}

void LessThan1() // Function for showing a customer entering less than 1.
{
    cout << "You've entered less than 1. Please enter 1 to 5." <<endl<<endl;
}

double insertCoin(double tPrice)
{
    system ("cls");

    double purchase = 0.00; // Purchase price
    double currentTotal = 0.00; // Current total inserted
    double change; // Change given
    int choice; // Choose a coin

    purchase = chocoSelect();

    cout << "\nINSERT COINS" <<endl;
    cout << "1. 5 cents" <<endl;
    cout << "2. 10 cents" <<endl;
    cout << "3. 20 cents" <<endl;
    cout << "4. 50 cents" <<endl;
    cout << "5. 100 cents" <<endl<<endl;

    do
    {
    cout << "Choose a coin: ";
    cin >> choice;

        switch (choice)
        {
            case 1:
            currentTotal = currentTotal + 0.05;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            case 2:
            currentTotal = currentTotal + 0.10;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            case 3:
            currentTotal = currentTotal + 0.20;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            case 4:
            currentTotal = currentTotal + 0.50;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            case 5:
            currentTotal = currentTotal + 1;
            cout << "Current Total    : $" << currentTotal <<endl<<endl;
            break;

            default:
            cout << "Invalid choice!" <<endl<<endl;
        }

    } while (currentTotal < purchase);

    return currentTotal;
}

double dispenser(int qAMB, int qSD, int qCS, int qCMB, double currentTotal, int cChoice) // Function for dispensing chocolates from a vending machine
{
    int chocoChoice; // Chocolate selected from the function "chocoSelect".
    int rAMB, rSD, rCS, rCMB; // Remaining number of All Milk Bar(s), Sugar Drops, Caramel Surprise(s), and Chunky Milk Bar(s) dispensed respectively.
    double rTotal; // Remaining total after each item is dispensed.

    rAMB = chocoSelect();
    rSD = chocoSelect();
    rCS = chocoSelect();
    rCMB = chocoSelect();
    rTotal = chocoSelect();
    chocoChoice = chocoSelect();

    system ("cls");

    if (chocoChoice == 1)
    {
        cout << "Your " << rAMB << " All Milk Bar(s) is/are dispensed one at a time." <<endl;

        do
        {
            cout << "All Milk Bar number: " << rAMB <<endl;
            rAMB = rAMB - 1;
            rTotal = rTotal - 0.25;
            cout << "Remaining money: $" << rTotal <<endl;
        } while (rAMB > 0);
    }

    else if (chocoChoice == 2)
    {
        cout << "Your " << rSD << " Sugar Drops is/are dispensed one at a time." <<endl;

        do
        {
            cout << "Sugar Drops number: " << rAMB <<endl;
            rSD = rSD - 1;
            rTotal = rTotal - 0.35;
            cout << "Remaining money: $" << rTotal <<endl;
        } while (rSD > 0);
    }

    else if (chocoChoice == 3)
    {
        cout << "Your " << rCS << " Caramel Surprise(s) is/are dispensed one at a time." <<endl;

        do
        {
            cout << "Caramel Surprise number: " << rAMB <<endl;
            rCS = rCS - 1;
            rTotal = rTotal - 0.50;
            cout << "Remaining money: $" << rTotal <<endl;
        } while (rCS > 0);
    }

    else
    {
        cout << "Your " << rCMB << " Chunky Milk Bar(s) is/are dispensed one at a time." <<endl;

        do
        {
            cout << "Chunky Milk Bar number: " << rCMB <<endl;
            rCMB = rCMB - 1;
            rTotal = rTotal - 0.75;
            cout << "Remaining money: $" << rTotal <<endl;
        } while (rCMB > 0);
    }

    cout << "Thank you for using our machine." <<endl;
    cout << "You have $" << rTotal << "change to colect." <<endl;

    return 1;
}

You're still not listening, the compiler has to read the functions before the code tries to call them. The first call is in main. Therefore, you either have to add prototypes, or move all the functions to the front of the file.

For instance, the prototype for 'MoreThan5()' would like like this:

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
using namespace std;
void MoreThan5();//prototype
int main()
{
    // Variables from function "chocoSelect".
    int cChoice;
    double tPrice;
    int qAMB, qSD, qCS, qCMB;
    // Variables from function "insertCoin".
    double currentTotal;
    chocoSelect();
    insertCoin(tPrice);
    dispenser(qAMB, qSD, qCS, qCMB, currentTotal, cChoice);
    system ("pause");
    return 0;
}

void MoreThan5() // Function for showing a customer entering more than 5.
{
    cout << "You've entered more than 5. Please enter 1 to 5." <<endl<<endl;
}
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.