cola machine

looking for a better way to write this I know I can use functions for each area and I am doing that next but want to know if there is a better way to write this overall. to me it looks kind of messy

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

using namespace std;

void ColaMachine(){
char beverage;
int again = 0;
int tries = 0;
do{
cout << "Please choose a drink" << endl;
cout << "1 = sprite\n2 =coke\n3 =water\n4 = monster\n" << endl;
cin >> beverage;
tries = tries +1;
if (tries == 3){
    cout << "You ran out of tries\nPress any key to exit" << endl;
}

switch(beverage){
case '1':
    cout << "Heres your sprite" << endl;
    break;

case '2':
        cout << "Heres your coke" << endl;
    break;

case '3':
    cout << "Heres your water" << endl;
    break;
case '4':
    cout << "Heres your monster" << endl;
    break;

default:
    cout << "not a valid option" << endl;
    do{
     cout << "try again? 1 = yes or 1 = no ?" << endl;
    cin >> again;

if (again == 2){
    cout << "press any key to exit" << endl;
    }
    else if (again == 1){
    cout << "Shall we" << endl;
    }
    else {
    cout << "not a valid answer" << endl;
    }
        }while (again != 1 || 2);
 }

    }while ((tries <= 3) && (again == 1) || (beverage <= 4));

}





int main()
{
    ColaMachine();

    return 0;
}
Last edited on
Yea you can use else if(){} on that and from personal preference i'd reccomend it but to each their own, and what do you mean by prompt them yes or no elaborate on that a bit more, and this just looks really messy but maybe i feel messy right now, break it into multiple function and have it check tries before each new function is ran (so declare int tries at the top) and use foward declaration to use multiple functions
example
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
#include "stdafx.h"
#include <iostream>
using namespace std;
char a;
int tries = 0;
void machinemenu(); //FOWARD DECLARATION!

void machinemenu2() {
	printf("2");
	tries + 1;
	machinemenu();
}
void machinemenu() {
	if (tries <= 3) {
		printf("1");
		machinemenu2();
	}
	else {
		printf("too many tries");
		cin >> a;
	}
}
int main()
{
	machinemenu();
}


and my comment about the messy code thing is stuff even most advanced people do as everyone likes their code their own way, i prefer mine split up and all and seeing yours with 7 if statements in a switch is just not my style

an example of your code redone with my tips
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
// testinh.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


using namespace std;
string chose;
int tries = 0;
int input;
void machinemenu(); //FOWARD DECLARATION!
int main(); //Another foward dec to restart :p


void machinemenu2() {
	if (input == 1) {
		printf("You chose Sprite");
		tries = tries + 1;
		//put the try again thing here
		system("pause");
	}
	else if (input == 2) {
		printf("You chose Coke");
		tries = tries + 1;
		//put the try again thing here
		system("pause");
	}
	else if (input == 3) {
		printf("You chose Water");
		tries = tries + 1;
		//put the try again thing here
		system("pause");
	}
	else if (input == 4) {
		printf("You chose Monster");
		tries = tries + 1;
		//put the try again thing here
		system("pause");
	}
	else {
		printf("You said %s which is an invalid option, please try again", input);
		main();
	}
}
void machinemenu() {
	if(tries == 4){
		printf("Out of Tries!");
	}
	else {
		printf("Please choose a drink!\n[1]Sprite\n[2]Coke\n[3]Water\n[4]Monster\n");
		cin >> input;
		machinemenu2();
	}
}
int main()
{
	system("cls");
	machinemenu();
}

Last edited on
Topic archived. No new replies allowed.