Hello, I recently started taking classes in c++, and one of our assignments is to create a program that converts Fahrenheit to Celsius degrees. I was able to do so, however I want to make the conversion part into a subprogram, I have some idea on how to write it, but whenever I do it crashes or something bad happens. Any help is appreciated thanks.

#include <iostream>
#include <iomanip>

using namespace std;

float FahrenheitToCelcius(float fahrenheit);

int main()
{
	float fahrenheit;
	float celsius;
	int choice;
	float FahrenheitToCelsius;
	do{

	cout << "This program converts fahrenheit to celsius" << endl << endl ;
	cout << "Please Enter Fahrenheit Degrees: ";
	cin >> fahrenheit; 
	cout << "Your Conversion Is: "<< fahrenheit << " Degree Fahrenheit to:" << endl;
	cout << FahrenheitToCelsius << fixed << setprecision(2) <<" Degrees Celsius.";
	cout << "Want To Play Again?";
	cout << "1 == Yes";
	cout << "2 == No " << endl;
	cin >> choice;

	} while (choice == 1);

	cin.get();
	return 0;
}

float FahrenheitToCelcius(float celsius, float fahrenheit)
{
	celsius = (5.0/8.0) * (fahrenheit - 32);
	return celsius;
}

Well you have some serious issues there.
On line 20 you try to cout a function. Uh-oh, that does not work at all.
You would write

cout << FahrenheitToCelsius(fahrenheit) << " Degrees Celsius";

Also, on line 32 your function definition mismatches the declaration up on line 6.

Make sure both lines read

float FahrenheitToCelcius(float fahrenheit)

Also, delete line 13.


I am surprised you say it crashes. I wouldn't think this would even compile.
Please make the corrections I suggested and post back with your results :)

Ps. You might want to call it function not subprogram, although it is probably not wrong ;-)

Also your formula for converting is wrong it is 5.0/9.0 not 5.0/8.0.

I have just started in C + + and I had this program to do: Write a program that inputs two (2) numbers and determines which of the two numbers is the smallest. If the numbers are equal, display a message that they are equal.

error C2447: '{' : missing function header (old-style formal list?)

Here is the program:

#include <iostream>

{

//add function definitions for input and output

int myNumber = 7;

if (myNumber > 4); 
     cout << “A true statement” << endl;
     cout << myNumber << “ is greater than five” << endl;

     if (myNumber == 7); 
          cout << myNumber << “ is a great number.”;
          cout << “It can be divided by 3 or 2 evenly!”;


else 
     cout << “A false statement” << endl;
     cout << myNumber << “ is less than five” 


<< endl;
}

I made some changes, however when I try to compile it, I get an error saying: 'FahrenheitToCelsius' : function does not take 1 arguments. Anyone know how to fix this?

#include <iostream>
#include <iomanip>
#include <fstream>

float FahrenheitToCelsius();
using namespace std;

int main()
{
	float fahrenheit;
	float celsius;
	int choice;
	do{

	cout << "This program converts fahrenheit to celsius" << endl << endl ;
	cout << "Please Enter Fahrenheit Degrees: ";
	cin >> fahrenheit; 
	cout << "Your Conversion Is: "<< fahrenheit << " Degree Fahrenheit to:" << endl;
	cout << FahrenheitToCelsius (fahrenheit) << setprecision(2) <<" Degrees Celsius.";
	cout << "Want To Play Again?";
	cout << "1 == Yes";
	cout << "2 == No " << endl;
	cin >> choice;

	} while (choice == 1);

	cin.get();
	return 0;
}

float FahrenheitToCelsius(float fahrenheit)
{

	return celsius = (5.0/9.0) * (fahrenheit - 32);
}

make line 7

float FahrenheitToCelsius( float );

make line 7

float FahrenheitToCelsius( float );

Nope!
It is line 5.

Your signature of function says it takes no argument, and then you use it on line 19 with one argument passed to it and hence the error

I have just started in C + + and I had this program to do: Write a program that inputs two (2) numbers and determines which of the two numbers is the smallest. If the numbers are equal, display a message that they are equal.

error C2447: '{' : missing function header (old-style formal list?)

Here is the program:

#include <iostream>

{

//add function definitions for input and output

int myNumber = 7;

if (myNumber > 4); 
     cout << “A true statement” << endl;
     cout << myNumber << “ is greater than five” << endl;

     if (myNumber == 7); 
          cout << myNumber << “ is a great number.”;
          cout << “It can be divided by 3 or 2 evenly!”;


else 
     cout << “A false statement” << endl;
     cout << myNumber << “ is less than five” 


<< endl;
} 

end quote.

Don't hijack threads. kindly open yours

I have finally got it to work, but we all know we can't have d45s6 degrees Fahrenheit, so does anyone know how i can validate the program so that it only accepts digits?

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;
float FahrenheitToCelsius(float);
int main()
{
	float fahrenheit;
	float celsius;
	int choice;
	do{

	cout << "This program converts fahrenheit to celsius" << endl << endl ;
	cout << "Please Enter Fahrenheit Degrees: ";
	cin >> fahrenheit;
	while (!(isdigit(fahrenheit))){
		
		cin.clear();
		fflush(stdin);
		cout << "Please Enter A Digit: "<< endl;
		cin >> fahrenheit;
	
	}




	cout << "Your Conversion Is: "<< fahrenheit << " Degree Fahrenheit to:" << endl;
	cout << FahrenheitToCelsius (fahrenheit) << setprecision(2) <<" Degrees Celsius.";
	cout << "Want To Play Again?";
	cout << "1 == Yes";
	cout << "2 == No " << endl;
	cin >> choice;

	} while (choice == 1);

	cin.get();
	return 0;
}

float FahrenheitToCelsius(float fahrenheit)
{

	return fahrenheit = (5.0/9.0) * (fahrenheit - 32);
}

whenever I try to enter letters or digits, with the code above it rejects them both, i only want it to reject letters.

isdigit checks a character if it is a digit character.
The user entered string is parsed into a float (float fahrenheit; on line 10) and is automatically (if possible) converted to a digit.

This means you will not be able to check if the entered string was letters or numbers.

You would have to read the cin to a character array instead, and then check each character for validity.

You could do something like this:

char input[10]; // max length is 9 (tenth place is needed for terminating null)
cin >> input;
for(unsigned int i = 0; i < 10; i++) // 10 again, as we wrote it in input size
{
    if(input[i] == '\0') // we have reached the end of the input
        break; 
    if(!isdigit(input[i]))
    {
        cout << input << " contains invalid characters." << endl;
        return -1; // exit program
    }
}

I have done as instructed, and the person is able to enter a value, but then it says that Fahrenheit is uninitialized, I don't know how to take the value that a person entered and put it in the cout as well. Take a look.

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;
float FahrenheitToCelsius(float);
int main()
{
	float fahrenheit;
	char input[10];
	float celsius;
	int choice;
	do{

	cout << "This program converts fahrenheit to celsius" << endl << endl ;
	cout << "Please Enter Fahrenheit Degrees: ";
	cin >> input;
	for(unsigned int i = 0; i<10; i++){
		if (input[i] == '\0')
			break;
		if(!isdigit(input[i])){
			cout <<"Please Enter Digits ";
		}

	}
	cout << "Your Conversion Is: "<< fahrenheit << " Degree Fahrenheit to:" << endl;
	cout << FahrenheitToCelsius (fahrenheit) << fixed <<setprecision(2) <<" Degrees Celsius.";
	cout << "Want To Play Again?";
	cout << "1 == Yes";
	cout << "2 == No " << endl;
	cin >> choice;

	} while (choice == 1);

	cin.get();
	return 0;
}

float FahrenheitToCelsius(float fahrenheit)
{

	return fahrenheit = (5.0/9.0) * (fahrenheit - 32);
}

I have done as instructed, and the person is able to enter a value, but then it says that Fahrenheit is uninitialized, I don't know how to take the value that a person entered and put it in the cout as well. Take a look.

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;
float FahrenheitToCelsius(float);
int main()
{
	float fahrenheit;
	char input[10];
	float celsius;
	int choice;
	do{

	cout << "This program converts fahrenheit to celsius" << endl << endl ;
	cout << "Please Enter Fahrenheit Degrees: ";
	cin >> input;
	for(unsigned int i = 0; i<10; i++){
		if (input[i] == '\0')
			break;
		if(!isdigit(input[i])){
			cout <<"Please Enter Digits ";
		}

	}
	cout << "Your Conversion Is: "<< fahrenheit << " Degree Fahrenheit to:" << endl;
	cout << FahrenheitToCelsius (fahrenheit) << fixed <<setprecision(2) <<" Degrees Celsius.";
	cout << "Want To Play Again?";
	cout << "1 == Yes";
	cout << "2 == No " << endl;
	cin >> choice;

	} while (choice == 1);

	cin.get();
	return 0;
}

float FahrenheitToCelsius(float fahrenheit)
{

	return fahrenheit = (5.0/9.0) * (fahrenheit - 32);
}

Sorry my bad.
This means that float fahrenheit has not been assigned a value.
You will have to convert the input string to a floating numeric value, after validating it.

You will have to #include <stringstream> also.
After line 26, you would have to do the conversion like this:

stringstream convert;
convert << input;
convert >> fahrenheit;

Sorry my bad.
This means that float fahrenheit has not been assigned a value.
You will have to convert the input string to a floating numeric value, after validating it.

You will have to #include <stringstream> also.
After line 26, you would have to do the conversion like this:

stringstream convert;
convert << input;
convert >> fahrenheit;

isn't there a way to do a simple loop?

This is the most correct way to do it.
Alternate methods are C ways, and is shouldn't be used in C++ :)

hi.im spirit of love.....im new member 1st year in it can you help me what is the use of subprogram?

what function should i use?in functio as subprogram???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.