No show after a space?

This is a discussion on No show after a space? within the C++ Programming forums, part of the General Programming Boards category; I made this simple program to mess around with user input. Code: #include <iostream> #include <string> int main() { std::cout ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    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, 12:09 PM
  2. Simple space combat AI
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-05-2009, 11:54 PM
  3. Find space inside Array?
    By hajas in forum C Programming
    Replies: 4
    Last Post: 06-21-2007, 06:46 AM
  4. Wireframe 3D engine problem
    By jmgk in forum Game Programming
    Replies: 9
    Last Post: 03-27-2006, 10:14 AM
  5. How to Convert Space in ascii?
    By larkin10 in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2005, 05:57 AM

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