Reputation: 69
I have an error when I try to build my program reading: 'error: 'celsius()' was not declared in this scope'
Now, correct me if I'm wrong but I think the problem is that because the function 'fahrenheit' comes before my other function 'celsius' when I call it in the fahrenheit function, it won't work. Now, it would be simple enough to switch them around but fahrenheit is also called in the celsius function.
In python, all you need to do is just globalise it with the 'global' syntax so what is the C++ equivalent?
Thanks
PS. Here is my code if you want it.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int fahrenheit(){
system("CLS");
cout << "-----------------------------------------------";
cout << "\nYOU HAVE CHOSEN FAHRENHEIT TO CELSIUS MODE";
cout << "\n----------------------------------------------";
bool again;
again = true;
while (again == true){
int tempurf;
cout << "\nFahrenheit Temperature to be Converted: ";
cin >> tempurf;
int tempurc;
tempurc = tempurf - 32;
tempurc = tempurc * 5;
tempurc = tempurc / 9;
cout << "\n\n" << tempurf << " F is " << tempurc << " C";
cout << "\n\n\n\nWHAT WOULD YOU LIKE TO DO: ";
cout << "\n - ANOTHER CONVERSION TYPE A";
cout << "\n - FOR CELSIUS MODE TYPE C";
cout << "\n - TO EXIT TYPE E";
bool goodc;
goodc = false;
while (goodc == false){
string choosing;
cout << "\n ";
cin >> choosing;
if (choosing == "A" or choosing == "a"){
system("CLS");
goodc = true;
}
else if (choosing == "C" or choosing == "c"){
goodc = true;
again = false;
celsius();
}
else if (choosing == "E" or choosing == "e"){
goodc = true;
again = false;
return 0;
}
else{
cout << "\n Invalid Choice";
}
}
}
}
int celsius(){
system("CLS");
cout << "---------------------------------------------";
cout << "\nYOU HAVE CHOSEN CELSIUS TO FAHRENHEIT MODE";
cout << "\n---------------------------------------------";
bool again;
again = true;
while (again == true){
int tempuc;
cout << "\nCelsius Temperature to be Converted: ";
cin >> tempuc;
int tempuf;
tempuf = tempuc * 9;
tempuf = tempuf / 5;
tempuf = tempuf + 32;
cout << "\n\n" << tempuc << " C is " << tempuf << " F";
cout << "\n\n\n\nWHAT WOULD YOU LIKE TO DO: ";
cout << "\n - ANOTHER CONVERSION TYPE A";
cout << "\n - FOR FAHRENHEIT MODE TYPE F";
cout << "\n - TO EXIT TYPE E";
bool goodc;
goodc = false;
while (goodc == false){
string choosing;
cout << "\n ";
cin >> choosing;
if (choosing == "A" or choosing == "a"){
system("CLS");
goodc = true;
}
else if (choosing == "F" or choosing == "f"){
goodc = true;
again = false;
fahrenheit();
}
else if (choosing == "E" or choosing == "e"){
goodc = true;
again = false;
return 0;
}
else{
cout << "\n Invalid Choice";
}
}
}
}
int main(){
cout << "Welcome to the Fahrenheit/Celsius Converter!";
cout << "\n By Ben Sarachi";
cout << "\n\nWhich way would you like to convert to:";
cout << "\n - If you would like Fahrenheit to Celsius please type F";
cout << "\n - If you would like Celsius to Fahrenheit please type C";
// GC stands for good choice
bool gc;
gc = false;
while (gc == false){
string choice;
cout << "\n ";
cin >> choice;
//Call Functions
if (choice == "F" or choice == "f"){
gc = true;
fahrenheit();
}
else if (choice == "C" or choice == "c"){
gc = true;
celsius();
}
else{
cout << "Invalid Choice";
}
}
}
Upvotes: 0
Views: 314
Reputation: 304
Separate function declaration from definition. Change your code to something like this:
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
int celsius();
int fahrenheit();
int fahrenheit()
{
// ...
}
int celsius()
{
// ...
}
int main(int argc, char** argv)
{
// ...
}
Upvotes: 0
Reputation: 2257
You want to add a forward declaration for your function so that the compiler knows the function exists. What's happening is that Fahrenheit is calling Celsius, but the compiler doesn't know what Celsius is at that point.
At the top of your code, add the following just below your includes:
int fahrenheit();
int celsius();
This tells the compiler that you will be defining those functions at some point.
Then you can declare your functions in any order in the file that you like.
Also, for future reference, that forward declaration should have the same signature as your function. So if you had a function like:
void foo(int bar) { ... }
then your forward declaration would be:
void foo(int);
Upvotes: 4
Reputation: 16328
You get the error because at the time of compiling the fahrenheit()
function, celsius()
is not known. You have to forward declare it.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int celsius(); // this is the forward declaration
int fahrenheit(){
// do something
celsius();
}
int celsius(){
// implement the function
}
The alternative is you create a class and put the two functions as member of that class. You won't need forward declaration then (though declaring a class is arguably another form of that).
You have several other problems in your code:
or
here choice == "F" or choice == "f"
? is it #defined somewhere as ||
?gc == false
or gc == true
. prefer to use gc
and !gc
as in if(gc)
or while(!gc)
Upvotes: 0
Reputation: 21749
What you need is function forward declarations. Put the following string before definition of fahrenheit
function:
int celsius();
This will tell the compiler, that such a function exists and has the following prototype. But the body will be introduce at some point later.
Upvotes: 0