I don't understand my errors

You have invented a vending machine capable of deep frying twinkies. Write a program to simulate the vending machine. It costs $3.50 to buy a deep-fried twinkie, and the machine only takes coins in denominations of a dollar, quarter, dime or nickel.

Write code to simulate a person putting money into the vending machine by repeatedly prompting the user for the next coin to be inserted. Output the total entered so far when each coin is inserted. When $3.50 or more is added, the program should output "Enjoy your deep-fried twinkie" along with any change that should be returned. Use top-down design to determine appropriate functions for the program.


//Twinkie.cpp
//Christina

//assignment 4-4
//calculates change inserted into a vending machine asking for more till sum has been met then gives change

#include <iostream>
using namespace std;

double calc(char amount);

int main()
{

char amount;
double total;

cout<<"For a delicious Deep-Fat Fried Twinkie, Please insert a coin: (q for quarter/d for dime/n for nickel /s for dollar)";
cin>>amount;

do
{

total=calc(amount);

if (total >= 3.5)
cout<<"You have " << total - 3.5 << "in change. \n Enjoy your Twinkie!";
else
{
cout<<"Insert another coin.";
cin>>amount;
}

}while(total < 3.5);

return 0;
}

double calc(char amount)
{
double Q= 0.25 ,D= 0.10 ,N= 0.05 ,S= 1.0;
double total;

switch(amount)
{
case 'q':
amount=Q;
break;
case 'd':
amount=D;
break;
case 'n':
amount=N;
break;
case 's':
amount=S;
break;
}

total += amount;

return (total);
}



1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(47): warning C4244: '=' : conversion from 'double' to 'char', possible loss of data
1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(50): warning C4244: '=' : conversion from 'double' to 'char', possible loss of data
1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(53): warning C4244: '=' : conversion from 'double' to 'char', possible loss of data
1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(56): warning C4244: '=' : conversion from 'double' to 'char', possible loss of data
1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(60): warning C4700: uninitialized local variable 'total' used
Last edited on
You've defined variable amount of type char. Then you assign a value of type double to it.
1
2
double Q= 0.25;
amount=Q;


That will cause a loss of data since the char can hold an integer in the range -128 to +127, with no decimal places.
Should I change the variable type char to string?
No. At least I don't think it would help. The char "amount" is serving a useful purpose. What I suggest in function calc() is define another variable of type double and use that for the calculations instead.
//Twinkie.cpp
//Christina

//assignment 4-4
//calculates change inserted into a vending machine asking for more till sum has been met then gives change

#include <iostream>
#include <string>
using namespace std;

double calc(double amount);

int main()
{
double amount,total;


cout<<"For a delicious Deep-Fat Fried Twinkie, Please insert a coin: \n Enter only one of the following (.25/ .10/ .05 / 1) \n";
cin>>amount;

do
{

total=calc(amount);

if (total >= 3.5)
cout<<"You have " << total - 3.5 << "in change. \n Enjoy your Twinkie!";
else
{
cout<<"Insert another coin. \n";
cin>>amount;
}

}while(total < 3.5);

return 0;
}

double calc(double amount)
{
double sum=0;

sum += amount;

return (sum);
}


This is what I changed my code to. If I don't initialize sum I get an error, but the way it runs now it resets the sum everytime so I can't get it to add it each time.
Last edited on
Something like this. I'm not sure whether its absolutely right, but you should get the idea:
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
#include <iostream>

using namespace std;

double calc(char amount);

int main()
{
    char amount;
    double total = 0;

    cout << "For a delicious Deep-Fat Fried Twinkie, Please insert a coin: "
            "(q for quarter/d for dime/n for nickel /s for dollar)";
    cin>>amount;

    do
    {
        total += calc(amount);

        if (total >= 3.5)
            cout<<"You have " << total - 3.5 << "in change. \n Enjoy your Twinkie!";
        else
        {
            cout<<"Insert another coin.";
            cin>>amount;
        }
    } while (total < 3.5);

return 0;
}

double calc(char amount)
{
    double Q= 0.25 ,D= 0.10 ,N= 0.05 ,S= 1.0;
    double total = 0;
    double value = 0;

    switch (amount)
    {
        case 'q':
            value=Q;
            break;
        case 'd':
            value=D;
            break;
        case 'n':
            value=N;
            break;
        case 's':
            value=S;
            break;
    }

    total += value;

    return (total);
}
Last edited on
There were a logical error. But, if you take out the total variable declaration and the total += value the program run perfectly. Thank you so much :)
Topic archived. No new replies allowed.