My first program with if/else

I'm trying to run this program. It compiles as it is but I can't get equations to work, display amount is not right. I'm just really unsure about all of it.

Program is suppose to get number of adult, child movie tickets, if they have a discount coupon, what typr discount coupon, calculate amount, and display all info in a box.

/*
IF
Write a program that will calculate a customer's purchase amount
for movie tickets.

The user should be prompted for the amount of adult movie tickets
they would like to purchase, the amount of child movie tickets
that they would like to purchase, and whether or not they have a
discount coupon.

If the user has a discount coupon, determine if it is for an adult
movie ticket ("adult") or for child movie ticket ("child").

Adult movie tickets cost 10.50 each, while child movie tickets
cost 5.00 each.

The discount coupon (if any) should be subtracted from the purchase
amount.

Calculate and display the overall purchase amount with two digits after
the decimal point.
*/


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{

cout << fixed << setprecision(0) << setw(3) << endl;

float numAdtTix, numChdTix, TotPurchase, DiscountCoup;

string haveDiscount, DiscountType;

//get the number of adult movie tickets

cout << "Enter the number of adult movie tickets that are being purchased: ";
cin >> numAdtTix;

//get the number of children's movie tickets

cout << "Enter the amount of child tickets that are being purchased: ";
cin >> numChdTix;

//ask if the user has a discount coupon

cout << "Do you have a discount coupon (Y for yes)? ";
cin >> haveDiscount;

//if the user has a discount coupon
// ask the user for the discount coupon type


cout << "Is this discount for an adult or child's ticket (A for adult or C for child)? ";
cin >> DiscountType;

// if the discount coupon is "adult"
// discount coupon is 10.50
// else
// if the discount coupon is "child"
// discount coupon is 5.00
// else
// error message for bad discout type
// endif
// endif
//endif

if ( DiscountType == "A" )
{
DiscountCoup == 10.50;
cout << "Discount: 10.50" << DiscountCoup << endl;
}
else
{
if ( DiscountType == "C" )
{
DiscountCoup == 5.00;
cout << "Discount: 5.00" << DiscountCoup << endl;
}
else
{
cout << DiscountCoup << "is an invalid coupon type" << endl;
}
}



//calculate the purchase amount

TotPurchase = (numAdtTix * 10.50) + (numChdTix * 5.00) - DiscountCoup;



return 0;
}

In if, use == for compare. But otherwise, use =(single) for assigning.

VarType var;
var = value; //it assigns value to var.

if( var == value ) // checks if var and value variables are equal
...

Note: == operator returns bool variable that holds comparison result.
bool result = var==value; //if var equals value, result will be true.
I made it = now it says error:could not convert "DiscountType,
and std::basic_string<_CharT,_Traits,_Alloc>::operator=[
shows this at lines 87 and 94

I'm so confused with this
Last edited on
Line 87 and 94? Can you post your final code? And put comment in lines you get error. And please please please please use code tags for posting code.[ code ] [ /code ] (without spaces)

And again, use == for compare( if( discountType == "A" )
Use = for assigning ( discountCoup = 5 )
Last edited on
You have a great help tolga gerekci! It works now. Just one more question if you don't mind....When program asks the user if they have a discount, when response is Y it goes on and asks what kind. If user says N I would like it to skip asking which type and go straight to the total. I assume I would need if/else statement in there as well (lines 33-35).



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
 Write a program that will calculate a customer's purchase amount
 for movie tickets.
 
The user should be prompted for the amount of adult movie tickets
 they would like to purchase, the amount of child movie tickets
 that they would like to purchase, and whether or not they have a 
discount coupon.
 
If the user has a discount coupon, determine if it is for an adult
 movie ticket ("adult") or for child movie ticket ("child").
 
Adult movie tickets cost 10.50 each, while child movie tickets
 cost 5.00 each.
 
The discount coupon (if any) should be subtracted from the purchase
 amount.
 
Calculate and display the overall purchase amount with two digits after
 the decimal point.
 */
 

#include <iostream>
 #include <iomanip>
 #include <string>
 
using namespace std;
 
int main()
 {
 
cout << fixed << setprecision(0) << setw(3) << endl;
 
float numAdtTix, numChdTix, TotPurchase, DiscountCoup = 0;
 
string haveDiscount, DiscountType;
 
cout << setprecision(2) << fixed << endl;
 
//get the number of adult movie tickets
 
cout << "Enter the number of adult movie tickets that are being purchased: ";
 cin >> numAdtTix;
 
//get the number of children's movie tickets
 
cout << "Enter the amount of child tickets that are being purchased: ";
 cin >> numChdTix;
 
//ask if the user has a discount coupon
 
cout << "Do you have a discount coupon (Y for yes, N for no)? ";
 cin >> haveDiscount;
 

//if the user has a discount coupon
 // ask the user for the discount coupon type
 
cout << "Is this discount for an adult or child's ticket (A for adult or C for child)? ";
 cin >> DiscountType;
 

// if the discount coupon is "adult"
 // discount coupon is 10.50
 // else
 // if the discount coupon is "child"
 // discount coupon is 5.00
 // else
 // error message for bad discout type
 // endif
 // endif
 //endif
 
if ( DiscountType == "A" )
 {
 DiscountCoup = 10.50;
 cout << "Discount: " << DiscountCoup << endl;
 }
 else
 {
 if ( DiscountType == "C" )
 {
 DiscountCoup = 5.00;
 cout << "Discount: " << DiscountCoup << endl;
 }
 else
 {
 cout << DiscountCoup << "is an invalid coupon type" << endl;
 }
 }
 


//calculate the purchase amount
 
TotPurchase = (numAdtTix * 10.50) + (numChdTix * 5.00) - DiscountCoup;
 cout << "Total Purchase: " << TotPurchase << endl;
 

return 0;
 }
this is what I came up with .... any suggestions. I get error line 76 Y is not declared in scope, line 97 error expected primary-expression before else, and line 97 error expected ; before else. Above is original program and below is corrections I was trying to attempt

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
/* 
IF
Write a program that will calculate a customer's purchase amount
for movie tickets.

The user should be prompted for the amount of adult movie tickets
they would like to purchase, the amount of child movie tickets
that they would like to purchase, and whether or not they have a 
discount coupon.

If the user has a discount coupon, determine if it is for an adult
movie ticket ("adult") or for child movie ticket ("child").

Adult movie tickets cost 10.50 each, while child movie tickets
cost 5.00 each.

The discount coupon (if any) should be subtracted from the purchase
amount.

Calculate and display the overall purchase amount with two digits after
the decimal point.
*/


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{

cout << fixed << setprecision(0) << setw(3) << endl;

float  numAdtTix, numChdTix, TotPurchase, DiscountCoup = 0;

string haveDiscount,  DiscountType;

cout << setprecision(2) << fixed << endl;


//get the number of adult movie tickets

cout << "Enter the number of adult movie tickets that are being purchased: ";
cin >> numAdtTix;


//get the number of children's movie tickets

cout << "Enter the amount of child tickets that are being purchased: ";
cin >> numChdTix;


//ask if the user has a discount coupon
//if the user has a discount coupon
//  ask the user for the discount coupon type
//  if the discount coupon is "adult"

//    discount coupon is 10.50
//  else
//   if the discount coupon is "child"
//    discount coupon is 5.00
//   else
//    error message for bad discout type
//   endif
//  endif
//endif


cout << "Do you have a discount coupon (Y for yes, N for no)? ";
cin >> haveDiscount;

if (haveDiscount == "Y")
   {
   haveDiscount = Y;
   cout << "Is this discount for an adult or child's ticket (A for adult or C for child)? ";
   cin >> DiscountType;
   }
   if ( DiscountType == "A" )
      {
      DiscountCoup = 10.50;
      cout << "Discount: " << DiscountCoup <<  endl;
      }
   else
      {
      if ( DiscountType == "C" )
         {
	      DiscountCoup = 5.00;
	      cout << "Discount: " << DiscountCoup << endl;
	     }
      else
         {
	     cout << DiscountCoup << "is an invalid coupon type" << endl;
         }
	  }
else
   {
   if ( haveDiscount == "N" );
   haveDiscount = N;
   cout << "Total Purchase: " << TotPurchase << endl;
   }


//calculate the purchase amount

TotPurchase = (numAdtTix * 10.50) + (numChdTix * 5.00) - DiscountCoup;



return 0;
}

Its trying to find a variable called Y because you didn't put ". If you put ", it will take that value as string(const char*).

Change this

haveDiscount = Y;

to this

haveDiscount = "Y" ;
thank you for all your help! I got it runninng now.
Topic archived. No new replies allowed.