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;
  cin>>ticket;
     switch (ticket)
     {
     case 'A' : cout<<"cost is"<<nums[0]<<endl;
     break;
     case 'B' : cout<<"cost is"<<nums[1]<<endl;
     break;     
     case 'C' : cout<<"cost is"<<nums[2]<<endl;
     break;
     default: cout<<"wrong ticket"<<endl;
     }


  system("pause");
  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

Recommended Answers

All 4 Replies

'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.

ah i missed that. i thought my array coding was incrrect. thanks

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.