hello, so im designing a simple prgram which calculates the cost of a ticket you buy. there are only 3 ticket options, namely a b and c.
here is my code.

#include <iostream>
using namespace std;
const int PRICE = 3;
int find(int [PRICE]);

int main()
  int nums[PRICE] = {2, 3 ,4};
  int ticket;
  cout<<"select a ticket"<<endl;
     switch (ticket)
     case 'A' : cout<<"cost is"<<nums[0]<<endl;
     case 'B' : cout<<"cost is"<<nums[1]<<endl;
     case 'C' : cout<<"cost is"<<nums[2]<<endl;
     default: cout<<"wrong ticket"<<endl;

  return 0;

however for whichever letter i put in i always get 'wrong ticket' any help would be appreciated thanks.

simply if i input 'A' it should output 'cost is 2', if i input 'B' it should output 'cost is 3' etc

Edited by Nick Evan: Fixed starting CODE-tag

7 Years
Discussion Span
Last Post by Tom83B

'ticket' is an integer, but you expect your user to input 'A', 'B' etc.
What could go wrong?

You should check if the cin >> ticket is even succeeding, before trying to use 'ticket' in the switch case.


The int data type is a numeric type that can only store whole numbers. You are trying to input and compare to characters. These types are not compatible. You must change "ticket" to the char data type.

You will also have to "normalize" the input because 'a' and 'A' are not the same value. If your user inputs 'a', it will not trigger case 'A' because it doesn't match. Look up the functions tolower() and toupper().


As said above, ticket is an integer. The compiler lets you compile it, because 'A', 'B', 'C' means basically the same as 65, 66, 67 - it's from ascii table (look it up). Each character from this table has an integer assigned.
So if write 65, it writes back "cost is two".
Change int ticket to char ticket. It works than.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.