Thread: Strings

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    36

    Question Strings

    Ok...
    I am a newbie here and i need to know how to do this

    Code:
     
    #include <iostream.h>
    
    using namespace std;
    
    int main()
    
    {
        
       ????
        cout<<"Hi there! I am PC, the computer. What's your name?:";
        cin>>name;
        cout<<"Hello " <<name<< endl;
        
        std::cin.get();
        return 0;
    }
    Basically i'm trying to make the computer print the name that i input in there. Thing is i need to define "name", which is a string of letters

    how do i do that?

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    you have a few options, but first...
    >> #include <iostream.h>
    use: #include <iostream> instead

    char name[SOME_MAXIMUM_SIZE];

    or

    #include <string>
    string name;

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    #include <iostream> // Notice that I excluded .h from the header file - see below
    
    using namespace std;
    
    int main()
    
    {
        
        char name[20];
        cout<<"Hi there! I am PC, the computer. What's your name?:";
        cin>>name;
        cout<<"Hello " <<name<< endl;
        
        cin.get(); // You don't need std:: here
        return 0;
    }
    Although this will compile with a friendly compiler, it is correct to exclude the ".h" from your header inclusion. I'm assuming you're using up-to-date C++ since you included "using namespace std". This is also why you don't need std:: before your call to cin.get().

    To declare, "name", you use an array of chars - this is called a C-style string. There are C++ strings, which are classes with functions, etc... but I used C-style strings here for a number of reasons, just so you know.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>but I used C-style strings here for a number of reasons, just so you know.
    Just out of curiosity, what are these reasons that you speak of? I mean, even Prelude thinks that beginners should start with C++ strings and then learn C-style later
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    1) I'm more used to C-style strings

    2) Useable in C and C++

    3) Personally I thought C-style strings would be easier for newbies to grasp since they wouldn't have done much OOP yet, but I'm not one to argue with someone with as much knowledge of the language as Prelude.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    #include <iostream>
    #include <string>
    
    int main ( void )
    {
    
        std::string name;
    
        std::cout << "What is your name: ";
        std::cin  >> name;
    
        // new line
        std::cout << std::endl;
    
        std::cout << name;
    
        return 0;
    
    }
    What is C++?

  7. #7
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    Quote Originally Posted by Sridar
    Ok...
    I am a newbie here and i need to know how to do this

    Code:
     
    #include <iostream.h>
    
    using namespace std;
    
    int main()
    
    {
        
       ????
        cout<<"Hi there! I am PC, the computer. What's your name?:";
        cin>>name;
        cout<<"Hello " <<name<< endl;
        
        std::cin.get();
        return 0;
    }
    Basically i'm trying to make the computer print the name that i input in there. Thing is i need to define "name", which is a string of letters

    how do i do that?












    That should work, except for the ??????? part of the source code. Delete that, compile, and run. It should work fine.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Delete that, compile, and run. It should work fine.
    In case you haven't noticed, he hasn't declared name. And he's using namespace std; with the <iostream.h> header. And there is no std::cin with the .h header. For a workine example, see vicious or sean's posts.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    
    	string name;
    
    	cout << "Enter name: ";
    	getline ( cin, name );
    
    	cout << endl << name;      
    
    	return 0;
    
    }
    Heres another example that will also read in spaces.

    [edit]
    If you use MSVC++ there might be a bug with getline.

    fix it by editing line 165 of the file: c:\Program Files\Microsoft Visual Studio\VC98\Include\STRING

    Line 165 should be:

    _I.rdbuf()->sbumpc();

    instead of:

    _I.rdbuf()->snextc();

    Thanks for hunter for the reminder!
    Last edited by Vicious; 09-22-2004 at 11:58 PM.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    i know your questions been answered but ill try to answer it again. To start with, you need a new tutorail. The one your using is out of date. i would suggest http://cplus.about.com you need to put in the library <string>. so you would put that just under #include <iostream>, remember that iostream.h and other .h header files are out of date. now you would declare a string just under
    Code:
    int main()
    {
    it should look like this
    Code:
    #include <iostream>
    #include <string> // required for strings
    using namespace std; // you should always have this
    int main()
    {
    string name;
    cout<<"Please enter your first name.";
    cin>>name;
    cout<<"Hello "<<name;
    
    cin.ignore();
    cin.get(); // no need for std before it, as you can see i also use cin.ignore, that is sometimes used 
    // when cin.get doesn't work by itself, make sure you use it before cin.get, not after
    
    return 0;
    }
    hope you get it

  11. #11
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    personally, i think c++ strings is much easier to handle than C strings.

    in the beginning was CppStrings
    ---------------------------Christian Bible,John 1:1
    by Hermitsky 2004

    blow me ... ...

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by sean_mackrory
    1) I'm more used to C-style strings
    Can't argue with that. Oh, perhaps you should get more used to C++ strings.

    2) Useable in C and C++
    Which is really not an issue in a program that uses cout and cin.

    3) Personally I thought C-style strings would be easier for newbies to grasp since they wouldn't have done much OOP yet, but I'm not one to argue with someone with as much knowledge of the language as Prelude.
    It's not about knowledge of the language, it's about common sense. The C++ string behaves like a built-in type, it just makes sense. The C-style string doesn't.

    Just a few examples. I'm assuming "using std::string". I want to put a string literal into a variable.
    Code:
    C++:
    string str = "Hello, World!";
    C:
    const char *str = "Hello, World!"; /* What is "const char *"?
    Why can't I say "char *"? The compiler allows it. Why can't I modify it? */
    char str[] = "Hello, World!"; /* "char []"? Weird syntax. */
    Yes, the syntax becomes second nature to C programmers, but newbies?

    String copying with unknown size:
    Code:
    C++:
    string str2 = str1;
    C:
    char *str2 = malloc(strlen(str1)+1);
    strcpy(str2, str1);
    /* And don't forget to free! */
    Again, all those questions. What is malloc? What is strlen? Why can't I just say str2 = str1? Why do I have to remember to free? Why shouldn't I return such stuff from a function?

    Concatenation:
    Code:
    C++:
    str1 += str2;
    C: (assuming that str1 was allocated with malloc)
    char *t = malloc(strlen(str1)+strlen(str2)+1);
    strcpy(t, str1);
    strcat(t, str2);
    free(str1);
    str1 = t;
    The code is getting longer.

    There are more issues. Buffer sizes. Why do I have to watch for buffer overflows? What is that, anyway? Why can't I return a local buffer from a function?

    And have you so far seen any signs of OOP in the C++ version? Anything that newbies would have to understand in order to use string? No, since it's a perfect blackbox. It might as well be built-in.

    Bottom line: newbies usually want to do things now, and later learn the whys and hows. It's simply more fun that way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    All hail CornedBee!

    Pizzapie:
    >>using namespace std; // you should always have this
    I beg to differ. For a more in-depth explanation, read this thread.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08: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