Hello It's me again, some might have seen a similar string, and it is probably because it is the same, but with a different problem. The following shows a program which uses a function that converts Fahrenheit into Celsius. The problem is that a person can enter any value they wish to, including letters, if this happens it will send the program into an infinite loop, so I made the code to read the input as an array, but now it won't go into the function, and the program will simply stop when i compile, anyone know how I can turn that input (that read as an array), into a string or something that can be executed into the function? Also, please include any changes I will have to make in order to make the function work.

#include <iostream>
#include <iomanip>

using namespace std;

float FahrenheitToCelsius(float);
int main()
{
    float fahrenheit;
    int choice;
	char input[10];
	bool valid;
    do{
    cout << "This program converts fahrenheit to celsius" << endl << endl ;


	do{
		valid = true;
    cout << "Please Enter Fahrenheit Degrees: ";
    cin >> input;
	for (int i = 0; i<strlen(input);i++){
		if (!isdigit(input[i])){
		valid = false;
		break;
		}
	}
	}while (valid == false);




    cout << "Your Conversion Is: "<< input << " 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);
}

Use atof(). Just add

fahrenheit = atof(input);

after the for loop.

if you want to take data in as a string and convert it you can do:

// you have to have these
#include <strstream>
#include <string>
int number;
std::string input;
std::stringstream ss;
cout << "enter number: ";
getline(cin, input);
ss << input;
ss >> number;  // now number is filled with the number from input;

Here is a handy little function I wrote that works like this

template <typename T>
void StringToNumber(std::string number, T & output)
{
    std::stringstream ss;
    ss << number;
    ss >> output;
}
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.