Thread: Convert string to float?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    Convert string to float?

    I've got two floats but they're as strings. how do i get this code to work. I don't know how to convert them so i can compare them. it's read from standard input that's why they're char*. The code below is just an example, i know it's not goin2 work lol. Please tell me how to convert so i can compare, thanx

    Code:
    char* f1 = 3.2;
    char* f2 = 4.53;
    
    if(f1 > f2) {
    
    cout << "F1 is bigger" <<endl;
    
    }else {
    
    cout <<F2 is bigger" <<endl;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    strtod()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Since this is C++, not C, you should use a std::stringstream
    Code:
    std::stringstream ss;
    std::string str = "3.14";
    float f = 0.0f;
    ss << str;
    ss >> f;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Moved to C++ programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    You can try boost::lexical_cast which is powerful & convenient.

    lexical_cast

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Hi,

    You can use the method atof() to implement it!

    #include <stdlib.h>

    ……
    char* f1 = 3.2;
    char* f2 = 4.53;

    int a, b;
    a = atof(f1);
    b = atof(f2);

    Carle
    _______________
    Comm100 - Open Source & Free Hosted Customer Service Software

  7. #7
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by carle View Post
    Hi,

    You can use the method atof() to implement it!

    #include <stdlib.h>

    ……
    char* f1 = 3.2;
    char* f2 = 4.53;

    int a, b;
    a = atof(f1);
    b = atof(f2);

    Carle
    _______________
    Comm100 - Open Source & Free Hosted Customer Service Software
    No. atof() is also C. The stringstream idea is C++.
    goto( comeFrom() );

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Would do you gain from trying to assign a floating number to a char pointer?
    Answer: nothing. Do not even attempt it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM