String to a float

I am challenging myself to write a simplified spreadsheet program. I am able to check for different cell inputs and assign values to a spreadsheet struct, but I can't figure out how to convert a string to a float.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
case ENTER:
{
        int value=0;
        bool y_n = false;
	gotoXY(8+(x*7),5+(y*4), " ");
	gotoXY(7,45,"Fill cell : ");
	getline( cin, Fill_Cell);
	int len = Fill_Cell.length()-1;
	for(int a=0;a<len;a++)
		if(Fill_Cell[a] == '.')
			y_n = true;
	if(Fill_Cell[0] == '=')
		gotoXY(7,45,"Filling cell with Formula..");
	else if (Fill_Cell[0]>='0' && Fill_Cell[0]<='9' && !y_n)
	{
		//Check for negative numbers later
                gotoXY(7,45,"Filling cell with an integer of ");
		istringstream convert(Fill_Cell);
 // stringstream used for the conversion constructed with the contents of Fill_Cell' 
	// ie: the stream will start containing the characters of 'Fill_Cell'
		convert >> value;
		spreadsheet[x][y].num = value;
		cout << spreadsheet[x][y].num;
	}
	else if (Fill_Cell[0]>='0' && Fill_Cell[0]<='9' && y_n)
	{
		gotoXY(7,45,"Filling cell with a float");
		//spreadsheet[x][y].number = ??;
	}
	else if ((Fill_Cell[0]>='A' && Fill_Cell[0]<='Z') || (Fill_Cell[0]>='a' && Fill_Cell[0]<='z'))
	{
		gotoXY(7,45,"Filling cell with a some text - ");
		spreadsheet[x][y].text = Fill_Cell;
		cout << spreadsheet[x][y].text;
	}
}


Not sure how I can use something similar to getting the integer, but get a float, instead.

PS:
After getting the above to work, I'll be putting this into a separate function
std::stof http://en.cppreference.com/w/cpp/string/basic_string/stof

Consider using double (and std::stod) instead of float.
@JLBorges

1
2
3
4
5
6
7
else if (Fill_Cell[0]>='0' && Fill_Cell[0]<='9' && y_n)
				{
					gotoXY(7,45,"Filling cell with a double = ");
					 value = stod( Fill_Cell , 0);
					spreadsheet[x][y].number = value;
					cout << spreadsheet[x][y].number;
				}

This works, thanks, up to a point. If I type any number with only one decimal place, as in, .1 to .9, I get the correct output, BUT, if I type two decimal places, it gets rounded . If I change the 0 in the stod() function to, say 2, I get an error. I'm not sure on how to really implement this. I tried using the std::size_t* N = 2, and then use N in place of the 0, but no. Using this part of the library is new to me, as I've never needed to implement anything even closely resembling it. So, could you nudge me a little more in the right way to use this?
Thanks for the help you've given so far.
> BUT, if I type two decimal places, it gets rounded .

The rounding is occurring in the stream output. Try:
1
2
value = stod( Fill_Cell ) ;
std::cout << std::fixed << std::seprecision(15) << value ;


The second argument to std::stod need not be specified; it has a default value of nullptr.

For an example where the second argument (the address of a number - std::size_t -
which receives the count of characters processed during the conversion) is passed, see:
http://www.cplusplus.com/forum/beginner/219637/#msg1011687
@JLBorges

Thank you. I'm now able to store the double in my struct, and print it out.
Topic archived. No new replies allowed.