variables and operators: calculating change and display results

hey everyone im stuck on trying to get the change for my program to work I got this all down but I keep getting zero on my change please help
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>
#include <iomanip>
using namespace std;

int main()
{
  string firstname;
  string lastname;
  string price;
  string paid;
  double amountprice;
  double amountpaid;
  double change;
  double dollar=0;
   double quarters=0;
  double dimes=0;
  double nickels=0;
  double cents=0;
  cout << "Please enter your first name"<< endl;
  cin>>firstname;
  cout << "Please enter your last name"<< endl;
  cin>> lastname;
  cout << " Welcome "<< firstname << " " <<  lastname << endl;
  cout<< "In this program we will see the amount money that is needed to be paid and the change that will be do" << endl;


  //ask user to enter the price and how much money they paid
  cout<<" Please enter the price of the object: $"<< setprecision(2) << fixed << price << endl;
  cin>>amountprice;
  cout<<" Enter the amount that you paid: $"<< setprecision(2) << fixed << paid << endl;
  cin>>amountpaid;
  if (price>paid)
   {
     cout<<"You didnt pay the full amount, pay the full amount or return the item"<<endl;
     return -1;
   }

 change = amountpaid - amountprice;

 cout << "The change for this transaction is: $" << setprecision(2) << fixed << change << endl;

 dollar = change / 1;
 change = change - dollar;

 quarters = change / .25;
 change = change - (quarters * .25);

 dimes = change / .10;
 change = change - (dimes * .10);

 nickels = change / .05;
 change = change - (nickels * .05);

 cents = change / .01;
 change = change - (cents * .01);

 cout << "Dollars:  " << dollar << endl;
 cout << "Quarters: " << quarters << endl;
 cout << "Dimes:    " << dimes << endl;
 cout << "Nickels:  " << nickels << endl;
 cout << "Cents:  " << cents << endl;

 cout << endl;
   return 0;
}


Last edited on
All of this is wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dollar = change / 1; // You're dividing by 1...
change = change - dollar; // dollar == change, so this equals 0
// nothing after this matters because it's already zero

quarters = change / .25;
change = change - (quarters * .25);

dimes = change / .10;
change = change - (dimes * .10);

nickels = change / .05;
change = change - (nickels * .05);

cents = change / .01;
change = change - (cents * .01);



Also, on line 33 your are comparing two strings, not two numbers.
Last edited on
well I did this but doesn't work, when I run it If I put 2.50 for the price it wont run but if I just put 2 it will ask for the amount paid so I don't know whats going on.
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
  string firstname;
  string lastname;
  string price;
  string paid;
  int amountprice;
  int amountpaid;
  int change=0;
  int dollar=0;
  int quarters=0;
  int dimes=0;
  int nickels=0;
  int cents=0;
  cout << "Please enter your first name"<< endl;
  cin>>firstname;
  cout << "Please enter your last name"<< endl;
  cin>> lastname;
  cout << " Welcome "<< firstname << " " <<  lastname << endl;
  cout<< "In this program we will see the amount money that is needed to be paid and the change that will be do" << endl;


  //ask user to enter the price and how much money they paid
  cout<<" Please enter the price of the object: $"<< setprecision(2) << fixed << price << endl;
  cin>>amountprice;
  cout<<" Enter the amount that you paid: $"<< setprecision(2) << fixed << paid << endl;
  cin>>amountpaid;
  if (amountprice>amountpaid)
   {
     cout<<"You didnt pay the full amount, pay the full amount or return the item"<<endl;
     return -1;
   }

 change = amountpaid - amountprice;

 cout << "The change for this transaction is: $" << setprecision(2) << fixed << change << endl;
 change = change*100;
 dollar = change/100;
 quarters=change/25;
 change=change -(quarters *25);
  dimes=change/10;
  change=change-(dimes*10);
 nickels=change/5;
 change=change-(nickels*5);
 cents=change;

 cout << "Dollars:  " << dollar << endl;
 cout << "Quarters: " << quarters << endl;
 cout << "Dimes:    " << dimes << endl;
 cout << "Nickels:  " << nickels << endl;
 cout << "Cents:  " << cents << endl;

 cout << endl;

   return 0;
}

setprecision(2) << fixed << price <<
on lines 29, 31, and 41 does nothing.

All of your number values are integers. http://en.wikipedia.org/wiki/Integer
You should be using float or double.
Also:

At the end of line 25, it should be "due" not "do". You also left out the period.
You're missing an apostrophe in "didn't" in line 35. Line 35 is also a comma-splice.
ok well I got this but im not getting it how I want it it gives me the cents but no exactly right
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
  string firstname;
  string lastname;
  string price;
  string paid;
  double amountprice;
  double amountpaid;
  double change;
  double dollar=0.0;
  double quarters=0.0;
  double dimes=0.0;
  double nickels=0.0;
  double cents=0.0;
  cout << "Please enter your first name"<< endl;
  cin>>firstname;
  cout << "Please enter your last name"<< endl;
  cin>> lastname;
  cout << " Welcome "<< firstname << " " <<  lastname << endl;
  cout<< "In this program we will see the amount money that is needed to be\
 paid and the change that will be due" << endl;


  //ask user to enter the price and how much money they paid
  cout<<" Please enter the price of the object: $" << endl;
  cin>>amountprice;
  cout<<" Enter the amount that you paid: $"<< endl;
  cin>>amountpaid;
  if (amountprice>amountpaid)
    {
      cout<<"You didn't pay the full amount, pay the full amount or return \
the item"<<endl;
      return -1;
    }

  cout << "The change for this transaction is: $"<< endl;
  change=amountpaid - amountprice;
 change = change*100;
  dollar = change/100;
  //change=change%100;
  quarters=change/25;
  //change=change%25;
   dimes=change/10;
   //change=change%10;
  nickels=change/5;
  //change=change%5;
  cents=change;

  cout << "Dollars:  " << dollar << endl;
  cout << "Quarters: " << quarters << endl;
  cout << "Dimes:    " << dimes << endl;
  cout << "Nickels:  " << nickels << endl;
  cout << "Cents:  " << cents << endl;

  cout << endl;

  return 0;
}

this is what I get if I am to run it but its not entirely right and im not getting the change for example the change for this transaction is 1.50

Please enter the price of the object: $
1.50
Enter the amount that you paid: $
3.00
The change for this transaction is: $
Dollars: 1.5
Quarters: 6
Dimes: 15
Nickels: 30
Cents: 150
There's no reason to include <iomanip>, you aren't using it. You also aren't using two of your string variables: price and paid.

Your code is doing exactly what you're telling it to do. Try thinking about what it IS doing and what you WANT it to do.
ok I got it thank you I appreciate it
Topic archived. No new replies allowed.