Home > database >  C If, else if, and else statements producing incorrect answers depending on user's answer
C If, else if, and else statements producing incorrect answers depending on user's answer

Time:10-05

The rules that I am trying to follow for this code is:

Calculate discount %, total discounted price of tickets, and $ amount of savings.

Tell user the discount, total price for # of tickets and how much user saves.

User is able to enter upper and lower case Y/N

For example, when user inputs 10 tickets, there are a student but NOT a customer. The discount is 10%, $90, $10 savings.

HOWEVER

When the user inputs, lets say, 6 tickets, they are a new customer but NOT a student. The discount SHOULD be 15%, $51, and $9 savings but the program will NOT calculate this and say the discount is 0%, $60, and $0 savings...

I have tried to change the last conditions from else if to else and the only difference is that another calculation is wrong. This leads me to believe that fundamentally I am not doing something correctly.

Keep in mind, I am relatively new to C . Here is the code:

#include <iostream>

using namespace std;

float numberTickets;
char customer;
char student;
float totalPrice;

int main()
{
   
    float discountCustomer;
    float discountStudent;
    float discountBoth;
    float savings;
    float savingsCustomer;
    float savingsStudent;

    cout<<"How many tickets are you purchasing? ";
    cin>>numberTickets;
    cout<<"Are you a new customer (Y/N): ";
    cin>>customer;
    cout<<"Are you a student (Y/N): ";
    cin>>student;

    if (customer=='N' || customer=='n' && student=='N' || student=='n'){
        cout<<"You will receive a discount of 0%"<<endl;
    }else if (student=='Y' || student=='y' && customer=='N' || customer=='n'){
        cout<<"You will receive a discount of 10%"<<endl;
    }else if (customer=='Y' || customer=='y' && student=='N' || student=='n'){
        cout<<"You will receive a discount of 15%"<<endl;
    }else if (customer=='Y' || customer=='y' && student=='Y' || student=='y'){
        cout<<"You will receive a discount of 25%"<<endl;
    }

    totalPrice = (numberTickets)*10;
    discountCustomer = (totalPrice)*(0.85);
    discountStudent = (totalPrice)*(0.90);
    discountBoth = (totalPrice)*(0.75);
    savings = (totalPrice)-(discountBoth);
    savingsCustomer = (totalPrice)-(discountCustomer);
    savingsStudent = (totalPrice)-(discountStudent);

    if (customer=='N' || customer=='n' && student=='N' || student=='n'){
        cout<<"Your price for "<<numberTickets<<" ticket(s) is $"<<totalPrice<<endl;
    }else if (student=='Y' || student=='y' && customer=='N' || customer=='n'){
        cout<<"Your price for "<<numberTickets<<" ticket(s) is $"<<discountStudent<<endl;
    }else if (customer=='Y' || customer=='y' && student=='N' || student=='n'){
        cout<<"Your price for "<<numberTickets<<" ticket(s) is $"<<discountCustomer<<endl;
    }else if (customer=='Y' || customer=='y' && student=='Y' || student=='y'){
        cout<<"Your price for "<<numberTickets<<" ticket(s) is $"<<discountBoth<<endl;
    }

    if (customer=='N' || customer=='n' && student=='N' || student=='n'){
        cout<<"You saved $0"<<endl;
    }else if (student=='Y' || student=='y' && customer=='N' || customer=='n'){
        cout<<"You saved $"<<savingsStudent<<endl;
    }else if (customer=='Y' || customer=='y' && student=='N' || student=='n'){
        cout<<"You saved $"<<savingsCustomer<<endl;
    }else if (customer=='Y' || customer=='y' && student=='Y' || student=='y'){
        cout<<"You saved $"<<savings<<endl;
}
    return 0;
}

CodePudding user response:

Double-check the rules of operator precedence. In particular, note that && has higher precedence than ||.

Precedence Operator Description
10 == != For equality operators = and ≠ respectively
14 && Logical AND
15 || Logical OR

So, consider a line like:

if (customer=='N' || customer=='n' && student=='N' || student=='n')

Adding some extra parentheses to make it clear how the compiler is grouping them:

if ((customer=='N') || ((customer=='n') && (student=='N')) || (student=='n'))

When what you actually meant was:

if ((customer=='N' || customer=='n') && (student=='N' || student=='n'))

CodePudding user response:

To me, it seems like it would be better to simply avoid using if statements that are quite so complex. You basically have four choices: no discount, student discount, new customer discount, and both discounts.

I'd start by setting a variable to indicate whether they're a new customer and/or student:

unsigned char student;
unsigned char customer;

int is_student = std::tolower(student) == 'y';
int is_customer = std::tolower(customer) == 'y';

Since we're using tolower, we need to assure the values aren't negative, so I've used unsigned char for them.

Then I'd combine both those facts into a single number, so I can use them as an index to look up the correct discount in a table:

double discount_table[] = { 100.0, 0.85, 0.90, 0.75 };

std::size_t discount_index = is_customer   2 * is_student;

double discounted_price = raw_price * discount_table[discount_index];

CodePudding user response:

I read your code and you know it's very simple but written in a long and complicated manner, I am not sure that I can tell you the simplest code, but yes better than the present one, instead of using so much if/else you can opt for the switch, I solved your problem using a switch, so give it a look, all variables are the same, I just defined a new extra variable 'num' in the code. I am sharing images see the code line number of the code to understand it sequentially. enter image description here

enter image description here

enter image description here

enter image description here

  • Related