string to double or float seems to be losing numbers

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <tchar.h>
#include <urlmon.h>
#include <dos.h>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <set>
#include <vector>
#include <iomanip>
using namespace std;
#pragma comment(lib, "urlmon.lib")

std::string jargon;

float bd = 0;
float val = 0;
float percentage = 0;

int main ()
{
	std::string part1;
	std::string part2;
	std::string part3;
	
	jargon = "apple 37.00 0.90";

	if (jargon.find(" ")!=string::npos)
	{
		jargon.erase(0, jargon.find(" "));
	}
	
	part1 = jargon;
	part1.erase(0,1);
	if (part1.find(" ")!=string::npos)
	{
		part1.erase(part1.find(" "));
	}

	float val = atoi(part1.c_str());
	
	part1 = jargon;
	part1.erase(0,1);

	if (part1.find(" ")!=string::npos)
	{
		part1.erase(0, part1.find(" "));
	}
	
	
	float bd = atoi(part1.c_str());
	cout << bd << endl;
	cout << val << endl;
	float percentage = ((bd/val) * 100);
	
	cout << (bd/val) << endl;
	cout << ((bd/val) * 100) << endl;
	
	return 0;
}


Yeah, I know I don't need that many includes, just easier for me to always cut and paste my list.

Now, in this program, I would expect to pull "37.00" and "0.90" out of the string, then acquire the percentage (via division and then multiplying by 100). However, if you run the program, instead I'm not getting decimals (for one) and two, it seems that it converts the "0.90" into "0". I've tried many different variations on the float vs double etc and this just happens to be the manifestation of the attempt that has led me to conclude that I don't know what's going on and or what I'm missing.
Whilst I haven't thoroughly examined your code, I note that you are turning the strings into integers before storing them in your floating point objects. Is this the source of your trouble?
I don't know, maybe, but as you'll see I'm converting them directly into float (or so that is my perception).

If

 
float bd = atoi(part1.c_str());


is not the proper way to convert a decimal string into a floating point number (decimals), then what is the proper way?
atoi(part1.c_str());

That produces an int. Then the float bd stores that value, turning the int into a float. Here, run this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <iostream>
#include <cstdlib>

int main()
{
  
  std::string eggs("1.234");
  

  std::cout << "Original val: " << eggs << std::endl;
  
  std::cout << "Output of atoi: " <<  atoi(eggs.c_str()) << std::endl;
  float bd = atoi(eggs.c_str());
  std::cout << "Value stored in float: " <<  bd;

  return 0;
}


and then run this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <iostream>
#include <cstdlib>

int main()
{
  
  std::string eggs("1.234");
  

  std::cout << "Original val: " << eggs << std::endl;
  
  std::cout << "Output of strtof: " <<  strtof(eggs.c_str(),0) << std::endl;
  float bd = strtof(eggs.c_str(),0);
  std::cout << "Value stored in float: " <<  bd;

  

  return 0;
}






Last edited on
error C3861: 'strtof': identifier not found

Also googled that and found "strtok" and it didn't work either but it was because it said it couldn't "convert paramter 1 from 'const char *' to 'char *'
strtof lives in cstdlib, but it's not provided by every compiler. It's C99 and C++11, and if your compiler isn't compliant with one of those, you might not have it.

Do you have strtod? That's a bit older and you should have that.

strtok doesn't convert strings into numbers. It converts strings into other strings.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <sstream>
#include <string>

template <typename VAL> VAL from_string(std::string S)
  {
    std::stringstream ss;
    VAL V;
    ss << S;
    ss >> V;
    return V;
  }

  std::string strValue = "107.835";
  float value = from_string<float>(strValue);
Moschops:
strtod worked!

Texan40:
That code is too advanced for me to make sense of TBH

Thanks guys
Bleh, what, is there s special command to use this in a file stream? I get the stinking result in the file of "-1.#ind"
that "-1.#ind" is a NaN (Not a Number) which means that the float value in question is either undefined or unrepresentable.
Topic archived. No new replies allowed.