Input value and If statement

I'm christian I'm currently a beginner in C++ and trying to learn but I guess I'm on my limits and I've already tried other things but I can't seem to make my program work. This is also one of my course requirement and I'm really having a hard time figuring out how to fix this.

Basically the user will input the station (a, b, c) and the program should compute for the fee but after entering a, b, or c the program outputs 0.00000 then any key will automatically close the interface.

Here is a sample of the code I wanted to work with:

#include<stdio.h>
#include<conio.h>

main()

{
float fee, to, from, a, b, c;
printf("Enter your current station: ");
scanf("%f",&from);
printf("Enter the station you want to go: ");
scanf("%f",&to);

if((to == a && from == b))
a = 10;
b = 15;
fee = to + from;
printf("%f", fee);

if((to == b && from == a))
a = 10;
b = 15;
fee = to + from;
printf("%f", fee);

if((to == c && from == a))
a = 10;
c = 5;
fee = to + from;
printf("%f", fee);

if((to == a && from == c))
a = 10;
c = 5;
fee = to + from;
printf("%f", fee);

if((to == b && from == c))
a = 10;
c = 5;
fee = to + from;
printf("%f", fee);

getch();
}

Hope anyone could help me, thank you for reading I really appreciate it.

Upvotes: 0

Views: 75

Answers (2)

Memento Mori
Memento Mori

Reputation: 3402

This line:

if((to == a && from == b))

is comparing values to a and b when neither a or b have been initialized. You have to give them values before you compare your variables to them.

You also don't have brace brackets after your if statements. They're probably not doing what you think they are. They way the code is now, only the line directly after each if statement will execute conditionally.

EDIT: I didn't notice.. You are getting input as floats. I think what you wanted to do was read in to and from as chars and write your if statements like this:

 if((to == 'a' && from == 'b'))

Upvotes: 3

Markus Deibel
Markus Deibel

Reputation: 1329

You're trying to store your users input of the charater 'a', 'b' or 'c' into a float variable. This will not work.

Whatever belongs to one if-statement needs to go into one block:

if(condition)
{
    operation1
    operation2
    ...
}

Also, check what variables you are adding to get the value for fee.


"You can see a lot by just looking." - Yogi Berra

Upvotes: 1

Related Questions