Thread: No show after a space?

  1. #1

    Angry No show after a space?

    I made this simple program to mess around with user input.

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	std::cout << "Sentence?: ";
    	std::string input;
    	std::cin >> input;
    	std::cout << "\nYou typed ";
    	std::cout << input;
    	return 0;
    }
    But whenever I run it, it works fine. Unless I type in more than 1 word, like "hotdog" works fine but "hot dog" only shows up as "hot". Why is this and how do I make it work the way I want it to?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    operator>> reads only up to the next whitespace character (space, tab, newline). You can use std::getline to read up to whatever delimiter you want:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	std::cout << "Sentence?: ";
    	std::string input;
    	std::getline(std::cin, input);
    	std::cout << "\nYou typed ";
    	std::cout << input;
    	return 0;
    }
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  2. Simple space combat AI
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-06-2009, 12:54 AM
  3. Find space inside Array?
    By hajas in forum C Programming
    Replies: 4
    Last Post: 06-21-2007, 07:46 AM
  4. Wireframe 3D engine problem
    By jmgk in forum Game Programming
    Replies: 9
    Last Post: 03-27-2006, 11:14 AM
  5. How to Convert Space in ascii?
    By larkin10 in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2005, 06:57 AM
Website Security Test