Thread: Convert Integer to String and String to Integer

  1. #16
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question

    I am willing to bet you are getting an error at the line below that, when you try to use printf to print a string object to the screen
    Oh, you say right, .... and I'm say right too. Because, It's very funny and I don't know why. I will explain, now:
    first code:
    Code:
    #include<iostream>
    #include<string>
    #include<stdio.h>
    #include<sstream>
    using namespace std;
    int main(){
    	int number=567;
    	string myphrase;
    	stringstream ss(stringstream::in | stringstream::out);
     	ss<<number;
    	myphrase = "your number is " + ss.str();
     }
    this code is NO ERROR.
    and this is my second code:
    Code:
    #include<iostream>
    #include<string>
    #include<stdio.h>
    #include<sstream>
    using namespace std;
    int main(){
    	int number=567;
    	string myphrase;
    	stringstream ss(stringstream::in | stringstream::out);
     	ss<<number;
    	myphrase = "your number is " + ss.str();//--> I use debugger and see that error from this line, mean it's not run line "printf" yet.
            /* ************************** */
            printf("%s\n",str);
    }
    So, I don't know how your compiler is, I'm using Code::Block, and I cannot answer why

    2) and answer to your question, why I prefer printf/scanf than cint/cout because printf/scanf run faster than cint/cout. It's pretty sure because:
    a) my teacher say that
    b) many document say that,too
    c) my experience: there a problem on SPOJ, I must read N^2 element (N=10^9), if I use printf/scanf I will pass my test, and if I use cint/cout, It's not

    that why, I always try to use old function from C

    so, who can answer for me above questions, please.
    thanks for ton

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by hqt View Post
    2) and answer to your question, why I prefer printf/scanf than cint/cout because printf/scanf run faster than cint/cout. It's pretty sure because:
    a) my teacher say that
    b) many document say that,too
    c) my experience: there a problem on SPOJ, I must read N^2 element (N=10^9), if I use printf/scanf I will pass my test, and if I use cint/cout, It's not

    that why, I always try to use old function from C

    so, who can answer for me above questions, please.
    thanks for ton
    Then your teacher is misguided because that is not true.
    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.

  3. #18
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by hqt View Post
    So, I don't know how your compiler is, I'm using Code::Block, and I cannot answer why
    Neither can I at the moment.

    Quote Originally Posted by hqt View Post
    2) and answer to your question, why I prefer printf/scanf than cint/cout because printf/scanf run faster than cint/cout. It's pretty sure because:
    a) my teacher say that
    b) many document say that,too
    c) my experience: there a problem on SPOJ, I must read N^2 element (N=10^9), if I use printf/scanf I will pass my test, and if I use cint/cout, It's not

    that why, I always try to use old function from C

    so, who can answer for me above questions, please.
    thanks for ton
    If you want to run C code, program in C not in C++. I have not heard of cin/cout being slower than printf and scanf, so I will save that discussion for someone else.

    If the code I posted does not compile for you, you need to post the specific error messages so we can figure out why.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #19
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    No no, the code you post is right, no problem but the code of mine, is wrong, and the secret is: I know that problem from that line, but when I still not run to that line, It meet problem

  5. #20
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you are getting errors because you are trying to use printf() to print a C++ string (std::string). you cannot do that. use cout. do not mix C and C++ console I/O, as they are not guaranteed to be compatible, and you may get unexpected (ugly) output if you mix them.

    cout is not slower than printf. this may have been true in the early days of C++, before there were highly optimized C++ compilers and libraries, and when C compilers and libraries were already very mature, but it is certainly false now.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hqt
    I use debugger and see that error from this line, mean it's not run line "printf" yet.
    You probably just misread the debugger's output. AndrewHunter is correct. The %s format specifier for printf applies to null terminated C-style strings, not std::string objects.

    Quote Originally Posted by hqt
    2) and answer to your question, why I prefer printf/scanf than cint/cout because printf/scanf run faster than cint/cout. It's pretty sure because:
    a) my teacher say that
    b) many document say that,too
    It is true that C++ I/O stream operations tend to be slower than their C counterparts because they may involve some extra work. On the other hand, this extra work can bring about safety and ease of usage (though the syntax may actually become more verbose, heh), and typically does not actually matter in the bigger picture.

    Quote Originally Posted by hqt
    c) my experience: there a problem on SPOJ, I must read N^2 element (N=10^9), if I use printf/scanf I will pass my test, and if I use cint/cout, It's not
    That is fine: if you find that you need to use the C-style I/O for special reasons, go ahead. But generally, default to C++-style I/O in C++.
    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

  7. #22
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Sorry, because my document not in English (of course, because I'm not in country that speak English , so I must find another source that give this:
    this is first:RootTalk: cint FAQ
    In most cases cint runs fast, but a small change makes it very slow. Why?
    Cint uses incremental bytecode compilation technique. The bytecode compiler
    has size and syntax limitations. When you hit that limitation, cint runs
    10-20 times slower.
    this is second:
    cin or scanf???
    you should see mariam.kupa post, he have same idea with me
    and many of my friend say that. of course, It just a little, but if you read 10^9^2 element, different will about 1-2 seconds (to cost !!!, because a program must run in that time )

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hqt
    this is first:RootTalk: cint FAQ
    That FAQ is irrelevant as it is about a C++ interpreter, not about C++ I/O streams.
    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

  9. #24
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Oh, std::string cannot use with printf/scanf
    Simple, but maybe enough for me

    and, I may have last question in this topic: I don't know purpose of stringstream, so who can explain it shortly for me, please. (In my mind, I don't think it just only to convert from integer to string )

    @laserlight: uhm, I think your have same time zone with me, I meet you at this early morning, and now, I meet you again
    Last edited by hqt; 09-15-2011 at 11:19 AM.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hqt
    I don't know purpose of stringstream, so who can explain it shortly for me, please. (In my mind, I don't think it just only to convert from integer to string
    Its purpose is to deal with strings as if they were I/O streams, hence the kind of operations available for I/O streams would be available, in a way, with strings. You may be familiar with sprintf, which has a similiar idea.

    Quote Originally Posted by hqt
    @laserlight: uhm, I think your have same time zone with me, I meet you at this early morning, and now, I meet you again
    GMT+8
    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

  11. #26
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Oh, convert from pascal to C have some strange points, and from C to C++, I cannot imagine :-O
    Now, I'm thinking about Perl, the most freedom language I have learnt :">

  12. #27
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    laserlight lives in the future ! Hi from the past !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to convert from string to integer
    By vopo in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 06:32 AM
  2. convert string into integer using recursion
    By peter_hii in forum C++ Programming
    Replies: 18
    Last Post: 08-23-2006, 10:09 AM
  3. convert from integer to string
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2006, 09:28 AM
  4. how do i convert a string to an integer??
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 09:21 PM
  5. FAQ how do i convert a string to an integer?? (C)
    By Unregistered in forum FAQ Board
    Replies: 1
    Last Post: 12-02-2001, 05:03 PM