Aug 9, 2012 at 9:10pm UTC
Programming I am working on for class this week requires that I use functions to pass information from one function to the other. It appears to me that I have done it correctly but an error keeps coming up. The error is occuring in the CtoF and FtoC functions. I have bolded them. Thanks.
My code is:
#include <iostream>
#include <iomanip>
using namespace std;
void displayMenu (void);
char getMenuSelection (void);
void getStartEndAndIncrement(float& start, float& end, float& increment);
float CtoF(float);
void displayCtoFTable(float, float, float);
float FtoC(float);
void displayFtoCTable(float,float,float);
char userchoice;
int main()
{
float start,end,increment;
do
{
displayMenu ();
userchoice=getMenuSelection ();
getStartEndAndIncrement (start,end,increment);
if (userchoice=='C' || userchoice=='c')
{
displayCtoFTable(start,end,increment);
}
else if (userchoice =='F' || userchoice=='f')
{
displayFtoCTable(start,end,increment);
}
}
while (userchoice !='Q'|| userchoice !='q');
void displayMenu ();
{
cout << right << setw(45) << "Celsius and Fahrenheit Temperation Converter\n" << endl;
cout << "\n";
cout << right << setw(45) << "Please choose from the following options\n" << endl;
cout << "\n";
cout << right << setw(45) << "C: Convert from CELSIUS to FAHRENHEIT" << endl;
cout << right << setw(45) << "F: Convert from FAHRENHEIT to CELSIUS" << endl;
cout << right << setw(45) << "Q: Quit" << endl;
cout << "\n";
}
char getMenuSelection ();
{
cout << "Enter your choice: ";
cin >> userchoice;
switch (userchoice)
{
case 'c':
case 'C':
cout <<"\nYou chose a Celsius to Fahrenheit conversion. "<<endl;
return userchoice;
break;
case 'f':
case 'F':
cout <<"\nYou chose a Fahrenheit to Celsius conversion. "<<endl;
return userchoice;
break;
case 'q':
case 'Q':
cin.get();
cout <<"\nPress ENTER to terminate program.."<<endl;
cin.get();
exit(0);
return userchoice;
break;
}
do
{
cout << endl;
cout << "Invalid choice, press ENTER\n"<<endl;
cin.ignore(2);
displayMenu ();
getMenuSelection ();
break;
}
while (userchoice != 'C' && userchoice != 'F' &&
userchoice != 'Q');
}
void getStartEndAndIncrement(float& start, float& end, float& increment);
{
cout << "\nEnter your starting temperature:";
cin >> start;
cout << "Enter your ending temperature:";
cin >> end;
cout << "Enter the temperature increment:";
cin >> increment;
}
float CtoF (float celsius)
{
float Fa;
Fa = 1.8 * celsius + 32.0;
cout << fixed << cout.precision(1);
return Fa;
}
float FtoC (float farh)
{
float Ce;
Ce = (5.0*(farh - 32.0))/9.0;
cout << fixed << cout.precision(1);
return Ce;
}
void displayCtoFTable (float start, float end, float increment)
{
cout << endl;
cout << setw(10) <<"Increment: " << increment<<endl;
cout << "---------------------------------------------- " << endl;
cout << "|*********** Celcius to Farenheit ***********|" << endl;
cout << "---------------------------------------------- " << endl;
cout << "| C"<<char (248)<<" || F"<<char (248)<<" |" << endl;
cout << "---------------------------------------------- " << endl;
for (float celsius = start; celsius <= end; celsius+=increment)
{
cout <<"|"<< setw(10)<<celsius<<char(248)<<setw(11)<<"||"
<<setw(13)<<CtoF(celsius)<<char(248)<<setw(11)<<"|"<<"\n";
}
cout << "----------------------------------------------" << endl;
cout << "----------------------------------------------" << endl;
cout << "\nPress enter to continue...";
cin.ignore(2);
}
void displayFtoCTable(float start, float end, float increment)
{
cout << endl;
cout << setw(10) <<"Increment: " << increment<<endl;
cout << "----------------------------------------------" << endl;
cout << "|*********** Farenheit to Celsius ***********|" << endl;
cout << "----------------------------------------------" << endl;
cout << "| F"<<char(248)<<" || C"<<char (248)<<" |" << endl;
cout << "----------------------------------------------" << endl;
for (float farh = start; farh <= end; farh+=increment){
cout <<"|"<< setw(10)<<farh<<char(248)<<setw(11)<<"||"
<<setw(13)<<FtoC(farh)<<char(248)<<setw(9)<<"|"<<"\n";}
cout << "----------------------------------------------" << endl;
cout << "----------------------------------------------" << endl;
cout << "\nPress enter to continue...";
cin.ignore(2);
}
return (0);
}
Aug 9, 2012 at 11:20pm UTC
1>c:\users\brad\documents\visual studio 2010\projects\week 5\week 5\fah_cel.cpp(112): error C2601: 'CtoF' : local function definitions are illegal
Looks like it is not accepting my values being put into the functions.
Last edited on Aug 9, 2012 at 11:23pm UTC
Aug 9, 2012 at 11:24pm UTC
No it means you need to move the functions (all of them) out of main.
Aug 9, 2012 at 11:37pm UTC
Naraku - where would they go then? No matter where I place them or declare them it gives me an error.
Aug 10, 2012 at 12:52am UTC
Move the functions below main and fix a few semi-colons and you should be able to compile.