I've searched for days on how to find the correct way to convert a string to a float and then double the user entry. Any help would be appreciated. Thanks.

#include <iostream>
#include <cstdlib>

using namespace std;

bool FloatInput(char []);


void main()
{   
    float floatValue;
    char buffer[100];
    bool validInput;

    do {
        cout << "\nEnter a number: ";
        validInput = FloatInput(buffer);
        if (!validInput) cout << "Number is not valid!!! Try again...\n";
    } while (!validInput);

    // must figure how to multiply float input by 2


    floatValue = atof(buffer); // this returns the same value!!!

    cout << "\nTwice your number is: " << floatValue << endl;

}
bool FloatInput(char buf[])
{
    bool validInput = true;
    int i,length,decimal = 0;

    cin.getline(buf,100);
    length = strlen(buf);

    i = 0;
    while (validInput && (i < length))
    {
        if (i == 0)
        {
            if (((buf[i] < '0') || (buf[i] > '9')) && (buf[i] != '-') && (buf[i] != '.'))
                validInput = false;
        }
        else
        {
            if (((buf[i] < '0') || (buf[i] > '9')) && (buf[i] != '.'))
                validInput = false;
        }
        if (buf[i] == '.') decimal++;
        if (decimal > 1) validInput = false;
        i++;
    }
    return validInput;
}

Recommended Answers

All 6 Replies

Sorry for the newbie mistake in my first post. But, like I said earlier I can't seem to find a solution to after converting a string to a float and then multiplying that value by 2.

#include <iostream>
#include <cstdlib>

using namespace std;

bool FloatInput(char []);


void main()
{	
	float floatValue;
	char buffer[100];
	bool validInput;
	
	do {
		cout << "\nEnter a number: ";
		validInput = FloatInput(buffer);
		if (!validInput) cout << "Number is not valid!!! Try again...\n";
	} while (!validInput);

	// must figure how to multiply float input by 2

	
	floatValue = atof(buffer);

	cout << "\nTwice your number is: " << floatValue << endl;

}
bool FloatInput(char buf[])
{
	bool validInput = true;
	int i,length,decimal = 0;

	cin.getline(buf,100);
	length = strlen(buf);

	i = 0;
	while (validInput && (i < length))
	{
		if (i == 0)
		{
			if (((buf[i] < '0') || (buf[i] > '9')) && (buf[i] != '-') && (buf[i] != '.'))
				validInput = false;
		}
		else
		{
			if (((buf[i] < '0') || (buf[i] > '9')) && (buf[i] != '.'))
				validInput = false;
		}
		if (buf[i] == '.') decimal++;
		if (decimal > 1) validInput = false;
		i++;
	}
	return validInput;
}

You converted your ASCII numberical value to a Floating-Point value with...

floatValue = atof(buffer); // this returns the same value!!!

So just multiply the result by two!

floatValue *= 2.0f;

Or did I miss what you were really asking for?

I had tried that, but it kept giving me a run time error saying that floatValue hadn't been initialized yet. But I thought it was initialized a couple of lines up under "float floatValue".

You probably want to use the 'atof' function

My own personal snippet :P

#include <iostream>
#include <string>

using namespace std;

int main()
{
	const char* a = "10.7";
	int b = atoi(a); //convert to int
	float c = atof(a); // convert to float

	cout << a << endl;
	cout << b << endl;
	cout << c << endl;

	return 0;
}

Hey wildgoose, after working in your fix, I figuring out that I was initializing it incorrectly. You solved it for me, thanks!!!

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.