954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with converting a string to a float then multiplying the answer

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
#include

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

slomad1956
Newbie Poster
4 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

You missed this
http://www.daniweb.com/forums/announcement8-3.html

Along with half a dozen other clues about code tags.

So now we have to wait until that gets fixed.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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;
}
slomad1956
Newbie Poster
4 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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?

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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".

slomad1956
Newbie Poster
4 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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;
}
gretty
Junior Poster
158 posts since Apr 2009
Reputation Points: 10
Solved Threads: 7
 

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

slomad1956
Newbie Poster
4 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You