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;
}