Code is producing errors and im not sure why

Good day all

So i have written a code that should in theory give discounts to various people depending on who they are (pensioners, students and other) however when i try tonrun my code i keep getting errors saying that there is no match for the '==;. I am very confused as to why this is and if any kind person could explain it to me i would be very grateful.

Also would someone mind checking my code for me and seeing if it is correct as it is for an assignment.

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

float calculateDiscountOthers(bool didBuyPopcorn)
{
  if (didBuyPopcorn == true)
    return 5.0;
  else
    return 0.0;
}

float calculateDiscount(string customerType, bool didBuyPopcorn)
{
  if ( customerType == 'p' || customerType == 's' )
  {
    if (didBuyPopcorn == true)
      return 20.0;
    else
      return 10.0;
  }
  else
  {
    return calculateDiscountOthers(didBuyPopcorn);
  }
}



int main()
{
  string customerType;
  string didBuyPopcorn;

  cout << "Enter the customer letter: " << endl;
  cin >> customerType;

  cout << "Did they buy popcorn?  Enter yes or no" << endl;
  cin >> didBuyPopcorn;

  bool didbuy;
  if (didBuyPopcorn =='y')
    didbuy = true;
  else
    didbuy = false;

  cout.setf(ios::fixed);
  cout.precision(2);

  float discountPercent = calculateDiscount(customerType, didbuy);
  float discountAmount = 80.00 * (discountPercent/100.0);

  float newTicketPrice = 80.00 - discountAmount;

  cout << "New ticket price: R" << newTicketPrice << endl;
}
Last edited on
The problem is that you're trying to compare a string with a char.
customerType == 'p'

customerType is a std::string.

'p' is a char.

There's no standard == operator that compares a std::string to a char.

What is it that you actually want your user to input? The prompt asks them to enter "yes" or "no", and reads their input as a std::string. However, your code then seems to assume they've entered a single character 'y' or 'n'.
Last edited on
"p" is a string , 'p' is a char - Prefer char for this.

Use double not float.

be careful with your types, use bool if that is what you mean.
'y' is a character
"y" is a string literal


Also, if (didBuyPopcorn == true)
at that point `didBuyPopcorn' is a bool, use it as such.
if (didBuyPopcorn)
Awesome, thank you everyone. Code is now outputting correctly
Topic archived. No new replies allowed.