0

So I was trying to read something from cin and spaces cut them, For example, if I got

AA 3 4 5
111 222 33

from cin, I want to store them in a string array. My code so far is

string temp;
int x = 0;
string array[256];
while(!cin.eof())
{
    cin >> temp;
    array[x] = temp;
    x += 1;
}

but then the program crashed. Then I added cout to tried figure out what is in temp and it shows:

AA345

So how can I store the input into an array with spaces cutting them?

SergeyA
  • 61,605
  • 5
  • 78
  • 137
GzAndy
  • 141
  • 3
  • 10
  • Possible duplicate of [C++ cin input with spaces?](http://stackoverflow.com/questions/5838711/c-cin-input-with-spaces) – Sviatoslav Yakymiv Feb 02 '16 at 21:54
  • @SviatoslavYakymiv, that is actually the exact opposite of suggested duplicate. – SergeyA Feb 02 '16 at 21:55
  • @SviatoslavYakymiv eh no, I mean I want to store every word in an array,with spaces cutting them. So in the example, I want to store AA in array[0], and store 3 in array[1], 4 in array[2] etc. – GzAndy Feb 02 '16 at 21:57
  • You need to provide the whole code, and indicate exactly where it crashed. There is nothing in the provided code which would cause an aplication to crash, unless you have more than 256 tokens, which is unlikely. – SergeyA Feb 02 '16 at 21:58
  • @SergeyA , I'd love to but I am afraid I may violate some rules... – GzAndy Feb 02 '16 at 22:00
  • @GzAndy, than you might well delete your posting. I know a couple of users here do have working crystal balls, but they are not active in this thread, and I do not have one - so nobody would be able to help you. – SergeyA Feb 02 '16 at 22:02
  • @SergeyA, Let's forget about the crashes cause I just figured out that it is not this part of code. So my concern now is how to divide the cin, ie. if I add cout<>temp, it shouldn't shows :"AAA 3 4 5" instead of "AAA345" – GzAndy Feb 02 '16 at 22:05
  • 1
    @GzAndy, you still need to provide the code. I have no idea how you are printing your stuff. – SergeyA Feb 02 '16 at 22:10
  • Do you have more than 256 inputs? – NathanOliver Feb 02 '16 at 22:25

1 Answers1

1

Here's one possibility to treat an input from cin with an arbitrary number of whitespaces between the entries, and to store the data in a vector by using the boost library:

#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
int main() {
  std::string temp;
  std::vector<std::string> entries;
  while(std::getline(std::cin,temp)) {  
      boost::split(entries, temp, boost::is_any_of(" "), boost::token_compress_on);
      std::cout << "number of entries: " << entries.size() << std::endl;
      for (int i = 0; i < entries.size(); ++i) 
        std::cout << "entry number " << i <<" is "<< entries[i] << std::endl;                  
    }  
  return 0;
}

edit

The same result could be obtained without using the awesome boost library, e.g., in the following way:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main() {
  std::string temp;
  std::vector<std::string> entries;
  while(std::getline(std::cin,temp)) {    
      std::istringstream iss(temp);
      while(!iss.eof()){ 
        iss >> temp;
        entries.push_back(temp);    
      }
      std::cout << "number of entries: " << entries.size() << std::endl;
      for (int i = 0; i < entries.size(); ++i)  
        std::cout<< "entry number " << i <<" is "<< entries[i] << std::endl;
      entries.erase(entries.begin(),entries.end()); 
    }
  return 0;
}

example

Input:

AA 12  6789     K7

Output:

number of entries: 4
entry number 0 is AA
entry number 1 is 12
entry number 2 is 6789
entry number 3 is K7

Hope this helps.

RHertel
  • 23,412
  • 5
  • 38
  • 64