Snack Vending Machine help.

Pages: 12
Hello, everyone. I'm currently trying to program a vending machine for a class that accepts a pin number 5 numbers long (else it'll loop till a proper pin number is entered), clears the screen, displays the options, selecting desired food item by letter, the price comes up and expects the user to input the proper amount till it's added up to the amount needed to pay (if it's over the price, it gives change accordingly). But I'm currently receiving a couple errors, and I have no idea what's wrong. Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdlib>
#include <iostream>

using namespace std;
double beverage = 5.00;
double candy = 2.25;
double hotdog = 7.00;
double popcorn = 6.75;
const int shutdown = 76087;
const int CUSTOMER = 1000;

int main(int pin, int snack, int tender, int counter, double cost)

do
{  
    do
    {
    pin = 0;
    system("cls");   
    cout << "Welcome to Movie Food";
    cout << "\nEnter your 5-digit pin code: ";
    cin >> pin;
    cin.clear(); cin.ignore(10, '\n');
    }
    while(pin < 10000 || pin > 99999);
    if(pin == shutdown){break;}
    do
    {
    snack = '\n';
    system("cls");
    cout << "Select a Snack\n"
    << "B - Beverage $5.00\n"
    << "C - Candy $2.25\n"
    << "H - Hot Dog $7.00\n"
    << "P - Popcorn $6.75\n"
    << "Q - Quit" << endl;
    cin >> snack;
    snack = toupper(snack);
    } 
    while(snack != 'B' && snack != 'C' && snack != 'H' && snack != 'P' && snack != 'Q');
    if(snack == 'Q'){continue;}
    counter++;
    cout << "\nYou selected";
    switch(snack)
    {
                 case 'B': cout << "Beverage" << endl; cost = beverage; break;
                 case 'C': cout << "Candy" << endl; cost = candy; break;
                 case 'H': cout << "Hot Dog" << endl; cost = hotdog; break;
                 case 'p': cout << "Popcorn" << endl; cost = popcorn; break;
    }
    do
    {
                 cout << "Deposit" << cost << "\nEnter amount tendered ";
                 cin >> tender;
                 cin.clear(); cin.ignore(10, '\n');
                 cost = cost - tender;
    }
    while(cost > 0);
    cost = abs(cost);
    if(cost > 0);
    {cout << "Change returned: " << cost << endl;}
    system("PAUSE");
{
    while(counter != CUSTOMER);
    cout << endl;
    cout << "This unit is shutting down to replenish its resources" << endl << endl;
    system("PAUSE");
    return 0;
}
}


I apologize if it looks messy or anything, but I'm still kinda a beginner. The errors in question are:
"expected init-decarator before "do""
and
"expected `,' or `;' before "do""
both on the same line (15).
I'm not expecting someone else to do my homework or anything, but if anyone can tell me why this error happens or what I can do about it it would be much appreciated!
closed account (Dy7SLyTq)
first: line 13 you need a { and second main has a set number of arguements
Ah, thanks alot!
But now I got a new set of errors after doing that:
expected `while' at end of input
expected `(' at end of input
expected primary-expression at end of input
expected `)' at end of input
expected `;' at end of input
expected `}' at end of input
It's also warning me about converting "int" to "double" on a few lines - do I need to worry about this? I think I've read something up that it's not a big deal, but I'm not sure.
I really don't know what it's expecting there, to be honest. I'll go ahead and repost my code with the few changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdlib>
#include <iostream>

using namespace std;
double beverage = 5.00;
double candy = 2.25;
double hotdog = 7.00;
double popcorn = 6.75;
const int shutdown = 76087;
const int CUSTOMER = 1000;

int main(int pin, int snack, int tender, int counter, int cost)
{
do
{  
    do
    {
    pin = 0;
    system("cls");   
    cout << "Welcome to Movie Food";
    cout << "\nEnter your 5-digit pin code: ";
    cin >> pin;
    cin.clear(); cin.ignore(10, '\n');
    }
    while(pin < 10000 || pin > 99999);
    if(pin == shutdown){break;}
    do
    {
    snack = '\n';
    system("cls");
    cout << "Select a Snack\n"
    << "B - Beverage $5.00\n"
    << "C - Candy $2.25\n"
    << "H - Hot Dog $7.00\n"
    << "P - Popcorn $6.75\n"
    << "Q - Quit" << endl;
    cin >> snack;
    snack = toupper(snack);
    } 
    while(snack != 'B' && snack != 'C' && snack != 'H' && snack != 'P' && snack != 'Q');
    if(snack == 'Q'){continue;}
    counter++;
    cout << "\nYou selected";
    switch(snack)
    {
                 case 'B': cout << "Beverage" << endl; cost = beverage; break;
                 case 'C': cout << "Candy" << endl; cost = candy; break;
                 case 'H': cout << "Hot Dog" << endl; cost = hotdog; break;
                 case 'p': cout << "Popcorn" << endl; cost = popcorn; break;
    }
    do
    {
                 cout << "Deposit" << cost << "\nEnter amount tendered ";
                 cin >> tender;
                 cin.clear(); cin.ignore(10, '\n');
                 cost = cost - tender;
    }
    while(cost > 0);
    cost = abs(cost);
    if(cost > 0);
    {cout << "Change returned: " << cost << endl;}
    system("PAUSE");
{
    while(counter != CUSTOMER);
    cout << endl;
    cout << "This unit is shutting down to replenish its resources" << endl << endl;
    system("PAUSE");
    return 0;
}
}
closed account (Dy7SLyTq)
once again take a look at mains args
you need to define pin and snack
What do you mean by that exactly? Sorry - I thought they were already defined?
main may have the following prototypes:

1
2
int main()
int main(int argc, char*argv[]) // or equivalent. 


Your version of main is illegal.

closed account (Dy7SLyTq)
well technically it can be used but you need to use one of the mains above
So do you mean have it like this?

1
2
int main(int pin, int snack, int tender, int counter, int cost)
int main(int argc, char*argv[])


While that did clear up the errors above, I'm back right where I started, but for "int" instead of "do".
closed account (Dy7SLyTq)
no... i confused you. i dont know where you got the idea that you need to have the first. but you cant not have the second
The point is that your main function should have the following signature:

 
int main(int argc, char*argv[])


main is a special function, that is called automatically when you run your program, and the arguments that get passed into it represent the command-line arguments to your program. These are:

argc - the number of items on the command line
argv - an array of strings containing the items on the command line

You seem to be defining main as a function which your code can explicitly call, taking other arguments. This is not legal.

Last edited on
closed account (Dy7SLyTq)
guys its legal under fuction overloading. so dont say its illegal, but i do agree that thats not what he intended to use it for
OK, fair point - maybe it's strictly legal, as long as there's another main function with the correct prototype as well. But it's a terrible idea. Functions named "main" are widely understood to mean something specific - the top-level function that is invoked at the start of execution. Using it for something else is not sensible.
guys its legal under fuction overloading. so dont say its illegal, but i do agree that thats not what he intended to use it for

In fact, main may not be overloaded, and it is illegal.
In fact, main may not be overloaded, and it is illegal.

You're right - a quick Google search got me this StackOverflow post:

stackoverflow.com/questions/5282151/can-we-overload-main-function-in-c

in which a poster quotes 3.6.1p2 of the standard:

An implementation shall not predefine the main function. This function shall not be overloaded.


I should have trusted my initial instinct :)
closed account (Dy7SLyTq)
huh... sorry i was wrong. i didnt think they would make it illegal to overload main. i know its not a good idea but it seems a little extreme
it seems a little extreme
main is a fundamentally important part of the interface between a C++ app and the OS it runs under. Makes sense to me that the standard would forbid you messing with it.
huh... sorry i was wrong. i didnt think they would make it illegal to overload main. i know its not a good idea but it seems a little extreme

I had hoped you'd have learned to check your assumptions before asserting their correctness when they're challenged. Apparently that apple needs to hit you on the head repeatedly.

If all you can say is "I think this is right" when someone is telling you it isn't -- do some flippin' research. A simple trip to google would've set you straight in less than 5 seconds.
closed account (Dy7SLyTq)
ok this is different cire. everyone makes mistakes and if you can overload any other function it would make sense that you can do it to main. you know if you had done "research" you would have realized i never asked what a mortgage was. you probably skimmed and inferred and then went to say i asked for it. so please dont critique me on little mistakes like that which are assumptions based on common sense when you do the same thing
you know if you had done "research" you would have realized i never asked what a mortgage was.

I have no clue what you're talking about.

What I'm talking about is threads you've posted to with incorrect information with regards to the C++ language or related subjects to which I've responded. I don't recall any such thread involving a mortgage.


you probably skimmed and inferred and then went to say i asked for it.

Again, I have no idea what you're talking about.


so please dont critique me on little mistakes like that which are assumptions based on common sense when you do the same thing

If you don't wish to have your incorrect assertions challenged and can't bother to research them yourself when you're told they're wrong, don't post on public forums. And, I most certainly do not do the same thing. It's quite often I'll look up what I think is right prior to posting because I know what I remember is not always 100% accurate.
closed account (Dy7SLyTq)
http://www.cplusplus.com/forum/lounge/95755/6/#msg516940
actually i didnt ask. my point is, we all make mistakes so there is no need to "keep hitting me over the head with the hammer". it was a tiny mistake based off a valid assumption that you can "overload main because doing so would have no effect on main itself".
Pages: 12