how to use stof

I'm trying to convert string to float but it will not compile. I found the stof thing on the internet but clearly I'm not using it wright. Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<string>
using namespace std;
int main (){

string str1, str2;
float num1,num2;

cout << "\n\n\tEnter string 1";
cin >> str1;
cout << "\n\n\tEnter string 2";
cin >> str2;

num1 = stof(str1);
num2 = stof(str2);

cout << "\n\n\tNumer 1: " << num1 << "\tNumer2: " << num2;

return 0;
}
works fine for me. What compiler are you using?
code::blocks
compiler says "stof was not declared" thats why I thought I'm not using it right
You have to add -std=c++0x or -std=c++11 in your Compiler's command line.
You should be able to find it in your Project's settings.
in my projuct's settings I have add file, remove file, project tree, notes and set programs arguments...
I have no clue how to do it.
Is there any other way to convert string to number and vice versa?
in codeblocks click settings > compiler then check the box that says
Have codeblocks follow the c++11 iso c++ language standard [-std=c++11]


Is there any other way to convert string to number and vice versa?

yes you can use stringstreams

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
#include<iostream>
#include<string>
#include <sstream>
using namespace std;
int main(){

	string str1, str2;
	float num1, num2;

	cout << "\n\n\tEnter string 1";
	cin >> str1;
	cout << "\n\n\tEnter string 2";
	cin >> str2;
	std::cin.ignore();

	std::stringstream ss;

	ss << str1;
	ss << " ";
	ss << str2;

	ss >> num1;
	ss >> num2;

	cout << "\n\n\tNumer 1: " << num1 << "\tNumer2: " << num2;
	std::cin.ignore();
	return 0;
}
I found c++11 iso...etc but unfortunately it didn't change anything.
How ever the other thing works just fine.
How do I change number to string?
std::to_string
if I do this string str3 = std::to_string (num1 + num2);
I get "to_string is not a member of std
if I do this string str3 = to_string (num1 + num2);
I get "to_string is not declared in this scope"
am I doing it wrong, or is it my compiler again and I need to look for other ways?
How do I change number to string?

The same way you convert a string to a number.


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
class="quote">
class="qd">How do I change number to string?
The same way you convert a string to a number. #include<iostream> #include<string> #include <sstream> using namespace std; int main(){ string str1, str2; float num1, num2; cout << "\n\n\tEnter float 1"; cin >> num1; cout << "\n\n\tEnter float 2"; cin >> num2; std::cin.ignore(); std::stringstream ss; ss << num1; ss << " "; ss << num2; ss >> str1; ss >> str2; cout << "\n\n\tNumer 1: " << str1 << "\tNumer2: " << str2; std::cin.ignore(); return 0; }
Last edited on
am I doing it wrong, or is it my compiler again and I need to look for other ways?

I don't think codeblock's default compiler supports std::to_string()
Thanks Yanson, it works perfectly:)
Topic archived. No new replies allowed.