# I am a beginner in programming and i have question

### Question

• *Exercise 3:

Write a program that, given the number of kilometers traveled and the gallons of gasoline consumed, produces the kilometers per gallon and the kilometers per liter. In your program you should define a function for kilometers per gallon, and another function for kilometers per liter. This program should repeat as many times as the user wishes.

1 Liter = 0.264172051 Gallon

1 Gallon = 3.7854118 Liter

Example Run:

Enter number of kilometers traveled: 45

Enter number of gallons of gas used: 12

Kilometers per gallon: 3.75

Kilometers per liter: 0.99

Would you like to enter another? [Y][N]: y

Enter number of kilometers traveled: 250

Enter number of gallons of gas used: 10

Kilometers per gallon: 25.00

Kilometers per liter: 6.60

Would you like to enter another? [Y][N]: n

-----------------------------------

my solution

#include<iostream>
using namespace std;
#include <stdio.h>
#include <cctype>
void gallon (double num1);
void liter (double num2);
int main()
{
double liters, gallons;
cout<<"Enter number of kilometers traveled:";
cin>>liters;
cout<<" Enter number of gallons of gas used: ";
cin>>gallons;
gallon(liters);
liter(gallons);
cout<<"\n Would you like to enter another?[y][n]";
char ans;
cin>>ans;
if (ans=="Y")
tolower(ans);
else
if (ans=="N")
tolower(ans);
while(ans=="y")
{
double liters, gallons;
cout<<"Enter number of kilometers traveled:";
cin>>liters;
cout<<"\n Enter number of gallons of gas used: ";
cin>>gallons;
gallon(liters);
liter(gallons);
cout<<"Kilometers per gallon:"<<liters;
cout<<"\n Kilometers per liter: "<<gallons;
cout<<"\n Would you like to enter another?[1.yas][2.no]";
int ans;
cin>>ans;
}
void gallon(double num1)
{
num1=num1*0.264172051;
cout<<"Kilometers per gallon:"<<num1;
}
void liter(double num2)
{
num2=num2*3.7854118;
cout<<"\n Kilometers per liter: "<<num2;
}
return 0;
}

---------------------------------

can any one tell me what is my error ?!

Saturday, November 09, 2013 9:53 AM

### Answers

• #include<iostream>

using namespace std;
#include <stdio.h>
#include <cctype>
void gallon (double num1);
void liter (double num2);
int main()
{
double liters, gallons;
cout<<"Enter number of kilometers traveled:";
cin>>liters;
cout<<" Enter number of gallons of gas used: ";
cin>>gallons;
gallon(liters);
liter(gallons);
cout<<"\n Would you like to enter another?[y][n]";
char ans;
cin>>ans;
if (ans=="Y")
tolower(ans);
else
if (ans=="N")
tolower(ans);
while(ans=="y")
{
double liters, gallons;
cout<<"Enter number of kilometers traveled:";
cin>>liters;
cout<<"\n Enter number of gallons of gas used: ";
cin>>gallons;
gallon(liters);
liter(gallons);
cout<<"Kilometers per gallon:"<<liters;
cout<<"\n Kilometers per liter: "<<gallons;
cout<<"\n Would you like to enter another?[1.yas][2.no]";
int ans;
cin>>ans;
}
void gallon(double num1)
{
num1=num1*0.264172051;
cout<<"Kilometers per gallon:"<<num1;
}
void liter(double num2)
{
num2=num2*3.7854118;
cout<<"\n Kilometers per liter: "<<num2;
}
return 0;
}

can any one tell me what is my error ?!

Did the code you posted even compile without errors?

I hope you didn't type it all in again here. Always use Windows copy & paste to post the
*actual* code you're working with. Always use the "Insert Code Block" feature of the forum
editor when posting code.

if (ans=="Y")

if (ans=="N")

"Y" ad "N" are constant char strings. You should be using 'Y' and 'N' for single character
literals.

Also, the definitions of functions gallon and liter are in the wrong place. That would
cause compile errors also. Move them *after* the closing brace for the main function.

- Wayne

Saturday, November 09, 2013 6:09 PM
• It is always helpful if you tell us what the problem is.  Since your code does not compile, you should not have executed it until you fixed the errors.

If the errors in your posted code are merely typos, next time use cut and paste.  But then we have to wonder why your posted output did not come from this code.  After the user responds y, in your while loop you call gallon (which prints one line), you call liter (which prints a second line), and then you "call" cout twice (which should print a third and fourth line).  Where are those last two lines in your output?  Of course, the fact that your posted numbers did not come from your posted code just further adds to the confusion.

Why did you include stdio.h?  You don't use any of its contents.  If you did need to use some C feature from this header, you would be better off including cstdio.

Why, when you ask the user to enter the number of kilometers, do you store the input in a variable named liters.  All it does is confuse you.

Why do you duplicate the code, once before the while and then within the while?    It would be simpler and less error prone to do all the work within a do ... while block.

Why do you care what the value of the response is before you call tolower?  The expression (tolower(ans) == 'y') tells you everything you need to now.

Your function gallon() converts liters to gallons.  It does nothing related to kilometers per gallon.  Similarly, your function liter converts gallons to liters, unrelated to kilometers per liter.

When you call these two functions, the arguments are passed by value.  Consequently, when these functions return, the values in variables liters and gallons are unchanged.  The data you print in the while loop merely repeats the user's input.

Your first prompt regarding additional data specifies a response of 'y' or 'n'.  Your second prompt at the end of the while loop strongly implies you are looking for a response of '1' or '2'.  It is very misleading.

That second definition of ans in your while loop means that the while statement is not evaluating the latest response.

When inputting a single character using cin, you still use two keystrokes (the second is for the Enter key).  cin will leave the '\n' corresponding to the Enter key in the buffer ready to be processed at the next input request.  Depending on the nature of that request, the extra character could cause unexpected problems.  You might be better off reading the response into a string and processing just the first character.

Output that is not terminated with a '\n' or std::endl might be buffered and not visible to the user until something causes the buffer to be flushed.  Your use of cout is inconsistent.  Some have a leading '\n' and some have no '\n' at all.

By the way, independent of the assignment, kilometers (or miles) per gallon is the common American measure of performance.  The usual European measure is consumption, liters per 100 kilometers.

Saturday, November 09, 2013 8:41 PM
• Well at a glance, you seem to be confusing liters and kilometers.  Your gallon function seems to be converting gallons to liters.  However, it should be printing out kilometers per gallon.  In order to do that it would need to know about both the kilometers traveled and gallons of gas used. (i.e. it should take two parameters and use them both)
Saturday, November 09, 2013 2:37 PM

### All replies

• Well at a glance, you seem to be confusing liters and kilometers.  Your gallon function seems to be converting gallons to liters.  However, it should be printing out kilometers per gallon.  In order to do that it would need to know about both the kilometers traveled and gallons of gas used. (i.e. it should take two parameters and use them both)
Saturday, November 09, 2013 2:37 PM
• #include<iostream>

using namespace std;
#include <stdio.h>
#include <cctype>
void gallon (double num1);
void liter (double num2);
int main()
{
double liters, gallons;
cout<<"Enter number of kilometers traveled:";
cin>>liters;
cout<<" Enter number of gallons of gas used: ";
cin>>gallons;
gallon(liters);
liter(gallons);
cout<<"\n Would you like to enter another?[y][n]";
char ans;
cin>>ans;
if (ans=="Y")
tolower(ans);
else
if (ans=="N")
tolower(ans);
while(ans=="y")
{
double liters, gallons;
cout<<"Enter number of kilometers traveled:";
cin>>liters;
cout<<"\n Enter number of gallons of gas used: ";
cin>>gallons;
gallon(liters);
liter(gallons);
cout<<"Kilometers per gallon:"<<liters;
cout<<"\n Kilometers per liter: "<<gallons;
cout<<"\n Would you like to enter another?[1.yas][2.no]";
int ans;
cin>>ans;
}
void gallon(double num1)
{
num1=num1*0.264172051;
cout<<"Kilometers per gallon:"<<num1;
}
void liter(double num2)
{
num2=num2*3.7854118;
cout<<"\n Kilometers per liter: "<<num2;
}
return 0;
}

can any one tell me what is my error ?!

Did the code you posted even compile without errors?

I hope you didn't type it all in again here. Always use Windows copy & paste to post the
*actual* code you're working with. Always use the "Insert Code Block" feature of the forum
editor when posting code.

if (ans=="Y")

if (ans=="N")

"Y" ad "N" are constant char strings. You should be using 'Y' and 'N' for single character
literals.

Also, the definitions of functions gallon and liter are in the wrong place. That would
cause compile errors also. Move them *after* the closing brace for the main function.

- Wayne

Saturday, November 09, 2013 6:09 PM
• It is always helpful if you tell us what the problem is.  Since your code does not compile, you should not have executed it until you fixed the errors.

If the errors in your posted code are merely typos, next time use cut and paste.  But then we have to wonder why your posted output did not come from this code.  After the user responds y, in your while loop you call gallon (which prints one line), you call liter (which prints a second line), and then you "call" cout twice (which should print a third and fourth line).  Where are those last two lines in your output?  Of course, the fact that your posted numbers did not come from your posted code just further adds to the confusion.

Why did you include stdio.h?  You don't use any of its contents.  If you did need to use some C feature from this header, you would be better off including cstdio.

Why, when you ask the user to enter the number of kilometers, do you store the input in a variable named liters.  All it does is confuse you.

Why do you duplicate the code, once before the while and then within the while?    It would be simpler and less error prone to do all the work within a do ... while block.

Why do you care what the value of the response is before you call tolower?  The expression (tolower(ans) == 'y') tells you everything you need to now.

Your function gallon() converts liters to gallons.  It does nothing related to kilometers per gallon.  Similarly, your function liter converts gallons to liters, unrelated to kilometers per liter.

When you call these two functions, the arguments are passed by value.  Consequently, when these functions return, the values in variables liters and gallons are unchanged.  The data you print in the while loop merely repeats the user's input.

Your first prompt regarding additional data specifies a response of 'y' or 'n'.  Your second prompt at the end of the while loop strongly implies you are looking for a response of '1' or '2'.  It is very misleading.

That second definition of ans in your while loop means that the while statement is not evaluating the latest response.

When inputting a single character using cin, you still use two keystrokes (the second is for the Enter key).  cin will leave the '\n' corresponding to the Enter key in the buffer ready to be processed at the next input request.  Depending on the nature of that request, the extra character could cause unexpected problems.  You might be better off reading the response into a string and processing just the first character.

Output that is not terminated with a '\n' or std::endl might be buffered and not visible to the user until something causes the buffer to be flushed.  Your use of cout is inconsistent.  Some have a leading '\n' and some have no '\n' at all.

By the way, independent of the assignment, kilometers (or miles) per gallon is the common American measure of performance.  The usual European measure is consumption, liters per 100 kilometers.

Saturday, November 09, 2013 8:41 PM
• Since your code does not compile, you should not have executed it until you fixed the errors.

Actually. *could* not have executed it ...

>If the errors in your posted code are merely typos, next time use cut and paste.
>But then we have to wonder why your posted output did not come from this code.

The posted output is part of the Exercise description, by way of example as to
what's required/expected.

- Wayne

Saturday, November 09, 2013 8:53 PM
• By the way, independent of the assignment, kilometers (or miles) per gallon is the common American measure of performance.  The usual European measure is consumption, liters per 100 kilometers.

Additional trivia:

1 Gallon = 3.7854118 Liter

This is the conversion factor for a U.S. liquid gallon.

A U.K. gallon (previously used in Canada as well) = 4.546087 litres

- Wayne

Saturday, November 09, 2013 9:04 PM