Hello dmunoz,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button, but you may still to indent your code.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
You should compile your code and try to fix the errors and warnings before you post it. I get this when I compiled it wit VS2017 with the C++14 standards.
Severity Code Description Project File Line Suppression State
Warning C4101 'yes': unreferenced local variable f:\vs 2017\original code.cpp 15
Error C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. original code.cpp 20
Error C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. original code.cpp 29
Error C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. original code.cpp 38
Warning C4477 'scanf' : format string '%i' requires an argument of type 'int *', but variadic argument 1 has type 'float *' original code.cpp 29
Warning C4477 'scanf' : format string '%i' requires an argument of type 'int *', but variadic argument 1 has type 'char *' original code.cpp 20
Warning C4101 'no': unreferenced local variable original code.cpp 16
Warning C4101 'mileRate': unreferenced local variable original code.cpp 8
Warning C4101 'distanceCost': unreferenced local variable original code.cpp 13
Warning C4244 '=': conversion from 'float' to 'int', possible loss of data original code.cpp 49
Warning C4244 '=': conversion from 'double' to 'int', possible loss of data original code.cpp 23
Warning C4244 '=': conversion from 'double' to 'int', possible loss of data original code.cpp 26
Warning C4244 '=': conversion from 'double' to 'int', possible loss of data original code.cpp 41
Warning C4244 '=': conversion from 'double' to 'int', possible loss of data original code.cpp 44
Warning C4552 '*': result of expression not used original code.cpp 35
|
That is just what I get.
Looking at your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
int main()
{
float distanceRate;
float distanceMiles;
int passengers;
int mileRate;
int firstpassengerCost;
int addpassengerCost;
int passengerCost;
int airportFee;
int distanceCost;
int totalFare;
char yes;
char no;
char airport;
printf("Are you coming from or to the airport - 1 for yes, 0 for no \n");
scanf("%i", &airport); /*User enters 0 or no*/
if (airport = 1)
airportFee = 2.00;
else
airportFee = 0.00;
printf("Enter distance to destination: \n");
scanf(" %i", &distanceMiles);
if (distanceMiles <= 1)
distanceRate = 3.50;
else if (distanceMiles > 1)
distanceMiles * distanceRate;
|
Looking at the variables some are "float"s and some are "int"s. When storing a "float" in an "int" it will work, but you will loose the decimal portion of the "float" and only store the whole number.
You should initialize your variables when defined. A couple of those variables generate an error when used before they receive a usable value.
Line 20:
if (airport = 1)
has more than one problem. First the "=" sets "airport" to 1 which makes the condition always true even if you did not want this. so you will always be adding "2" even if you do not want to. What you need here is "==" which means to compare. Second "airport" is defined as a "char", so setting a "char" equal to an "int" will not give you what you may be thinking.
Then there is the prompt. If you are looking for a "Y" (Yes) or "N" (No) answer then say so. Also in the "scanf" you want to use "c" not "i". Removing the "\n" from the end of the string will put the input on the same line.
This should work better for you:
1 2 3 4 5 6 7 8 9 10
|
printf("Are you coming from or to the airport - (Y / N): ");
scanf("%c", &airport); //User enters 'Y' or 'N'.
if (airport == 'y' || airport == 'n') // <--- Changes case of letter.
airport -= 32;
if (airport == 'Y')
airportFee = 2.00;
else
airportFee = 0.00;
|
There may be a better way to do the if statement at line 4, but I have not worked with C for awhile and this is the first thing that came to mind. C++ has the function "std::toupper()" and "std::olower()" for this. Not sure about C.
In line 7 this is how to compare 2 characters.
For the next part:
1 2 3 4 5 6 7 8
|
printf("Enter distance to destination: ");
scanf(" %i", &distanceMiles);
if (distanceMiles <= 1)
distanceRate = 3.50;
else if (distanceMiles > 1)
distanceMiles * distanceRate;
|
In line 2. The specifier should be an "f", for a floating point number, not the "i", which is an "int". With this mismatch you are most likely getting a very large negative number. Not what you want, but may work and not be noticeable.
In line 8. What is the value of "distanceRate"? Actually it makes no difference because the line does nothing. I am not sure, but I think you would want something more like this:
1 2 3 4 5 6
|
if (distanceMiles <= 1)
distanceRate = 3.50;
else if (distanceMiles > 1)
distanceRate = 3.00;
something = distanceMiles * distanceRate;
|
Without the original instructions I can not say if you are even going in the right direction.
It would help if you would post thee complete instruction for this program.
For now that is as much as I have worked on. I will look the rest and see what I can find.
Andy