mad libs

This is a discussion on mad libs within the C++ Programming forums, part of the General Programming Boards category; <string> using namespace std; That worked. However, being that it has its own header file, I assumed it would work ...

  1. #16
    The Orange Pean
    Guest

    Doh, stupid me

    <string>
    using namespace std;

    That worked. However, being that it has its own header file, I assumed it would work better than using a character array, but I still have the same problem.

    #include <iostream>
    #include <string>
    int main()
    {
    string Name;
    Name = ""
    cin<<Name<<endl;
    cout<<"Hello <<Name<<endl;
    }

    If I input Thomas, the output is:

    Hello Thomas

    However, if I input Thomas Anderson, the output is

    Hello Thomas

    How can I include spaces and such in one string?

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

    ~

    I'm sure there's a better way to get input with the string class, but as I've never needed to, I don't know it.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main (void)
    {
    	char buffer[256];
    	cin.getline(buffer,256);
    
    	string s(buffer);
    	cout << s << endl;
    
    	return 0;
    }
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  3. #18
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
    	string userInput;
    	string Greeting("Hello ");
    
    	cout << "Please enter your name: ";
    	getline(cin, userInput);
    
    	Greeting += userInput;
    
    	cout << Greeting << endl;
    
    	return 0;
    }
    You can use the "+=" operator (or the "+" operator), with strings to concatenate strings.

    Code:
    #include <iostream>
    #include <string>
    int main()
    {
    string Name;
    Name = ""
    cin<<Name<<endl;
    cout<<"Hello <<Name<<endl;
    }
    Your code does not compile - You need to include a "using namespace std;" after including the iostream header. There is a ; missing from your assignment to Name. << is not appropriate for cin objects - it should be >> . You also have not closed off the last cout statement.

    The reason that you are getting the behaviour your seeing is that cin's default behaviour is to read only up until the first whitespace. The rest of your input is still sitting in the input buffer.. i.e. if you where to do this:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string Name;		// For user input
    	string Cleanup;		// Cleanup what is in the buffer
    	
    	cout << "Please enter both names: ";	// ask for the users input
    	
    	cin >> Name;							// Only text up to the first whitespace
    											// is stored in Name
    	cout << "First name: " << Name << endl; 
    
    	cin >> Cleanup;							// The next call to cin places what is left
    											// in the input buffer into string Cleanup
    	cout << "This is the second name: " << Cleanup << endl;
    
    }
    So, if you are looking to get input for more than one word at a time, it is more appropriate to use the getline() method.

    NOTE: This is not the best way of removing unwanted data in the input buffer. Lookup the ignore() method.
    Last edited by foniks munkee; 01-18-2003 at 10:34 PM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

Page 2 of 2 FirstFirst 12
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, 03: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

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