Sign in to follow this  
Ultimatepuff

Simple Logic Error Help

Recommended Posts

Hello again. I am working on another school project where a user inputs if he/she is a student, visitor, or employee for a school parking meter, and it would output the cost. Here is the format of prices:
Quote:
d) Calculates the amount charged based upon the following rates: a. students: $1 no matter how long the stay b. visitors: $5 no matter how long they stay c. employees: car - $5 per hour, motorbike - $2 per hour, truck - $10 per hour However, if parking time is longer than 4 hours, a 25% discount rate is applied (for any hours after the 4th hour). If parking time is longer than 12 hours, a 50% discount is applied (to all hours after the 12th hour).
Here is my code:
#include <iostream>
#include <cstdlib>
using namespace std;

int main(){
    char status, vehicle;
    int hours;
    double cost;   
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    cout << "Parking Perming Machine\n" << endl;
    cout << "Please enter your status.\n(S for student, V for visitor, E for employee):\n";
    cin >> status;
    cout << "Please enter your vehicle type.\n(C for car, M for motorbike, T for truck):\n";
    cin >> vehicle; 
    cout << "Please enter your parking time in hours: ";
    cin >> hours;
    cout << " \n";
    
    if (status == 'S', 's') 
    cost = 1;
    else if (status == 'V', 'v') 
    cost = 5;
    else if (status == 'E', 'e' && vehicle =='C', 'c' && hours <= 4) 
    cost = hours * 5;
    else if (status == 'E', 'e' && vehicle =='M', 'm' && hours <= 4) 
    cost = hours * 2;
    else if (status == 'E', 'e' && vehicle == 'T', 't' && hours <= 4) 
    cost = hours * 10;
    else if (status == 'E', 'e' && vehicle == 'C', 'c' && hours > 4 <= 12) 
    cost = (20) + (hours - 4) * 5 * .75;
    else if (status == 'E', 'e' && vehicle == 'M', 'm' && hours > 4 <= 12) 
    cost = (8) + (hours - 4) * 2 * .75;
    else if (status == 'E', 'e' && vehicle == 'T', 't' && hours > 4 <= 12) 
    cost = (40) + (hours - 4) * 10 * .75;
    else if (status == 'E', 'e' && vehicle == 'C', 'c' && hours > 12) 
    cost = (20) + (30) + (hours - 12) * 5 * .50;
    else if (status == 'E', 'e' && vehicle == 'M', 'm' && hours > 12) 
    cost = (8) + (12) + (hours - 12) * 2 * .50;
    else if (status == 'E', 'e' && vehicle == 'T', 't' && hours > 12) 
    cost = (40) + (60) + (hours - 12) * 10 * .50;
    else 
    exit(1);
    
    cout << "Your total payment for "<< hours << " hours is: $"<< cost << endl << endl;
    system ("pause");
    return 0;
}
Every time I compile and run it, no matter what I input, the result will always be $1.00. Any suggestions as to how I would fix this?

Share this post


Link to post
Share on other sites
Not allowed to help on homework problems, but this:


if (status == 'S', 's')
cost = 1;




is equivalent to:

status == 'S';
if ( 's' )
cost = 1;




is equivalent to:

true;
if ( true )
cost = 1;




is equivalent to:

if ( true )
cost = 1;




And:


if (status == 'E', 'e' && vehicle =='C', 'c' && hours <= 4)



is equivalent to:

if ( true && hours <= 4 )

Share this post


Link to post
Share on other sites
You need to put the || logical operator instead of the comma.



if (status == 'S' || status == 's')
cost = 1;

//same for the rest of the statements with the comma




Share this post


Link to post
Share on other sites
Unrelated to the above, I strongly recommend that you use curly braces for every conditional block. Having


if (foo)
bar;


works, yes, but it lends itself to hard-to-notice bugs that can be easily avoided just by adding a curly brace on either end.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this