Soda Machine Program

+3 Jack Mead · July 25, 2015
Hi guys! Made this little program that simulates a soda machine. The only bugs I could find in it is in the beginning. If you type anything else besides an integer, the program will crash.

Anyways, I really wanted to know how I could improve my program. (programming tips, easier ways of doing something, etc.)

#include <iostream>
#include <string.h>
#include <windows.h>

using namespace std;

int CokeMachine();


int main(){
     CokeMachine();
}


int CokeMachine(){

    startover:

    system("color 30");
    system("cls");

    int userinput = 0;
    int stop1 = 0;
    while(stop1 == 0){
        cout << "\nPlease press 1 to begin. ";
        cin >> userinput;
        Beep(450,200);
        if(userinput == 1){
            stop1 = 1;
        }else{
        system("cls");
        }
    }
    string popchoice;
    int userinput1 = 0;
    int stop2 = 0;
    while(stop2 == 0){
    cout << "\n(0 to exit) Choose a beverage. (1)Pepsi  (2)Coke  (3)Root Beer  (4)Orange : ";
    cin >> userinput1;
    Beep(450,200);
    if(userinput1 == 1 || userinput1 == 2 || userinput1 == 3 || userinput1 == 4){
        stop2 = 1;
        }
    if(userinput1 == 1){
        popchoice = "Pepsi";
    }
    if(userinput1 == 2){
        popchoice = "Coke";
    }
    if(userinput1 == 3){
        popchoice = "Root Beer";
    }
    if(userinput1 == 4){
        popchoice = "Orange";
    }
    if(userinput1 == 0){
        goto startover;
    }
    }

    string sizechoice;
    int userinput2 = 0;
    int stop3 = 0;
    while(stop3 == 0){
        cout << "\n(0 to exit) 8floz or 16floz of " << popchoice << "? (1 for 8floz, 2 for 16floz): ";
        cin >> userinput2;
        Beep(450,200);
    if(userinput2 == 1 || userinput2 == 2){
        stop3 = 1;
    }
    if(userinput2 == 0){
        goto startover;
    }
    if(userinput2 == 1){
        sizechoice = "8floz";
    }
    if(userinput2 == 2){
        sizechoice = "16floz";
    }
    }

    double price = 0.00;
    if(userinput1 == 1 && userinput2 == 1 || userinput1 == 2 && userinput2 == 1 || userinput1 == 3 && userinput2 == 1 || userinput1 == 4 && userinput2 == 1){
        price = 0.95;
    }
    if(userinput1 == 1 && userinput2 == 2 || userinput1 == 2 && userinput2 == 2 || userinput1 == 3 && userinput2 == 2 || userinput1 == 4 && userinput2 == 2){
        price = 1.99;
    }

    string amount;
    double total = 0.00;
    int stop4 = 0;
    cout << "\n(0 to exit)" << " " << popchoice << " " << sizechoice << " equals: $" << price << endl;
    while(stop4 == 0){
    change:
    cout << "\n(0 to exit) Please insert change now. " << endl;
    cout << "(N)$0.05  (D)$0.10  (Q)$0.25  (B)$1.00  (5B)$5.00\n";
    cin >> amount;
    if(amount == "N" || amount == "n"){
        total += 0.05;
        system("cls");
        Beep(450,200);
        cout << "Total: $" << total << " --- " << "Cost: $" << price << endl;
        Sleep(100);
            if(total < price){
                goto change;
        }else if(total >= price){
            goto aftchange;
        }
    }else if(amount == "D" || amount == "d"){
        total += 0.10;
        system("cls");
        Beep(450,200);
        cout << "Total: $" << total << " --- " << "Cost: $" << price << endl;
        Sleep(100);
            if(total < price){
                goto change;
            }else if(total >= price){
                goto aftchange;
            }

    }else if(amount == "Q" || amount == "q"){
        total += 0.25;
        system("cls");
        Beep(450,200);
        cout << "Total: $" << total << " --- " << "Cost: $" << price << endl;
        Sleep(100);
            if(total < price){
                goto change;
            }else if(total >= price){
                goto aftchange;
            }
    }else if(amount == "B" || amount == "b"){
        total += 1.00;
        system("cls");
        Beep(450,200);
        cout << "Total: $" << total << " --- " << "Cost: $" << price << endl;
        Sleep(100);
            if(total < price){
                goto change;
            }else if(total >= price){
                goto aftchange;
            }
    }else if(amount == "5B" || amount == "5b"){
        total += 5.00;
        system("cls");
        Beep(450,200);
        cout << "Total: $" << total << " --- " << "Cost: $" << price << endl;
        Sleep(100);
            if(total < price){
                goto change;
            }else if(total >= price){
                goto aftchange;
            }
    }else if(amount == "0"){
        Beep(450,200);
        goto startover;
    }
    }

    aftchange:

        system ("cls");

        int userinput3;

        cout << "Total: $" << total << " --- " << "Cost: $" << price << endl;
        cout << "\nPress (1) to receive change or (0) to exit. ";
        cin >> userinput3;
        Beep(450,200);
        if(total == price && userinput3 == 1){
            cout << "\nChange received: $0.00";
            goto ending;
            }
        if(userinput3 == 1){
            total = total - price;
            cout << "\nChange received: $" << total;
        }else if(userinput3 == 0){
            cout <<"\nChange receive: $" << total;
            Sleep(500); cout <<"\n."; Sleep(500); cout <<"."; Sleep(500); cout <<"."; Sleep(500); cout <<"."; Sleep(500); cout <<".";Sleep(500); cout <<"."; Sleep(500); cout <<"."; Sleep(500);
            goto startover;
        }else{
            goto aftchange;
        }

        ending:
        cout << "\n\nHere is your beverage. Enjoy!" << endl;
        Sleep(500); cout <<"\n."; Sleep(500); cout <<"."; Sleep(500); cout <<"."; Sleep(500); cout <<"."; Sleep(500); cout <<".";Sleep(500); cout <<"."; Sleep(500); cout <<"."; Sleep(500);
        goto startover;








}

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · July 25, 2015
try not to use goto.  instead, use a return code to either loop the function in main or end the program.
0 Adam Tindall · July 25, 2015
You could add in a header file along with another cpp and some classes to shorten the main program.  So having a function for like size then the flavor etc.  That would make it look a little cleaner but I think the base code looks good :) Oh maybe like ending with:

cin.ignore().get();

return 0;

If you just want to end and fresh start

Or

Like mentioned use another function at the end to loop around back to top:

char choice;

do{
... your program goes here

cin >> choice;
} while(choice != 'n');


So yeah whenever you wanted to end the program you just put your last "choice" in there... Should work. hope this helps!
0 Adam Tindall · July 25, 2015
Sorry I meant just break off the classes into the header/cpp
0 Adam Tindall · August 19, 2015
This code is great! Thank you so much I am using it in my little project!

:)
0 Sha Kal · August 19, 2015
I got one question, why is it all in main?
0 Adam Tindall · August 19, 2015
Haha Sha! That is exactly what I was thinking.  It is awesome for all sorts of stuff can just make it faster by making some components into objects out of main maybe?  I am trying to just adjust it into my project and go from there.  Great skelton to work from. 
0 Adam Tindall · August 19, 2015
I am pretty new too but understand about half of evolving c...
0 Adam Tindall · August 19, 2015
I need it for my friends on fb so they understand the pig I am trying to dress up (school checkout program) so they can help me out.
+1 Sha Kal · August 20, 2015
Yeah, you should put into functions as much as possible, not all the things but that's more advanced C, when you should know what goes into main and what doesnt, when you count the speed of program execution time etc etc...but for now try to use functions as much as possible :D
  • 1

C++

106,985 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator