Strings

This is a discussion on Strings within the C++ Programming forums, part of the General Programming Boards category; I just noticed something and was wondering why if i write this. Code: int main() { cout << "Please enter ...

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Strings

    I just noticed something and was wondering why if i write this.
    Code:
    int main()
    {
    	cout << "Please enter your first name: ";
    	string name;
    	cin >> name;
    	const string greeting = "Hello, " + name + "!";
    	const int pad = 1;
    	const int rows = pad * 2 + 3;
    	return 0;
    }
    and I use the following #includes <iostream> and <cstring> I get a crap load of << erros ? but if i change this <cstring> to <string>
    It works.

    Does <cstring> not work with the << and ? I notice that with the use of <cstring> peeps are using cin.getline. Is that becuase you need to get the entire string including whitespace? and >> wont do that?
    big146

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    <cstring> is the c++ equivlent of <string.h> which deal with null terminated array of characters

    <string> controls the string class.

    Two very different and seperate files.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    What Thantos said ...

    Plus, you can use cin.getline with C-style null-terminated character arrays if you want to get input without stopping at the first whitespace. Similarly, you can use std::getline with the C++ string class to get input without stopping at the first whitespace. Using >> with either C-style strings or the C++ string class will cause the input to stop at the first whitespace. Note that the getline that works with C++ strings takes different arguments than the one that works with the character arrays. The C++ string version takes the input stream as the first argument (e.g. std::getline(std::cin, ...);), whereas the other version is a member of the input stream class (e.g. std::cin.getline(...);).

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and I use the following #includes <iostream> and <cstring> I get a crap load of << erros ?
    Look more carefully at your book's code. Precision is key when writing programs, and a single character can mess you up.
    My best code is written with the delete key.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Also, you would normally get somewhat obvious errors from using the string class without #include-ing the <string> header. However, in some implementations just #include-ing <iostream> brings in some of the std::string declarations. This causes even more confusion, because the string class seems to work until you try to do certain operations (like using operator>>) that require the <string> header. That is one of the reasons some people think you have to add a .c_str() to output a string to cout -- they just haven't included the proper <string> header.

  6. #6
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Quote Originally Posted by Prelude
    >
    Look more carefully at your book's code. Precision is key when writing programs, and a single character can mess you up.
    I did write the program with <string>,I just wanted to see what it did with <cstring>. I was thinking <cstring> was the updated version of <string>. I did not realize that they are two diff things.

    thks for all the replys

    P.S. I know there are alot of book reviews out there but i would like to hear from some of you on what would be a good book to learn ths STL.
    Last edited by big146; 06-17-2004 at 08:08 PM.
    big146

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i would like to hear from some of you on what would be a good book to learn ths STL
    The one you're reading is a good start. Past that, the recommended text is this.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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