String to float

I am new to C++. I am trying to write a function that receives a string input as an argument, prints the string as a message and gets and returns a float from the user

I know I am off. line 32 is especially bad, really guessing with stof. Also, I am not sure what to put on line 35 after return.

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
  9 #include <iostream>
 10 #include <string>
 11 #include <sstream>
 12 
 13 using namespace std;
 14 
 15 float get_float(string essage);
 16 float check_valid_input(float check, float low, float high);
 17 
 18 int main ()
 19 {
 20    float check, low, high;
 21    string message;
 22 
 23    get_float(message);
 24    check_valid_input(check, low, high);
 25 
 26    return 0;
 27 }
 28 
 29 float get_float(string message)
 30 {
 31 
 32    float message = std::stof (check);//help!
 33    message = "Information question here ";
 34 
 35    return message;
 36 }
I think this is what you want
1
2
3
4
5
6
7
8
9
10
11
12
13
14
float get_float(string message);
int main()
{
    std::string str_value;
    std::cout << "Enter a float:";
    std::cin >> str_value;

    float fl_value = get_float(str_value);
}

float get_float(string message)
{
    float std::stof(message);
}
Do you mean:

1
2
3
4
float get_float(string message)
{
    return std::stof(message);
}
when i use: return std::stof(message); = I get the following error: error: ‘stof’ is not a member of ‘std’

for float std::stof(message); = invalid use of qualified-name ‘std::stof’

should i be calling a certain library? i.e. #include <blah>

g++ -v = gcc version 4.4.7
Last edited on
Assuming you're using the proper -std flag, this is not your fault. It's a known bug with mingw g++. Actually, I still see the same bug with cygwin64 g++ 4.8.1

Perhaps it's time you invested in a proper OS :-) I run Linux in a VM and the oldest compiler I have at hand right now is g++ 4.7.3, and it doesn't have this problem.
In the meantime, before you install a new operating system, there are several alternatives for this line of code. Here's one:
 
    return std::atof(message.c_str());
I was just thinking the same, however no luck. I still see an error:

area.cpp: In function ‘float get_float(std::string)’:
area.cpp:37: error: ‘atof’ is not a member of ‘std’
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
 13 using namespace std;
 14 
 15 float get_float(string essage);
 16 float check_valid_input(float check, float low, float high);
 17 
 18 int main ()
 19 {
 20    float check, low, high;
 21    string message;
 22 
 23    //get_float(message);
 24 
 25    cout << "Enter a float: ";
 26    cin >> check;
 27 
 28    get_float(message);
 29 
 30    check_valid_input(check, low, high);
 31 
 32    return 0;
 33 }
 34 
 35 float get_float(string message)
 36 {
 37    float check = std::atof(message.c_str());
 38    return check;
 39 }


so i took out std:: and i got this:

area.cpp: In function ‘float get_float(std::string)’:
area.cpp:37: error: ‘atof’ was not declared in this scope
Last edited on
Do you have
#include <cstdlib>
I do now! thank you Chervil!
@Chevril
You also have to buy a new computer that can handle the new OS :-)
Indeed, you're right. Though actually I'm running a dual-boot setup on a relatively modest laptop, with both windows and linux.
Last edited on
Topic archived. No new replies allowed.