Super newbie questions

This is a discussion on Super newbie questions within the C++ Programming forums, part of the General Programming Boards category; 1- Code: #include <iostream.h> #include <string> string S = "lalala"; string B = S.substr(1,2); cout << B; This syntax error... ...

  1. #1
    Registered User Need_O2's Avatar
    Join Date
    Jan 2008
    Posts
    13

    Question Super newbie questions

    1-
    Code:
    #include <iostream.h>
    #include <string>
    
    string S = "lalala";
    string B = S.substr(1,2);
    cout << B;
    This syntax error... I want to make it display "la" for example
    Can you show a good substring usage to me

    Btw it said string isnt a type and substr isnt declared :/

    2- I want a IntegerToString function please (think I can make one but want to know if there is a native or included-in-header one)
    and StringToInteger (same content with other paranthesis)


    Thanks for replies

    (I use Dev-C++ btw)

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    string is part of the std namespace, so it should be be std::string.
    Ot alternatively, you add "using namespace std;" and it will compile.
    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. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string s = "number: ";
    	s += int(88);
    	cout << s << endl;
    	return 0;
    }
    // output:
    // number: X

    At least C++ makes me laugh Out LOUD ! ! !

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you want "using namespace std" or "std::string"?

    stringstream (include <sstream>) will do string to integer and integer to string. Strinstream corresponds (roughly) to cin and cout as the sscanf and sprintf does to scanf and printf. For example:
    Code:
    ...
    
      stringstream ss; 
      ss << 10; 
      string s;
      ss >> s;
      cout << 10 << " as a string is \"" << s << "\"" << endl;
    ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by matsp View Post
    Perhaps you want "using namespace std" or "std::string"?

    stringstream (include <sstream>) will do string to integer and integer to string. Strinstream corresponds (roughly) to cin and cout as the sscanf and sprintf does to scanf and printf. For example:
    Code:
    ...
    
      stringstream ss; 
      ss << 10; 
      string s;
      ss >> s;
      cout << 10 << " as a string is \"" << s << "\"" << endl;
    ...
    --
    Mats
    it failed to compile. what header to use?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    it failed to compile. what header to use?
    I said you need <sstream>, and of course needs <string> as well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User Need_O2's Avatar
    Join Date
    Jan 2008
    Posts
    13
    Code:
    #include <iostream.h>
    #include <string>
    using namespace std
    
    int main()
    {
    string S = "lalala";
    string B = S.substr(1,2);
    cout << B;
    return 0;
    }


    Ok like this ? Sorry Im connecting from internet cafe (dont have internet in home) so I dont think I can test now...

  8. #8
    Banned
    Join Date
    Nov 2007
    Posts
    678
    lol! sorry i missed it!

    i was trying strstream, stringstream etc. etc.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is missing a semicolon (after using...) and there is no such thing as <iostream.h> (that's <iostream>).

    Also keep in mind that the first index in strings (among most other things in C++) is 0, in case you want to extract "la", rather than "al".
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User Need_O2's Avatar
    Join Date
    Jan 2008
    Posts
    13
    first index is 0 okay thanks for the tip (so its substr(0,2))
    well I have a iostream.h that includes iostream in it
    but okay I'll use iostream

    Anyway will it work or not
    As I said I cant test it

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,741
    The STL usually works to perfection, provided you can use it. Nevertheless I tested:
    Code:
    #include <iostream>
    #include <ostream>
    #include <string>
    int main ( )
    {
        std::cout << '\"' << std::string( "lalala" ).substr( 0, 2 ) << '\"' << std::endl;
    }
    I came out with:
    "la"
    Press any key to continue . . .


    Next time, just wait until you get home.

  12. #12
    Registered User Need_O2's Avatar
    Join Date
    Jan 2008
    Posts
    13
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    string S = "lalala";
    string B = S.substr(0,2);
    cout << B << endl;
    system("Pause");
    return 0;
    }
    working thanks so much

    ----------------------------------------------

    I have another question

    File Editor example of Dev-C++ reads the content of file as LPSTR
    string s = <Some LPSTR var> is ok or do I need something different for converting between LPSTR and string ?
    Last edited by Need_O2; 04-16-2008 at 06:46 AM.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,741
    LPSTR, if I remember correctly, is just an alias for char *. If you're working with legacy code and you need C strings out of a string object, call its' c_str() method.

  14. #14
    Registered User Need_O2's Avatar
    Join Date
    Jan 2008
    Posts
    13
    ... so ???

    string s = c_str(<LPSTR variable>) ?

    how to convert it back to LPSTR then ?

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,741
    Code:
    void legacy_foo( char * param );
    
    // ...
    
    legacy_foo ( my_string.c_str() );
    But there is little sense in working with C strings unless you absolutely must.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory handling functions (newbie questions)
    By PaulBlay in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 06:37 AM
  2. newbie questions
    By raptorx in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 09:30 PM
  3. Newbie Questions
    By snoopy1 in forum C++ Programming
    Replies: 3
    Last Post: 11-19-2006, 01:00 PM
  4. Real newbie with VC6 questions
    By MagiZedd in forum Windows Programming
    Replies: 8
    Last Post: 10-15-2001, 08:27 PM
  5. newbie questions again
    By dune911 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2001, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21