output display incorrectly issue



the amount display were incorrect
as equation "else tp=1029.98;" did not work

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main ()
{
double tp,nt,tap;
char ct;

cout<<"Please enter the class and the number of tickets you wish to buy: ";
cin>>ct>>nt;

if (ct=='E'||'e')
{
tp=329.98;
}
else
{
tp=1029.98;
}

tap=tp*nt;

cout<<"Total amount payable is : $"<<tap<<"\n";


system("pause");
return 0;
}
Last edited on
The || operator takes two boolean values and returns true if at least one of them is true.

'e' is not a bool, it's a char. In this situation it will be treated as true because it's a non-zero value. This means ct=='E'||'e' is equivalent to ct=='E'||true which will always evaluate to true.

To make it work you need to write the if statement as if (ct=='E'||ct=='e').

Another option is to make use of the std::toupper (or std::tolower) function. if (toupper(ct) == 'E')
http://www.cplusplus.com/reference/cctype/toupper/
Thank you! This is real helpful to me!
Topic archived. No new replies allowed.