Thread: mad libs

  1. #1
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262

    mad libs

    Im writing my first real C++ program I previously knew C but I want to switch to C++... well I wanted to write a mad libs program, but the problem is I used char as my data type for all the words, but Char is only 1 digit, so what would I use for a string data type?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use the class 'String', or use an array of characters, or a pointer to a character and allocate memory for them.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    In both C and C++, a char is one character. A string can be stored in a character array.

    char Name[20] ; // Can hold a a 19 character string plus the null termination.

    C-style strings still work in C++.

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    You shouldn't get into the habit of using c-style's in C++, imo.

    Heres an example code using strings:

    Code:
    #include <iostream.h>
    #include <string.h>
    
    
    int main(int argc, char* argv[])
    {
    	string users_name;
    
    	cout<< "Enter your name: ";
    	cin>>users_name;
    
    	cout<< "Hello " << users_name << endl;
    
    	return 0;
    }

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by RoD
    You shouldn't get into the habit of using c-style's in C++, imo.
    True, and you shouldn't get into habit of using old headers.
    <iostream>, <string> and the usual std:: stuff.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    if the .h isn't on iostream my VC++ 6.0 won't compile.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Are you sure? I have 6 and it compiles fine...
    You only run into problems which you mix and match old-style with new-style.

  8. #8
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i wasn't using namespace, i hadn't realized the connection, well i'll have to do it that way now,

    thnx guys.

  9. #9
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Is there a reason no one has offered up :
    PHP Code:
    char *pString;
    pString "What ever.";
    // or
    cin>>pString;
    // want to access individual letter
    cout << "letter: " << pString[2] << endl
    This seems really really flexible to me, And I'm a little concerned I haven't seen it pop up yet. Is there something I'm missing, something wrong with this method?
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    What does that have to do with this thread?

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by dbgt goten
    Is there a reason no one has offered up :
    PHP Code:
    char *pString;
    pString "What ever.";
    // or
    cin>>pString;
    // want to access individual letter
    cout << "letter: " << pString[2] << endl
    This seems really really flexible to me, And I'm a little concerned I haven't seen it pop up yet. Is there something I'm missing, something wrong with this method?
    That is only acceptable if you never wish to edit the string. When you just make a pointer to a string that you just defined with "string goes here", the string itself is placed in unwritable memory. If you ever try to edit anything in the string, you should in most cases get a big fat runtime error.

  12. #12
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Oh ok. I guess that clears it up for me.
    I can write a simple edit() function that can cover for the lad of character editing.

    Thanks for the clarification.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  13. #13
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Managing character arrays is a very tedious task and was a priority and nuisance for C coders. The C++ standard's string class is powerful, flexible, and robust, I recommend it's use for any serious string usage.

  14. #14
    The Orange Pean
    Guest
    Why doesnt this compile for me?

    #include <iostream>
    #include <cstring>
    using namespace std;
    int main()
    {
    string Hello;
    Hello = "Hello";
    cout<<Hello;
    return 0;
    }

    It doesnt see string as a data type. I am using Visual C++ 6.

  15. #15
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    /* ... */
    <cstring> is the C-header with C string functions like strlen(). <string> is the C++ header with the string class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic libs and undefined references
    By ccox421 in forum C Programming
    Replies: 5
    Last Post: 03-28-2009, 10:25 AM
  2. How do I make shared libs on Linux?
    By cpjust in forum C Programming
    Replies: 13
    Last Post: 11-05-2007, 04:54 PM
  3. I, Robot -- It's a trick, and I'm mad.
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-15-2004, 01:57 PM
  4. dx libs for borland c++
    By silk.odyssey in forum Game Programming
    Replies: 4
    Last Post: 06-16-2004, 12:09 AM
  5. Spam :mad:
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-22-2001, 10:43 PM