Need Help With C++ Practice Program

Sorry in advance if the code is messy

Okay so Ive been exercising my C++ skills a little bit lately and I have gone about making a soda machine. Its gone great. But when I had my friend test it out. they wanted to buy another soda. I hadnt yet thought of that but when they suggested it, I started implementing it. (Im sure there was a simpler way of doing so) So I had started doing it and finally got it to work. But there was one thing bugging me. I couldnt get the value of the variable "balance" or "new_balance" or "new_balance2" to stay the way it was when it changed. For instance. I would buy a coke and then it would deduct an amount from my balance. Then it would ask me if I wanted to buy again. I would say yes. and buy a pepsi, but nothing was deducted from my balance that was originally deducted. My balance was reset and it only deducted from 10, not from the changed balance. If you guys can understand what Im trying to say, then can someone help me? I think its because I need to initialize a pointer to the balance variable. But Im not sure.

-Thanks in advance

Regards,
Souper

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>

using namespace std;

int main()
{
	 // Soda Machine Project

	float soda_Coke = 2.50; //1
	float soda_Pepsi = 2; //2
	float soda_DrPepper = 2; //3
	float soda_Fanta = 1.50; //4
	float soda_Sprite = 1; //5

	float balance;
	float new_balance;
	float new_balance2;

	balance = 10;

	char option;
	char option_yes_no;
	char option2;

	cout << "Hello, and welcome to the Soda Machine Project\n";
	cout << "Your current balance is " << balance << endl; 
	cout << "What soda would you like today?\n";
	cout << " \n";
	cout << "Press '1' for Coke\n";
	cout << " \n";
	cout << "Press '2' for Pepsi\n";
	cout << " \n";
	cout << "Press '3' for DrPepper\n";
	cout << " \n";
	cout << "Press '4' for Fanta\n";
	cout << " \n";
	cout << "Press '5' for Sprite\n";

	cin >> option;

	if(option == '1'){
		cout << "You have selected Coke.\n";
		cout << soda_Coke << " deducted from balance\n";
		balance - soda_Coke;
		new_balance = balance - soda_Coke;
		cout << "Current balance is " << new_balance << endl;
		cout << "Would you like to purchase another soda?\n";
		cout << " 'Y' for yes 'N' for No\n";
	}

	else if(option == '2'){
		cout << "You have selected Pepsi.\n";
		cout << soda_Pepsi << " deducted from balance\n";
		balance - soda_Pepsi;
		new_balance = balance - soda_Pepsi;
		cout << "Current balance is " << new_balance << endl;
		cout << "Would you like to purchase another soda?\n";
		cout << " 'Y' for yes 'N' for No\n";
	}

	else if(option == '3'){
		cout << "You have selected DrPepper\n";
		cout << soda_DrPepper << " deducted from balance\n";
		balance - soda_DrPepper;
		new_balance = balance - soda_DrPepper;
		cout << "Current balance is " << new_balance << endl;
		cout << "Would you like to purchase another soda?\n";
		cout << " 'Y' for yes 'N' for No\n";
	}

	else if(option == '4'){
		cout << "You have selected Fanta\n";
		cout << soda_Fanta << " deducted from balance\n";
		balance - soda_Fanta;
		new_balance = balance - soda_Fanta;
		cout << "Current balance is " << new_balance << endl;
		cout << "Would you like to purchase another soda?\n";
		cout << " 'Y' for yes 'N' for No\n";
	}

	else if(option == '5'){
		cout << "You have selected Sprite\n";
		cout << soda_Sprite << " deducted from balance\n";
		balance - soda_Sprite;
		new_balance = balance - soda_Sprite;
		cout << "Current balance is " << new_balance << endl;
		cout << "Would you like to purchase another soda?\n";
		cout << " 'Y' for yes 'N' for No\n";
	}

	else {
		cout << "Error. Choice was not valid\n";
		cout << "Would you like to purchase another soda?\n";
		cout << " 'Y' for yes 'N' for No\n";
	}

		cin >> option_yes_no; //Asking User if they would like to purchase another soda restarting the process

		if(option_yes_no == 'n'){
			cout << "Goodbye. Have a nice day\n";
			return 0;
		}

		else if(option_yes_no == 'y'){
			cout << "What soda would you like today?\n";
			cout << " \n";
			cout << "Press '1' for Coke\n";
			cout << " \n";
			cout << "Press '2' for Pepsi\n";
			cout << " \n";
			cout << "Press '3' for DrPepper\n";
			cout << " \n";
			cout << "Press '4' for Fanta\n";
			cout << " \n";
			cout << "Press '5' for Sprite\n";
		}

		cin >> option2;

	if(option2 == '1'){
		cout << "You have selected Coke.\n";
		cout << soda_Coke << " deducted from balance\n";
		new_balance - soda_Coke;
		new_balance2 = new_balance - soda_Coke;
		cout << "Current balance is " << new_balance << endl;
	}

	else if(option2 == '2'){
		cout << "You have selected Pepsi.\n";
		cout << soda_Pepsi << " deducted from balance\n";
		balance - soda_Pepsi;
		new_balance2 = new_balance - soda_Pepsi;
		cout << "Current balance is " << new_balance << endl;
	}

	
	else if(option2 == '3'){
		cout << "You have selected DrPepper\n";
		cout << soda_DrPepper << " deducted from balance\n";
		balance - soda_DrPepper;
		new_balance2 = new_balance - soda_DrPepper;
		cout << "Current balance is " << new_balance << endl;
	}


	else if(option2 == '4'){
		cout << "You have selected Fanta\n";
		cout << soda_Fanta << " deducted from balance\n";
		balance - soda_Fanta;
		new_balance2 = new_balance - soda_Fanta;
		cout << "Current balance is " << new_balance << endl;
	}

		else if(option2 == '5'){
		cout << "You have selected Sprite\n";
		cout << soda_Sprite << " deducted from balance\n";
		balance - soda_Sprite;
		new_balance2 = new_balance - soda_Sprite;
		cout << "Current balance is " << new_balance << endl;
	}

		else{
		cout << "Error. Choice was not valid\n";
	}

		
	





	char f;
	cin >> f;
	return 0;

	}
Hello FrankMcDonald, You have chosen some very strange ways of achieving your second cola option the user should be able to buy as much soda as they wish as long as their balance has sufficient funds. there is a very simple way of achieving this in what is called a while loop. for 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
float Cheapest = soda_Sprite;
while(balance > Cheapest)
{
 
//display options here
// 1: coke
//2: sprite
//etc  \n

cin >> option;
if(option==1)
{
balance-=soda_Coke;
}
else if(option == 2)
{
 balance-=soda_Sprite;
}



}
cout << "Thank you for using the FrankMcDonald McSoda Machine!\n";
system("pause");//bad voodoo XD
return 0;

Also I would recomend using enum and switch statements for your choices... 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
28
29
30
enum SODA
{
  SPRITE,
 COKE,
  MNTDEW,
};
while(balance>Cheapest)
{
//display options here
// 1: coke
//2: sprite
//etc  \n

cin >> option;
switch(option)
{
case SPRITE:
balance-=soda_Sprite;
break;
case COKE:
balance-=soda_Coke;
break;
case MNTDEW:
balance-=soda_Dew;
break
default:
cout<<"Invalid option Chosen!\n";
break;
}
}
how can i put animation my word ("bpsu") that is moving from the left to the right and
("bpsu") from the right to left using stdio.h,conio.h,iostream.h,dos.h tell me what to do i cant make moving that words on c++ please help me
thanks advance .
how can i put animation my word ("bpsu") that is moving from the left to the right and
("bpsu") from the right to left using stdio.h,conio.h,iostream.h,dos.h tell me what to do i cant make moving that words on c++ please help me
thanks advance .
Well i did Experiment with dos in c++ using turbo c++ 3.0 inside of dosbox. I had a graphics library, but i do not remember the name of it just google c++ dos graphics library i am sure you will find something... also open gl is an option if you're up for the challenge XD. I was alble to create small bitmap sprites using the library i had to create a small 2d skyrim game that i played in dosbox on my andriod. you can do some pretty neat things in dos... its just really hard to learn. Good luck
@NoviceNate333 Thanks a lot Nate ;) I completely forgot about enum and switch functions. Im a little rusty. Only 14 years old. I learned a lot about C++ about a year ago and decided to pick back up on it now. Having some trouble right now but thanks for your help :)
Last edited on
Topic archived. No new replies allowed.