function with switch statement

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 }
Last edited on
Remove the return value if you don't need it and move the variable definition in the body of the function:

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
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 ;
 }
Can I use double Inputdata as a type of the function instead of void ?
closed account (S6k9GNh0)
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?

At most:
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
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. 
Last edited on
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;



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;


}
int 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 <<double MilesperGal(double F,double m);

break ;
case 'd':
case 'D':
cout << "enter the fuel used";
cin>>FuelUsed;
cout<<"enter MPG ";
cin>>MilesPerGal;


cout <<double mileage(double F,double M);

break ;
case 'g':
case 'G':
cout << "enter the Mileage used";
cin>>Mileage;
cout<<"enter mileage ";
cin>>MilesPerGal;
cout << double fuelUsed (double M,double MPG) ;



default:
return 0; }

}
Last edited on
double FuelUsed;
double Mileage;
double MilesPerGal;

all are uninitialized, so have random values.

Not that you use them, because all of your cout's in the switch are not calling the function anyway.

cout << double MilesperGal(double F, double m);

This is a compile error.

I have tried to use these variables in several ways but none of them work .
Can you please tell me how to fix it ?
What is your program trying to do?

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
How come this program doesn't have int main() in it ?
what is conio.h ?
Sir matanuragi !
This program is still not working.
It says

------ Build started: Project: fdsfas, Configuration: Debug Win32 ------
Compiling...
fasdf.cpp
c:\users\pro standard f.s co\desktop\sf grasscut\oscar\fdsfas\fdsfas\fasdf.cpp(1) : fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
Build log was saved at "file://c:\Users\Pro Standard F.S Co\Desktop\SF grasscut\oscar\fdsfas\fdsfas\Debug\BuildLog.htm"
fdsfas - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
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
Topic archived. No new replies allowed.