It tells me error id returned 1 exit status when I try to run the code and have search it up from what i have seen is that you mostly get this error from misspelling main() function but that is not the case here.
#include <iostream>
#include <string>
using namespace std;
int main(){
double gallons();
double miles();
double meters();
char option;
cout<<"This program converts english units to metrics:"<<endl;
cout<<"--------------------------------------------------"<<endl;
cout<<"--------------- Select any option ----------------"<<endl;
cout<<"press g to convert gallon to liters"<<endl;
cout<<"press m to convert miles to kilometers"<<endl;
cout<<"press f to convert meters to feet"<<endl;
cin>>option;
if(option =='G' || option == 'g'){
gallons();
}
if(option =='M' || option == 'm'){
miles();
}
if(option =='F' || option == 'f'){
meters();
}
gallons();{
double gallon;
cout<<"Enter the amount of gallon you want to convert to liters"<<endl;
cin>>gallon;
gallon = gallon * 3.78541;
cout<<"You have "<<gallon<<" liters"<<endl;
}
miles();{
double mile;
cout<<"Enter the miles covered that you want to convert to kilometers"<<endl;
cin>>mile;
mile = mile * 1.60934;
cout<<"You have covered "<<mile<<" kilometers"<<endl;
}
meters();{
double meter;
cout<<"How many meters do you want to convert to feet?"<<endl;
cin>>meter;
meter = meter * 3.28084;
cout<<"You have covered "<<meter<<" kilometers"<<endl;
}
return (0);
}
This is the end of the code.
CodePudding user response:
In your code:
- Functions declaration should be above main().
- Functions definition should be outside main().
- Since you're printing the output in your function itself, declare the return type of your functions as
void
.
Do the above changes and it will work:
#include <iostream>
#include <string>
using namespace std;
void gallons();
void miles();
void meters();
int main(){
char option;
cout<<"This program converts english units to metrics:"<<endl;
cout<<"--------------------------------------------------"<<endl;
cout<<"--------------- Select any option ----------------"<<endl;
cout<<"press g to convert gallon to liters"<<endl;
cout<<"press m to convert miles to kilometers"<<endl;
cout<<"press f to convert meters to feet"<<endl;
cin>>option;
if(option =='G' || option == 'g'){
gallons();
}
if(option =='M' || option == 'm'){
miles();
}
if(option =='F' || option == 'f'){
meters();
}
}
void gallons() {
double gallon;
cout<<"Enter the amount of gallon you want to convert to liters"<<endl;
cin>>gallon;
gallon = gallon * 3.78541;
cout<<"You have "<<gallon<<" liters"<<endl;
}
void miles() {
double mile;
cout<<"Enter the miles covered that you want to convert to kilometers"<<endl;
cin>>mile;
mile = mile * 1.60934;
cout<<"You have covered "<<mile<<" kilometers"<<endl;
}
void meters() {
double meter;
cout<<"How many meters do you want to convert to feet?"<<endl;
cin>>meter;
meter = meter * 3.28084;
cout<<"You have covered "<<meter<<" feet"<<endl;
}
CodePudding user response:
One of the mistakes you've made is that you haven't properly declared or defined you're functions. If you want to take the declare then define approach, you'll want to move you're declaration to the global scope and out of the main()
function. Also because they don't return anything they should be declared void:
#include <iostream>
#include <string>
using namespace std;
void gallons();
void miles();
void meters();
int main()
{ /// Code goes here }
This has the benefit of allowing you call the code and work on the definition later. One thing with functions is that a semi-colon after a function is only necessary if it is a declaration not for the definition as such, to define the functions you can do this:
#include <iostream>
#include <string>
using namespace std;
void gallons();
void miles();
void meters();
int main()
{ /// Code goes here }
void gallons()
{
double gallon;
cout << "Enter the amount of gallon you want to convert to litres" << endl;
cin >> gallon;
gallon = gallon * 3.78541;
cout << "You have " << gallon << " litres" << endl;
}
void miles()
{
double mile;
cout<<"Enter the miles covered that you want to convert to kilometers"<<endl;
cin>>mile;
mile = mile * 1.60934;
cout<<"You have covered "<<mile<<" kilometers"<<endl;
}
void meters()
{
double meter;
cout << "How many meters do you want to convert to feet?" << endl;
cin >> meter;
meter = meter * 3.28084;
cout << "You have covered "<<meter<<" kilometres" << endl;
}
Finally it is considered bad practice to use the line using namespace std;
as later on when you use other namespaces from libraries, frameworks etc you might do the same thing and its generally not good as this bring naming conflicts between libraries so try to avoid that. A better approach so you don't have to keep writing std::cout
, 1std::cinand
std::endleverywhere with the
std::` is to write:
using std::cout;
using std::cin;
using std::endl;
This has the same effect but only occurs for those two names. Overall, the code should look something like this.
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
void gallons();
void miles();
void meters();
int main()
{
char option;
cout << "This program converts English units to metrics:" << endl;
cout <<"--------------------------------------------------" << endl;
cout <<"--------------- Select any option ----------------" <<e ndl;
cout <<"press g to convert gallon to litres" << endl;
cout <<"press m to convert miles to kilometres"<< endl;
cout <<"press f to convert meters to feet" << endl;
cin >> option;
if(option == 'G' || option == 'g')
{
gallons();
}
if(option =='M' || option == 'm')
{
miles();
}
if(option =='F' || option == 'f')
{
meters();
}
return 0;
}
void gallons()
{
double gallon;
cout << "Enter the amount of gallon you want to convert to litres" << endl;
cin >> gallon;
gallon = gallon * 3.78541;
cout << "You have " << gallon << " litres" << endl;
}
void miles()
{
double mile;
cout<<"Enter the miles covered that you want to convert to kilometers"<<endl;
cin>>mile;
mile = mile * 1.60934;
cout<<"You have covered "<<mile<<" kilometers"<<endl;
}
void meters()
{
double meter;
cout << "How many meters do you want to convert to feet?" << endl;
cin >> meter;
meter = meter * 3.28084;
cout << "You have covered "<<meter<<" kilometres" << endl;
}
Hope all this makes sense.