vending machine sample need help, please look!

I have to make a sample run look like this. I cannot seem to get this right.
what am i doing wrong. PLEASE HELP!

Sample run (as a suggestion)

Welcome to My Vending machine.
Select a coin to insert from below (1-4)
1. for dollar
2. for quarter
3 for dime
4 for nickel
Your choice (1-4): 7
Invalid choice. Please select again.
Your choice (1-4): 1

Inserted so far: $1.00 out of $4.05

Select a coin to insert from below (1-4)
1. for dollar
2. for quarter
3 for dime
4 for nickel
Your choice (1-4): 2

Inserted so far: $1.25 out of $4.05

....

Inserted so far: $4.35 out of $4.05

Purchase completed!

Your change: $0.30





Here are my codes:


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
#include <iostream>
using namespace std;

int main ()
{
	 int TOTAL_COST = 405;
	 int DOLLAR = 100, QUARTER = 25, DIME = 10, NICKEL = 5;
     int total_coins = 0, num, change;
{
     cout << " 1. for dollar \n 2. for quarter \n 3. for dime \n 4. for nickel \n";  
	 cout << " Your choice (1-4): " << endl;
	 cin >> num;

     if (num >= 0)
{
    total_coins += num; //total up coins
    cout << " Inserted so far: $ " << total_coins << " out of $" << TOTAL_COST << endl;
}
    else
{
cout << "Invalid choice. Please select again."; //display a message
}
}	while (total_coins < TOTAL_COST);
	cout << "Purchase Completed! ";//display enjoy message
	//calculate the change
	change = total_coins - TOTAL_COST;
	if (change > 0)
{
    cout << "Your change is: " << change;//message           
}
     
    return 0;
}
Last edited on
My suggestion code (Rebuilded)
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
//Index (you can define more)
#define DOLLAR 0
#define QUARTER 1
#define DIME 2
#define NICKEL 3
int TOTAL_COST = 405;
int total_coins = 0, num, change;

int nCoinIns[4];// = {100, 25, 10, 5};

int main(){
//Parsing values
nCoinIns[DOLLAR] = 100;
nCoinIns[QUARTER] = 25;
nCoinIns[DIME] = 10;
nCoinIns[NICKEL] = 5;
cout << " Welcome to My Vending machine. " << endl;
while (total_coins < TOTAL_COST) // Loop
{
cout << "Select a coin to insert from below (1-4)\n" << endl;
cout << " 1. for dollar \n 2. for quarter \n 3. for dime \n 4. for nickel \n";
cout << " Your choice (1-4): " << endl;
cin >> num;
if(num <= 0 || num > 4){//Check wrong value
cout << "Invalid choice. Please select again.\n\n" << endl; continue;}

total_coins += nCoinIns[num - 1]; //Look !!!!!

cout << " Inserted so far: $ " << total_coins << " out of $" << TOTAL_COST << endl; 

if(total_coins >= TOTAL_COST) {//Stop purchasing or not
cout << "Purchase Completed! \n\n" << endl;
change = total_coins - TOTAL_COST;
if (change > 0)cout << "Your change is: " << change;
break; //Stop !!!
}


} // End purchasing loop
return 0;
}
/////////////////////////////////////////////////// 

Thus, my suggestion code may be very different ???

I used an array, which contains the coin types. You can define more if you need. (This is called a trick)

total_coins += nCoinIns[num - 1];

Why num - 1 ? See the nCoinIns initiating.

And, avoid using too much if{[...]} else{[...]}. No problem, but it will make your code more complex and more harder to read at somewhere.

Do you have any question about this code?
Hope this helps.
Last edited on
Now, back to your question.
if (num >= 0)
{
total_coins += num; //total up coins
cout << " Inserted so far: $ " << total_coins << " out of $" << TOTAL_COST << endl;}

"num" is a coin index, and "total_coins += num;" ??? Perhaps I may have to re-input it hundreds of times !!!

About "total_coins += num" ?....
You want (num > 0) (Dollar) and num <= 4 (NICKEL) ?
Then, why did you define those variables ? (They're redundant)
I can input : num = 224, 466 or 63578... (They're valid, ????)

It would be something like this :
1
2
3
4
5
6
7
if (num > 0 && num < 5){
if(num == 1)total_coins += DOLLAR;
if(num == 2)total_coins += QUARTER;
if(num == 3)total_coins += DIME;
if(num == 4)total_coins += NICKEL;
cout << " Inserted so far: $ " << total_coins << " out of $" << TOTAL_COST << endl;}
else cout << "Invalid choice. Please select again."; //display a message 
Last edited on
@Jackson Marie (19)

There are a number of problems with your code, it doesn't work, it is poorly formatted, and worse than the OP's code. Your second post isn't any better.

The worse thing is that it could cause the OP to waste a lot of time trying to fix your errors.

@paetim

IMO, ignore Jackson.

Edit your post so it uses code tags - the <> button on the right, then we can give you advice from there. If you have compiler output, post that as well
@TheIdeasMan
I recommend you don't think too much. I, always never request anyone to try my own sources. All code & sources before any post are tested carefully. I tested the example and it's fine. NO ERRORS, NO CRASHES, NO WARNINGS !!!!!! I don't understand what you say, is there any awful point in the suggestion code? Then in your opinions, what is good, what is bad? What is missing, what is redundant? What is too high, what is wrong? And what is... !?!??!?!?!!?
You should note it's only a reference source, and there are many methods around. Hey TheBestIdeasMan, You should have a different idea better than me ???

@paetim
Don't be afraid. As TheIdeasMan said, maybe the code will never work perfectly 100%, but in some similar cases, maybe it'll able to help you.
Or follow the BestIdeasMan's advice, I'm totally sure many better knowledgeable people in the world in the world will help & answer you earnestly.... (I'm only just an opinion...)
Edit: This source is based on your demonstration program text. A small problem is int - float but I haven't made any change and ignored them because your source had defined them. If you want, simply divide the value by 100 (It should correct)
HTH
Last edited on
I need the selection when you put (1-4) to be the right amount. When i type in 2 it should be .25 but it says 2. Please help!
Please give your program output
1. for dollar
2. for quarter
3. for dime
4. for nickel
Your choice (1-4):
5
Inserted so far: $ 5 out of $405
Please look at reference code. Also you should review the second post, this problem was answered.
If you need help, ask here.
Last edited on
I tried using your code and it did not work. If you could put it all together so I can try it that would help a lot. Thank you
Compling errors? Different compliers?
all of the codes would not work together. i put my codes in code format. please look
I'll review & rewrite and post again my correct code in the afternoon or in the evening.... Cheers up! :)

HTH
ok. thanks
This code should work (:thumbs up)

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

//Index (you can define more)
#define DOLLAR 0
#define QUARTER 1
#define DIME 2
#define NICKEL 3
int TOTAL_COST = 405;
int total_coins = 0, num = 0, change = 0;

int nCoinIns[4];// = {100, 25, 10, 5};

int main(){
//Parsing values
nCoinIns[DOLLAR] = 100;
nCoinIns[QUARTER] = 25;
nCoinIns[DIME] = 10;
nCoinIns[NICKEL] = 5;
printf(" Welcome to My Vending machine. \n\n");
while (total_coins < TOTAL_COST) // Loop
{
printf("Select a coin to insert from below (1-4)\n");
printf("1. for dollar \n 2. for quarter \n 3. for dime \n 4. for nickel \n");
printf("Your choice (1-4): ");

scanf("%d",&num);

if(num <= 0 || num > 4){//Check wrong value
printf("Invalid choice. Please select again.\n\n");continue;}


total_coins += nCoinIns[num - 1]; //Look !!!!!

printf("Inserted so far: $ %.3f out of $ %.3f\n\n",float(total_coins / 100.0f), float(TOTAL_COST / 100.0f));
// Or just
// printf("Inserted so far: $ %d out of $ %d\n\n",total_coins, TOTAL_COST);

if(total_coins >= TOTAL_COST) {//Stop purchasing or not
printf("\nPurchase Completed! \n");
change = total_coins - TOTAL_COST;
if (change > 0)printf("Your change is: $ %.3f \n",float (change / 100.0f));
// Or just
//if (change > 0)printf("Your change is: %d \n",change);
break; //Stop !!!
}


} // End purchasing loop
printf("\n\nEnter a number before end...",change);
scanf("%d",&num);
return 0;
}
///////////////////////////////////////////////////  

If you need help, ask here... :)
Thank you
Topic archived. No new replies allowed.