String and char* problems

This is a discussion on String and char* problems within the C++ Programming forums, part of the General Programming Boards category; Ok, first off I'de like to say hello, I am a newcomer to this list and I am in the ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Question String and char* problems

    Ok, first off I'de like to say hello, I am a newcomer to this list and I am in the process of learning C++, I know that people posting blatant questions without researching thier problems are not tolerated however I think you'll find I'm not one of those people.
    I've been stuck on this particular problem for two weeks now, and I've just found this board so I hope someone here can help me understand whats going on.

    Here is my setup:

    I'm using two books titled:

    1.) "Learning to Program in C++" By Steve Heller
    2.) "The C++ Programming Language" Third Edition By Bjarne
    Stroustrup

    I am using two development environments and two compilers.

    1.) Microsoft Visual C++ 6.0
    2.) Bloodshed C++ 4.0

    My platform is Windows 2000 professional.

    Now onto what I know thus far.

    I've just reached the end of chatper 3 in the first book, and am trying to create a simple program thats takes the user's typed input and displays it.

    For peole who don't know the strucutre of the book the iformation presented thus far are: int, char, short, int main(),
    cout, cin.
    However I fear that the author is skipping important parts of these basics.

    For example here is a program I have written based on the explanations in my book:


    #include <iostream.h>
    #include <string.h>


    int main()
    {
    string s1;

    cout << "Enter a phrase:";
    cin >> s1;
    cout << "You entered:" << s1;


    return 0;
    }

    Both MSVC++ and Bloodshed compiler tell me that:

    7 stringattmp11.cpp `string' undeclared (first use this function)

    Which is telling me that line 7 has a problem, apparently I have not DECLARED the string... Please tell me how I do this.

    My book doesn't cover it and I don't want to go chapter hopping as I might skip even more crucial information.

    The Stroustrup book is an excellent reference however there are multiple instances of string in the indiex and I don't know which one covers implementing string for a program like this.

    I usually don't go asking for help unless I really get stuck and I can't see the light at the end of this tunnel.

    Can somebody please help me?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    string.h contains the prototypes for the C-style string functions, you'll need to use the STL string header -

    #include <iostream>
    #include <string>

    using namespce std;

    int main()
    {
    string s1;

    cout << "Enter a phrase:";
    cin >> s1;
    cout << "You entered:" << s1;


    return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Interesting.

    this is my original code:

    #include <iostream.h>
    #include <string.h>


    int main()
    {
    string s1;

    cout << "Enter a phrase:";
    cin >> s1;
    cout << "You entered:" << s1;


    return 0;
    }


    I edit it to make it look like this:

    #include <iostream.h>
    #include <string.h>


    int main()
    {

    using namespace std;

    string s1;

    cout << "Enter a phrase:";
    cin >> s1;
    cout << "You entered:" << s1;


    return 0;
    }

    the undeclared problem still happened.
    then I saw that you took out the .h in the headers so I tried that and thus got:

    #include <iostream>
    #include <string>


    int main()
    {

    using namespace std;

    string s1;

    cout << "Enter a phrase:";
    cin >> s1;
    cout << "You entered:" << s1;


    return 0;
    }

    which compiled and ran perfectly, so then I commented out the

    using namespace std;
    to get
    //using namespace std;

    and now I have this working code:

    #include <iostream>
    #include <string>


    int main()
    {

    //using namespace std;

    string s1;

    cout << "Enter a phrase:";
    cin >> s1;
    cout << "You entered:" << s1;


    return 0;
    }

    Thank you very much, however when I type spaces at the prompt on the prggram only the first word before the spacebar is hit is displayed. Also why does the .h matter in the include lines?

    Thank you very much, you've made my day!

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You need to bring the std namespace into global or function scope. Your compiler shouldn't compile the code with using namespace std commented out.

    When you use the extraction operator (>>) with cin, it'll only read characters into the string up to the first whitespace. If you want to read whitespace separated words you need to use cin.get() or cin.getline().

    The h files include the prototypes for the older C and C++ libraries. The files without the h extension include the declarations for the newer C++ libraries (and should wrap the older C libraries in the std namespace). To use the C++ string template class you need to use the string header, it is a completely different file from string.h.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Thank you very much, that explained a lot of critical information that my first book didn't even mention. My book comes bundled with DJGPP which doesn't seem to work on w2k so I went out and obtained the two mentioned compilers thinking that a book using examples based on a dos C++ compiler would translate seamlessly to newer C++ compilers.

    Is DJGPP so far behind the times that it requires .h to be used and does not have the string template class biult in? via the string file?

    Alsosince you knew the answer right off the bat, could you direct me to some further reading so that I may get a complete and relevant reference regarding these details?

    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. problems with string manipulation
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 05:45 PM

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