I'm a vb.net guy and I'm trying to learn C++. I understand the OO concept very well, and I consider myself a decent programmer. Currently what I'm using is Microsoft Visual C++ 2010 Express and I'm trying to create a console application that will convert Fahrenheit to Celsius:
#include <iostream>
usingnamespace std;
int main()
{
cout << "This is a Fahrenheit to Celcius conversion tool. The temperature can be in two different formats: 74 -or- 74.0.";
//Loop until the user doesn't want to continue
bool looping = true;
do
{
looping = conversion;
} while (looping = true);
return 0;
}
bool conversion()
{
//fahrenheit and celsius variables
double fahrenheit;
double celsius;
cout << "Please type in the Fahrenheit degree:";
//fahrenheit will be what the user entered
cin >> fahrenheit;
//celsius converted here
celsius = f_to_c(fahrenheit);
//print out the celsius degree
cout << fahrenheit << "degrees = " << celsius << "degrees";
cout << "Continue? y - yes or n - no";
//return true/false based on if the user wants to keep converting
if (cin >> "y")
{
returntrue;
}
else
{
returnfalse;
}
}
double f_to_c(double f)
{
//celsius = fahrenheit - 32 * 5 / 9, ignoring PEMDAS
double c = (f - 32) * 5 / 9;
//return the celsius value
return c;
}
However, when building this I get an error:
-edit- it's to big to put in this post, so I'll put it in the next post.
It seems as though I'm doing everything correctly, but I don't know, I'm no C++ expert :P
#include <iostream>
usingnamespace std;
//Forward declare our two functions
bool conversion();
double f_to_c(double f);
//Main!
int main()
{
cout << "This is a Fahrenheit to Celcius conversion tool. The temperature can be in two different formats: 74 -or- 74.0.";
//Loop until the user doesn't want to continue
bool looping = true;
do
{
looping = conversion;
{};
} while (looping == true);
return 0;
}
bool conversion()
{
//fahrenheit and celsius variables
double fahrenheit;
double celsius;
cout << "Please type in the Fahrenheit degree:";
//fahrenheit will be what the user entered
cin >> fahrenheit;
//celsius converted here
celsius = f_to_c(fahrenheit);
//print out the celsius degree
cout << fahrenheit << "degrees = " << celsius << "degrees";
cout << "Continue? y - yes or n - no";
//return true/false based on if the user wants to keep converting
if (cin >> "y")
{
returntrue;
}
else
{
returnfalse;
}
}
double f_to_c(double f)
{
//celsius = fahrenheit - 32 * 5 / 9, ignoring PEMDAS
double c = (f - 32) * 5 / 9;
//return the celsius value
return c;
}
I've also added two brackets after conversion as suggested in post #2. Unfortunately it still won't build. This is the error I get:
-Edit- Still to large, I will try to post it on the next post... again.
I didn't think that I was defining them in main, I thought that declaring the two functions above the Main() was forward declaring. Although I did just google forward declare c++ and that's how it told me to do it.
#include <iostream>
usingnamespace std;
//Forward declare our two functions
bool conversion();
double f_to_c(double f);
//Main!
int main()
{
cout << "This is a Fahrenheit to Celcius conversion tool. The temperature can be in two different formats: 74 -or- 74.0.";
//Loop until the user doesn't want to continue
while( conversion() ){;}
return 0;
}
bool conversion()
{
//fahrenheit and celsius variables
double fahrenheit;
double celsius;
cout << "Please type in the Fahrenheit degree:";
//fahrenheit will be what the user entered
cin >> fahrenheit;
//celsius converted here
celsius = f_to_c(fahrenheit);
//print out the celsius degree
cout << fahrenheit << "degrees = " << celsius << "degrees";
char choice;
cin >> choice;
//return true/false based on if the user wants to keep converting
if (choice == 'y')
{
returntrue;
}
else
{
returnfalse;
}
}
double f_to_c(double f)
{
//celsius = fahrenheit - 32 * 5 / 9, ignoring PEMDAS
double c = (f - 32) * 5 / 9;
//return the celsius value
return c;
}
And it works. My next question is, in the conditional statement where I check if it's y or not, why do I use single quotation marks instead of double? I would assume that with y being a string, it'd be double quotes.
Oh ok, that makes sense. I'm trying to learn C++ by diving into projects, just like I did with vb.net. Starting from small ones like this to full blown data management systems, so it's all a learning process. More than likely I'll start up a new thread :]