Thread: Converting double to a string.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    24

    Converting double to a string.

    I want to put a double to a string and output that string, but i can't figure out how to do it.

    Code:
    	string Add(double A, double B)
    	{
    		double C,D,X;
    		string Hide;
    		Hide = "";
    		if( B > 0)
    		{
    			C = A;
    			D = B;
    			X = C/D;
                     //    Hide += X;
    			Hide << X;
    		}
    		else
    		{
    			Hide = "Illegal Division By Zero";
    		}
    		return Hide;
    	}
    basically if i put like 3 for A and 4 for B, the double X will = 0.75 and i want it to output a string that says 0.75. I can't figure out how to put that double to the string though. I tried << because my teacher mentioned something about << overloading a function but what i did isn't correct. I tried googling around dtoa because they have an itoa but i didn't really find anything for it. Help would be very appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Look up string streams.
    stringstream - C++ Reference
    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
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Operator << is overloaded to accept doubles on the right hand side, but you need a stream on the left hand side.

    This FAQ has an example:
    [39] Miscellaneous technical issues Updated! , C++ FAQ

    Something like:

    Code:
    #include <sstream>
    
    string Add(double A, double B)
    	{
    		double C,D,X;
    		string Hide;
    		Hide = "";
    		if( B > 0)
    		{
    			C = A;
    			D = B;
    			X = C/D;
                             //create stream object
    			std::ostringstream o;
                             // << here behaves like << with cout on lhs
    			o << X;
    			Hide = o.str();
    		}
    		else
    		{
    			Hide = "Illegal Division By Zero";
    		}
    		return Hide;
    	}

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    my library is

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <cstring>
    Do i need the std:: part on ostringstream if i have included both string and cstring, just to be safe?

    __________________________________________________ ____________________________________
    I #include <sstream>, and i found out i don't need that std:: part. What is the purpose to the std:: part anyways?

    Also for some reason my code outputs nothing, it should return the string.

    header file

    Code:
    	string Add(double A, double B)
    	{
    		double C,D,X;
    		string Hide;
    		Hide = "";
    		if( B > 0)
    		{
    			
    			ostringstream o;
    			C = A;
    			D = B;
    			X = C/D;
    			o << X;
    			Hide = o.str();
    		}
    		else
    		{
    			Hide = "Illegal Division By Zero";
    		}
    	
    		return Hide;
    	}
    calling program:
    Code:
    
    #include "stdafx.h"
    #include "Overload.h"
    
    int main()
    {
    	COverload Me;
    
    	Me.Add(3,4);
    
    	cout << "\n\n";
    	system("pause");
    
    	return 0;
    }
    it doesn't return the 0.75 if i have 3,4

    nor does it return the Hide = "Illegal Division By Zero"; line if i put 0 for B.
    Last edited by dvsumosize; 03-27-2011 at 01:18 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    stringstream lies in the std namespace; therefore the "std::" part is always mandatory. It does not matter what headers you include, or not. This does not change the fact.
    What does change the fact is using statements, such as "using namespace std;" which tells the compiler to take all the contents of the std namespace and put it into the global namespace. And the global namespace requires no prefix.
    Also, using "using namespace std;" in a header file is bad.

    As for the reason you see nothing... that is because you don't output the return of your function.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    my bad, its late and i wasn't thinking about that haha, thanks.
    __________________________________________________ __________________________________________
    Code:
    		return cout << "\t\t The value is = " <<  Hide;
    i might have made just a ridiculous error but why is this cout giving me an error of:

    1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_strin g(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'std::basic_ostream<_Elem,_Traits>' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\data\object oriented progrmaming\data\overloading\overloading\overload. h 74 1 Overloading

    2 IntelliSense: no suitable user-defined conversion from "std::basic_ostream<char, std::char_traits<char>>" to "std::string" exists c:\data\object oriented progrmaming\data\overloading\overloading\overload. h 74 10 Overloading
    Last edited by dvsumosize; 03-27-2011 at 01:57 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Think of what you are doing. Your function is supposed to return a string with the result of the operation. What are you doing here?
    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.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    Well i am returning a cout which isn't really a string right?

    but when i returned Hide on its own, nothing had appeared.

    i thought hide would output because i'm calling the Add method in my calling program

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dvsumosize View Post
    Well i am returning a cout which isn't really a string right?
    A cout is a stream object. More specifically, a global stream object that sends data to the standard output.
    So no, not a string.

    but when i returned Hide on its own, nothing had appeared.
    You don't think that is because you do nothing with the returned result from the function in main?
    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.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    i just realized what you are saying

    Sorry, latenight brain fail.

    Code:
    COverload Me;
    
    	cout << "\n\t The Numbers Are " << Me.Add(3,4);
    __________________________________________________ ___________________

    Thank you for your explanations

    Oh and why is using namespace std; bad in a header file?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    alright, thank you. Also do you have any links that are a good guide on overloading?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Again!!
    By Jjoraisnt32 in forum C Programming
    Replies: 7
    Last Post: 02-03-2011, 02:18 PM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  4. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM