Errors in my program

Anyone knows how to fix this error: expected unqualified-id before { token.

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
#include <iostream>
#include <conio.h>

using namespace std;

int main();
{
    int 1,2,3,4,5,A;
    
    cout<<" Welcome to Beverage Machine, Choose your drink please. ";
    cout<<" Press 1 for Water, Press 2 for Cola, Press 3 for Orange Juice ";
    cout<<" Press 4 for Lemon Juice and Press 5 for Beer. ";
    cin>>A;
    if (A==1) {
         cout<<" Here's the Water you've ordered. Thank you. ";
    }
    else if (A==2) {
         cout<<" Here's the Cola you've ordered. Thank you. ";
    }
    else if (A==3) {
         cout<<" Here's the Orange Juice you've ordered. Thank you. ";
    }
    else if (A==4) {
         cout<<" Here's the Lemon Juice you've ordered. Thank you. ";
    }
    else if (A==5) {
         cout<<" Here's the Beer you've ordered. Thank you. ";
    else {
         cout<<" The beverage you've chosen is out of stock. Sorry. ";
    }
    getch();
return 0;
}
Last edited on
line 6 should be int main()
when i remove the semicolon there it shows more errors.
In function `int main()':
8expected primary-expression before "int"
8expected `;' before "int"
13`A' undeclared (first use this function)
28expected primary-expression before "else"
28expected `;' before "else"
33expected `}' at end of input
Variables names can't start with a number so int 1,2,3,4,5 isn't legal syntax
Good catch maer. Not even sure why you are declaring those variables since the only one you actually use in your program is A.
Last edited on
I thank you two, maeriden and Raezzor. Now my program runs at last. It only needs endl or \n now. More power :)
There are a few things wrong with your code.

1. the ; following int main()

2. int 1,2,3,4,5 -- These aren't even used and invalid syntax.

I have gone ahead and edited your code into a better and working format, here you go!

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
#include <iostream>
#include <limits>

int main()
{
	using namespace std;

    int nSelection;
    
    cout<< "Welcome to the Beverage Machine, Choose your drink please. ";
    cout<< "Enter 1 for Water, Enter 2 for Cola, Enter 3 for Orange Juice ";
    cout<< "Enter 4 for Lemon Juice or Enter 5 for Beer. " << endl;
    
    cin >> nSelection;
	cout << endl;

	cout << "Here's the ";

	switch(nSelection)
	{
	case 1:
		cout << "Water ";
		break;
	case 2:
		cout << "Cola ";
		break;
	case 3:
		cout << "Orange Juice ";
		break;
	case 4:
		cout << "Lemon Juice ";
		break;
	case 5:
		cout << "Beer ";
		break;
	default:
		cout << "Error! Invalid selection!" << endl << endl;
		main();
		break;
	}

	cout << "you ordered. Thank you!" << endl;

	std::cout << "Press ENTER to continue...";
	std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

	return 0;
}
Last edited on
As TrulyRazor posted, it is better to use a switch statement instead a bunch of else ifs, because a switch statement is much simpler to use (and made for these exact situations).
In our class, we are just in basic hello world program. no if statements or advanced codes are discussed. im just practicing so that i can perform well in the class. thanks guys. now im done studying if statements my next target is that switch statement your talking about. thanks thanks thanks.
Hey guys I got a question, what if I want a confirmation like, if the user input 1 my program will say, are you sure to buy water?, something like that. How am I suppose to do that? thanks in advance.
Here's the program I made previously with a confirm check:

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <limits>

int main()
{
	using namespace std;

    int nSelection;
    
    cout << "Welcome to the Beverage Machine, Choose your drink please. ";

	bool bConfirm = 0;

	while(bConfirm == 0)
	{
    cout << "Enter 1 for Water, Enter 2 for Cola, Enter 3 for Orange Juice ";
    cout << "Enter 4 for Lemon Juice or Enter 5 for Beer. " << endl;
    
    cin >> nSelection;
	cout << endl;

	cout << "Are you sure you want ";

	switch(nSelection)
	{
	case 1:
		cout << "Water";
		break;
	case 2:
		cout << "Cola";
		break;
	case 3:
		cout << "Orange Juice";
		break;
	case 4:
		cout << "Lemon Juice";
		break;
	case 5:
		cout << "Beer";
		break;
	default:
		cout << "Error! Invalid selection!" << endl << endl;
		main();
		break;
	}

	cout << "?" << endl;

	cout << "Enter 0 for No, enter 1 for Yes" << endl;
	cin >> bConfirm;

	cout << endl;
	}


	cout << "Here's the ";

	switch(nSelection)
	{
	case 1:
		cout << "Water ";
		break;
	case 2:
		cout << "Cola ";
		break;
	case 3:
		cout << "Orange Juice ";
		break;
	case 4:
		cout << "Lemon Juice ";
		break;
	case 5:
		cout << "Beer ";
		break;
	default:
		cout << "Error! Invalid selection!" << endl << endl;
		main();
		break;
	}

	cout << "you ordered. Thank you!" << endl << endl;

	std::cout << "Press ENTER to continue...";
	std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

	return 0;
}
Last edited on
Here's an improved version (with a confirm check)

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
#include <iostream>
#include <limits>
#include <string>

int main()
{
	using namespace std;

    int nSelection;
	bool bConfirm = 0;
	string strSelection;
    
    cout << "Welcome to the Beverage Machine, Choose your drink please. " << endl;

	while(bConfirm == 0)
	{
    cout << "Enter 1 for Water, Enter 2 for Cola, Enter 3 for Orange Juice," << endl;
    cout << "Enter 4 for Lemon Juice or Enter 5 for Beer. " << endl;
    
    cin >> nSelection;
	cout << endl;

	switch(nSelection)
	{
	case 1:
		strSelection = "Water";
		break;
	case 2:
		strSelection = "Cola";
		break;
	case 3:
		strSelection = "Orange Juice";
		break;
	case 4:
		strSelection = "Lemon Juice";
		break;
	case 5:
		strSelection = "Beer";
		break;
	default:
		cout << "Error! Invalid selection." << endl << endl;
		main();
		break;
	}

	cout << "Are you sure you want " << strSelection << "?" << endl;
	cout << "Enter 0 for No, Enter 1 for Yes." << endl;

	cin >> bConfirm;

	cout << endl;
	}

	cout << "Here's the " << strSelection << " you ordered. Thank you!" << endl << endl;

	std::cout << "Press ENTER to continue...";
	std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

	return 0;
}
Topic archived. No new replies allowed.