0

I do apologize if this has been asked before, but I haven't really seen this come up in my books or in other examples. So here I go.

I have been getting into a card game, after getting lazy with constant shuffling I made a program that does it for me, theoretically. I realize there are already programs that do so, but where is the fun in that? Onto the problem that led to the question. Whenever I typed in a card with a two or more word name, it chopped off all the words but the first. I have known this to happen but I don't know how to fix it normally. Let alone how to store "A, the C" in a vector and keep spaces.

The Question: How do I store a string like "A, the C" in a string and put it in a container and be able to retrieve it with the spaces in tact? Am I doing something wrong in the code, or am I using the wrong tool for the shed?

#include <iostream>
#include <string>

using namespace std;

int main()
{
string example = " ";
cin >> example; //typed eggs and milk, only got eggs
cout << example << endl;

}
Episha
  • 67
  • 1
  • 10
  • 2
    Your question has nothing to do with vectors in particular. Please keep simplifying your problem until it's [minimal](http://stackoverflow.com/help/mcve). – chris Apr 06 '15 at 01:52
  • 1
    @chris it's been re-edited in a simple fashion that still reproduces the final product. – Episha Apr 06 '15 at 02:03
  • string example = ""; getline(cin, example); found here: http://stackoverflow.com/questions/5455802/how-to-read-a-complete-line-from-the-user-using-cin – EmeraldOverflow Apr 06 '15 at 02:07

1 Answers1

2

Instead of

cin >> example;

use

std::getline(std::cin, example);

cin >> example; will stop reading when it finds a white space. std::getline will cotinue reading until a specified delimiter ('\n' by default) is found.

More on std::getline.

R Sahu
  • 204,454
  • 14
  • 159
  • 270