Can anyone please tell me how can I have one function that takes an argument that represents a variable value selected in the following switch statement ?
Double InputDate (char ch)
{
cout<<"Enter M to caluculate mileage , D for distance traveled ,G for Gal per miles and Q to quit the program ";
cin>>x ;
cout << x << endl ;
switch (x)
{
case 'm':
case 'M':
MilesperGal();
break ;
case 'd':
case 'D':
mileage();
break ;
case 'g':
case 'G':
fuelUsed ();
break;
default:
return 0 }
void InputDate ()
{
char x ;
cout<<"Enter M to caluculate mileage , D for distance traveled ,G for Gal per miles and Q to quit the program ";
cin>>x ;
cout << x << endl ;
switch (x)
{
case'm':
case'M':
MilesperGal();
break ;
case'd':
case'D':
mileage();
break ;
case'g':
case'G':
fuelUsed ();
break;
default:
return ;
}
masiht, your missing the point. A function doesn't have a 'type'. It has a return value which has a type. If your return type is Double and you don't use it, then what is the point in having that double?
int InputDate ()
{
char myChar;
cout << "Enter M to caluculate mileage , D for distance traveled ,G for Galper miles and Q to quit the program " << endl;
cin >> myChar;
cout << myChar << endl ;
switch (x)
{
case'm':
case'M':
MilesperGal();
break;
case'd':
case'D':
mileage();
break ;
case'g':
case'G':
fuelUsed ();
break;
default:
cout << x << "is not a valid command letter. Please choose another" << endl;
return 1; //Same thing with a boolean. If it doesn't have a matching case, return 1 to note that is was a failure.
}
return 0; //or return 0 to note that the function succeeded.
Thank you computerquip !
I usually make small programs but this time i decided to make bigger program.I have read some good books but still not able to make this program.Can you please tell me what is the problem with this program.
#include<iostream>
using namespace std;
double MilesperGal(double F,double m)
{
double M;
What is the user supposed to enter on the keyboard?
This declares a function:
1 2 3
int main() {
float foo( int x, float y );
}
This calls a function:
1 2 3 4 5
int main() {
int x = 5;
float y = 3.14;
float result = foo( x, y );
}
Note how when declaring a function you specify the types of parameters and the return type of the function and when calling a function you do not specify the types.
Dear masiht,
I think you forgot that when we invoke a function then we do not write its return type.
Correct Code is as follows
#include<iostream.h>
#include<conio.h>
double MilesperGal(double F,double m)
{
double M;
M = m/F;
return M;
};
double mileage(double F,double M)
{
double m;
m=F*M;
return m;
}
double fuelUsed (double M,double MPG)
{
double f;
f=M/MPG;
return f;
}
void main()
{
double FuelUsed;
double Mileage;
double MilesPerGal;
char x ;
cout<<"Enter M to caluculate mileage , D for distance traveled ,G for Gal per miles and Q to quit the program ";
cin>>x ;
cout << x << endl ;
switch (x)
{
case 'm':
case 'M':
cout << "enter the fuel used";
cin>>FuelUsed;
cout<<"enter mileage ";
cin>>Mileage;
cout<<MilesperGal(FuelUsed,Mileage);
;
break ;
case 'd':
case 'D':
cout << "enter the fuel used";
cin>>FuelUsed;
cout<<"enter MPG ";
cin>>MilesPerGal;
cout <<mileage(FuelUsed,MilesPerGal );
break ;
case 'g':
case 'G':
cout << "enter the Mileage used";
cin>>Mileage;
cout<<"enter mileage ";
cin>>MilesPerGal;
cout<<fuelUsed (Mileage,MilesPerGal) ;
break;
default:cout<<"Wrong choice";
break; }
getch();
}
I hope your problem is solved now.
with regards,
matanuragi
Dear mashit,
I have used void main function.
you just do one thing, in your earlier program change the switch statement with this one
switch (x)
{
case 'm':
case 'M':
cout << "enter the fuel used";
cin>>FuelUsed;
cout<<"enter mileage ";
cin>>Mileage;
cout<<MilesperGal(FuelUsed,Mileage);
;
break ;
case 'd':
case 'D':
cout << "enter the fuel used";
cin>>FuelUsed;
cout<<"enter MPG ";
cin>>MilesPerGal;
cout <<mileage(FuelUsed,MilesPerGal );
break ;
case 'g':
case 'G':
cout << "enter the Mileage used";
cin>>Mileage;
cout<<"enter mileage ";
cin>>MilesPerGal;
cout<<fuelUsed (Mileage,MilesPerGal) ;
break;
default:cout<<"Wrong choice";
break;
}
this will definitely help you.
with regards,
matanuragi