The Student Room Group

vending machine in c++

hey guys, im a wee bit stuck at the minute, im doing in a foundation degree in computing, im really good at the other subjects but I seem to have not taken every thing on board in relation too c++.

i did the main core of the vending machine, pretty simple, but i need to include a section which asks if the user is the operator and then the operator refills the machine with change and more choclate.

the code i have written so far is but it has quite a few errors in :frown:

any help is appreciated

#include <iostream>
using namespace std;

int choc;
choc = 10;
int pin;
pin = 0;
void choice ()
{
cout <<"Please enter your pin number" << endl;
cin >> pin;
while (pin != 5008)
{
cout <<"Invalid pin number, please re-enter" << endl
cin >> pin;
}
if (pin == 5008)
{
cout << "please refill the chocolate bars by " << choc - 10 << endl;
}
}

int main ()
{

int choice;
int cash;
cash =0;
int choc;
choc = 10;
int change1;
change1 = 0;
int x;
x = 0;
int y;
y = 0;

int change2;
change2 = 300;


cout << "Welcome to Rob's chocolate machine \n \n " << endl;
cout << "This machine only accepts \n5 pence coins \n10 pence coins\n20 pence coins\n50 pence coins\n1 pound(100 pence) coins\n2 pound(200 pence) coins"<< endl;
cout << "If you are the operater, please press 1, otherwise press any other key to continue"<< endl;
cin >> choice;
if (choice == 1)
{
choice();
}

else {
while (y=1)
{

cout << "There are "<< choc << " chocolate bars remaining \n " << endl;
cout << "Chocolate bars are 65 pence \n \n" << endl;
cout << "Please enter your cash : " << endl;
cin >> cash;

while(cash < 65)
{

cout << "Please enter more cash, your current credit is : " << cash << endl;
cin >> x;
cash = cash + x;
}
if (cash >= 65)
{
change1 = cash - 65;
cout << "Thank you for your selection\n" << endl;
cout <<"Please collect your change : " << change1 << endl;
cout << "\nRemaining chocolate bars is : " << choc -1 << endl;
change2 = change2 - change1;
cout << change2<< endl;
cout << "If you would like another chocolate bar, please press 1" << endl;
choc = choc - 1;
cin >> y;
}


}
}
return 0;
}

Scroll to see replies

Reply 1
Would help if we knew what error messages you're getting. :smile:
And PLEASE use code tags next time to preserve indentation :smile:
Reply 3
robcryer
hey guys, im a wee bit stuck at the minute, im doing in a foundation degree in computing, im really good at the other subjects but I seem to have not taken every thing on board in relation too c++.

i did the main core of the vending machine, pretty simple, but i need to include a section which asks if the user is the operator and then the operator refills the machine with change and more choclate.

the code i have written so far is but it has quite a few errors in :frown:

any help is appreciated

#include <iostream>
using namespace std;

int choc;
choc = 10;
int pin;
pin = 0;
void choice ()
{
cout <<"Please enter your pin number" << endl;
cin >> pin;
while (pin != 5008)
{
cout <<"Invalid pin number, please re-enter" << endl//No semicolon
cin >> pin;
}
if (pin == 5008)
{
cout << "please refill the chocolate bars by " << choc - 10 << endl;
}
}

int main ()
{

int choice;
int cash;
cash =0;
int choc;
choc = 10;
int change1;
change1 = 0;
int x;
x = 0;
int y;
y = 0;

int change2;
change2 = 300;


cout << "Welcome to Rob's chocolate machine \n \n " << endl;
cout << "This machine only accepts \n5 pence coins \n10 pence coins\n20 pence coins\n50 pence coins\n1 pound(100 pence) coins\n2 pound(200 pence) coins"<< endl;
cout << "If you are the operater, please press 1, otherwise press any other key to continue"<< endl;
cin >> choice;
if (choice == 1)
{
choice();
}

else {
while (y=1)//Should be ==1
{

cout << "There are "<< choc << " chocolate bars remaining \n " << endl;
cout << "Chocolate bars are 65 pence \n \n" << endl;
cout << "Please enter your cash : " << endl;
cin >> cash;

while(cash < 65)
{

cout << "Please enter more cash, your current credit is : " << cash << endl;
cin >> x;
cash = cash + x;
}
if (cash >= 65)
{
change1 = cash - 65;
cout << "Thank you for your selection\n" << endl;
cout <<"Please collect your change : " << change1 << endl;
cout << "\nRemaining chocolate bars is : " << choc -1 << endl;
change2 = change2 - change1;
cout << change2<< endl;
cout << "If you would like another chocolate bar, please press 1" << endl;
choc = choc - 1;
cin >> y;
}


}
}
return 0;
}



Spotted a couple.
Reply 4
You're giving global variables a value outside a function, which you can't do. Change

int choc;
choc = 10;
int pin;
pin = 0;

to

int choc = 10;
int pin = 0;

You're also defining the integer variable 'choice', but defining a function of the same name. Change all occurrences of the variable 'choice' to something that has no conflicts with previously-defined things. With the edition of a semicolon and the == fix previously suggested (unless I've forgotten something), it compiles fine under g++.
Reply 5
hey guys, really appreciate the help.

i did a bit of work on it(added some of my own stuff which i thought would make it better) and the only issue i have now is when i run the program and enter 1, which goes to the operator section, it works fine, but when i dont enter 1 for the operator, it terminates the program.

Again any help is appreciated, I really wanna learn how to do this properly, and sometimes I feel like my lecturer very difficult too talk too, i think because he has a far superior knowledge off c++ he finds it very difficult too explain the simple stuff to us, thats kinda why im struggling

#include <iostream>
using namespace std;

int choc =10;
int pin =0;
int change2 =300;
int change1 =0;
int refill = 0;
int refill2 =0;
void choice ()
{
cout <<"Please enter your pin number" << endl;
cin >> pin;
while (pin != 5008)
{
cout <<"Invalid pin number, please re-enter" << endl;
cin >> pin;
}
if (pin == 5008)
{
cout << "Please refill the chocolate bars by a maximum of " << choc - 10 << endl;
cin >> refill;
cout << "Thank you, there is currently " << choc + refill << " bars" << endl;
cout << "Curent change remaining is " << change2 << endl;
cout << "Now refill the change without exceeding " << 300 - change2 <<" please" << endl;
cin >> refill2;
change2 = refill2 + change2;
cout << "Current change is " << change2 << endl;
cout <<"Thank you for refilling the machine, this simulation will now exit. " << endl;
}
}

int main ()
{

int choose;
choose = 0;
int cash;
cash =0;
int choc;
choc = 10;
int x;
x = 0;
int y;
y = 0;

cout << "Welcome to Rob's chocolate machine \n \n " << endl;
cout << "This machine only accepts \n5 pence coins \n10 pence coins\n20 pence coins\n50 pence coins\n1 pound(100 pence) coins\n2 pound(200 pence) coins"<< endl;
cout << "If you are the operater, please press 1, otherwise press any other key to continue"<< endl;
cin >> choose;
if (choose == 1)
{
choice();
}

else
{
while (y==1)
{

cout << "There are "<< choc << " chocolate bars remaining \n " << endl;
cout << "Chocolate bars are 65 pence \n \n" << endl;
cout << "Please enter your cash : " << endl;
cin >> cash;

while(cash < 65)
{

cout << "Please enter more cash, your current credit is : " << cash << endl;
cin >> x;
cash = cash + x;
}
if (cash >= 65)
{
change1 = cash - 65;
cout << "Thank you for your selection\n" << endl;
cout <<"Please collect your change : " << change1 << endl;
cout << "\nRemaining chocolate bars is : " << choc -1 << endl;
change2 = change2 - change1;
cout << "If you would like another chocolate bar, please press 1" << endl;
choc = choc - 1;
cin >> y;
}


}
}
return 0;
}
Try again, with [ code ] tags.
Reply 7
robcryer
hey guys, really appreciate the help.

i did a bit of work on it(added some of my own stuff which i thought would make it better) and the only issue i have now is when i run the program and enter 1, which goes to the operator section, it works fine, but when i dont enter 1 for the operator, it terminates the program.



It terminates due to this line: while (y==1)
y starts off as 0 so that loop never gets executed and it goes strait to: return 0;
Reply 8
cheers for that mate.

i changed (y == 1) to (y= 1) and it seems to work, but only when i run the choice function the fist time, if i run the main function, then select to goto the choice function, for some reason it keeps on looping, i have tried my best to look at this logically, but i just cant understand why if it runs firstly, then terminates (which i want it too do), but if i dont select the function first, the function keeps on looping and asking me to re-enter my pin(which i dont want it to do). I know I must sound stupid to u lot, sorry for the stupid questions, its just starting to piss me off though!!!

I will show my current version again :

#include <iostream>
using namespace std;

int choc =10;
int pin =0;
int change2 =300;
int change1 =0;
int refill =0;
int refill2 =0;

void choice ()// this is the function for the operator
{
cout <<"Please enter your pin number" << endl;
cin >> pin;
while (pin != 5008)
{
cout <<"Invalid pin number, please re-enter" << endl;
cin >> pin;
}
if (pin == 5008)
{
cout << "Please refill the chocolate bars by a maximum of " << 10 - choc << endl;
cin >> refill;
cout << "Thank you, there is currently " << choc + refill << " bars" << endl;
cout << "Curent change remaining is " << change2 << endl;
cout << "Now refill the change without exceeding " << 300 - change2 <<" please" << endl;
cin >> refill2;
change2 = refill2 + change2;
cout << "Current change is " << change2 << endl;
cout <<"Thank you for refilling the machine, this simulation will now exit. " << endl;

}
}
int main ()
{
int choose =0;
int cash = 0;
int x = 0;
int y = 0;

cout << "\n\n\t \t Welcome to Rob's chocolate machine \n \n" << endl;
cout << "This machine only accepts \n5 pence coins \n10 pence coins\n20 pence coins\n50 pence coins\n1 pound(100 pence) coins\n2 pound(200 pence) coins"<< endl;
cout << "If you are the operater, please press 1, \notherwise press any other key to continue"<< endl;
cin >> choose;
if (choose == 1)
{
choice();
}

else

{
while (y=1)
{

cout << "There are "<< choc << " chocolate bars remaining \n " << endl; // the main function
cout << "Chocolate bars are 65 pence \n \n" << endl;
cout << "Please enter your cash : " << endl;
cin >> cash;

while(cash < 65)
{

cout << "Please enter more cash, your current credit is : " << cash << endl;
cin >> x;
cash = cash + x;
}
if (cash >= 65)
{
change1 = cash - 65;
cout << "Thank you for your selection\n" << endl;
cout <<"Please collect your change : " << change1 << endl;
cout << "\nRemaining chocolate bars is : " << choc -1 << endl;
change2 = change2 - change1;
cout << "If you would like another chocolate bar, please press 1" << endl;
choc = choc - 1;
cin >> y;
while (y != 1)
{
choice();
}
}
}
}
return 0;
}
Reply 9
Please, please, please - post your code between code tags - [ code ] to start, and [ /code ] to end (without the spaces).

i changed (y == 1) to (y= 1) and it seems to work


The first means check that y equals 1, the second means set y to 1, and return 1 (logically true), so it *always* returns true.
Reply 10
can you explain what u mean by code tags please?

i dont think we have been taught how to use them, unless u mean //
Reply 11
When posting code onto this forum you should begin the snippet with [ code] (remove the space) and finish it with . This allows it to be formatted properly so we can read it better.
Reply 12
Sorry guys first time I have posted code onto any forum and asked for help.

I thought I recognised it from the html stuff that i have done when at college
Reply 13
I don't know loads of C++ but don't you need the

int main()

at the top of the program? underneath:
include <iostream>
using mainspace std
Reply 14
typically, but not if you have a function, im pretty sure off that
Reply 15
King Pieb
I don't know loads of C++ but don't you need the

int main()

at the top of the program? underneath:
include <iostream>
using mainspace std


No you don't. You can either declare functions above it, then do int/void main() to start with or...

you can have all functions made before, and have the last function as main()
Reply 16
I've run it in visual C++, changed what had been said to be changed, and this is the only error message I get
1>c:\users\andrew\documents\uni\object oriented programming for music technology 1\labs\hal_v3_1\hal_v3_1\hal_v3\hal_v2\main.cpp(46) : error C2064: term does not evaluate to a function taking 0 arguments
Reply 17
Use code tags.
Reply 18
The main() function can appear anywhere in your code, so long as it appears once (and only once).
Reply 19
now with code yipee

This is what i have done so far, like i said earlier, the choice function works perfectly when run first, but if selected after i have gone through main the main function then run select the choice function, it stays in a loop instead of terminating

#include <iostream>
using namespace std;

int choc =10;
int pin =0;
int change2 =300;
int change1 =0;
int refill =0;
int refill2 =0;

void choice ()// this is the function for the operator
{
cout <<"Please enter your pin number" << endl;
cin >> pin;
while (pin != 5008)
{
cout <<"Invalid pin number, please re-enter" << endl;
cin >> pin;
}
if (pin == 5008)
{
cout << "Please refill the chocolate bars by a maximum of " << 10 - choc << endl;
cin >> refill;
cout << "Thank you, there is currently " << choc + refill << " bars" << endl;
cout << "Curent change remaining is " << change2 << endl;
cout << "Now refill the change without exceeding " << 300 - change2 <<" please" << endl;
cin >> refill2;
change2 = refill2 + change2;
cout << "Current change is " << change2 << endl;
cout <<"Thank you for refilling the machine, this simulation will now exit. " << endl;

}
}
int main ()
{
int choose =0;
int cash = 0;
int x = 0;
int y = 0;

cout << "\n\n\t \t Welcome to Rob's chocolate machine \n \n" << endl;
cout << "This machine only accepts \n5 pence coins \n10 pence coins\n20 pence coins\n50 pence coins\n1 pound(100 pence) coins\n2 pound(200 pence) coins"<< endl;
cout << "If you are the operater, please press 1, \notherwise press any other key to continue"<< endl;
cin >> choose;
if (choose == 1)
{
choice();
}

else

{
while (y=1)
{

cout << "There are "<< choc << " chocolate bars remaining \n " << endl; // the main function
cout << "Chocolate bars are 65 pence \n \n" << endl;
cout << "Please enter your cash : " << endl;
cin >> cash;

while(cash < 65)
{

cout << "Please enter more cash, your current credit is : " << cash << endl;
cin >> x;
cash = cash + x;
}
if (cash >= 65)
{
change1 = cash - 65;
cout << "Thank you for your selection\n" << endl;
cout <<"Please collect your change : " << change1 << endl;
cout << "\nRemaining chocolate bars is : " << choc -1 << endl;
change2 = change2 - change1;
cout << "If you would like another chocolate bar, please press 1" << endl;
choc = choc - 1;
cin >> y;
while (y != 1)
{
choice();
}
}
}
}
return 0;
}

Latest

Trending

Trending