0

Hello how to get all strings until find space and push_back the words until space in the second turn of For loop to start getting all string after space and again until find space that's my code

for example this sentece 5bbbb3 1f a0aaa f1fg3

i want to get bbbb and push_back into in a vector of chars then to push_back aaaa and so

vector of chars vec = vec.[0] == 'bbbb' vec.[1] == 'aaaa' vec.[2] == 'f' vec.[3] == 'ffg'

Thank you in advanced

these are my 2 codes both does not work

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){

    string sentece;
    getline(cin, sentece);
    vector<char> words;

    for (int i = 0; i < sentece.size(); ++i)
    {
        while (sentece.substr(i, ' '))
        {
            if(isalpha(sentece.at(i)))
            {
                words.push_back(sentece.at(i));

            }
        }
    }
    cout << words[0] << '\n';
    cout << words[1] << '\n';
    cout << words[2] << '\n';

    for(const auto& a : words)
    {
        cout << a;
    }

        return 0;
}

//==================================================================


#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){

    string sentece;
    getline(cin, sentece);
    vector<char> words;

    for (int i = 0; i < sentece.size(); ++i)
    {
        while (sentece.at(i) != ' ')
        {
            if(isalpha(sentece.at(i)))
            {
                words.push_back(sentece.at(i));

            }
            if(sentece.at(i) == ' ')
            {
                break;
            }
        }
    }

     cout << words[0] << '\n';
    cout << words[1] << '\n';
    cout << words[2] << '\n';

    for(const auto& a : words)
    {
        cout << a;
    }


        return 0;
}
Who123
  • 29
  • 5
  • 1
    So there's one immediately obvious mistake. `vector words;` This is a vector of chars, but `"bbbb"` is a string, so you can't push it onto a vector of chars. chars and strings are different things. What you want is `vector words;`. So throw away the code so far, start with a vector of strings and try again. – john Apr 10 '20 at 10:28
  • 1
    Adding to the fray, nothing changes `sentece` nor `i` *within* the while loop of the first example, so that thing will loop forever pushing the same `sentece.at(i)` character into the vector until you exhaust your process memory (which may take awhile, depending on your disks space, OS, and virtual paging system). – WhozCraig Apr 10 '20 at 10:34
  • Thank 's but the main problem is that i don't know how to take all words until space and push_back it to the vector and these pushed back words to be stored in one index of the vector ? and the second question is if i make all strings than while (sentece.at(i) != " ") it showings me this error of the != this part error(operand types are incompatible "char" and "const char *") – Who123 Apr 10 '20 at 10:43
  • 1
    It's clear you haven't understood the difference between a `string` and a `char`. That code should be `while (sentece.at(i) != ' ')` because `sentece` is a string so `sentece.at(i)` is a char. Until you've understood the difference this is going to be very difficult for you. – john Apr 10 '20 at 10:47
  • Yes i know know i'm studying for string and streams and i'm trying to understand it by making some code . Thank you for the help. i will try harder to learn it . Thanks very much again – Who123 Apr 10 '20 at 11:05

2 Answers2

1

I believe this code should give you the answer you want:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    string test = "5bbbb3 1f a0aaa f1fg3";
    vector<string> words;

    for (int i = 0; i < test.size(); i++)
    {
        string tmp = "";
        while (test[i] != ' ' && i < test.size())
        {
            if (isalpha(test.at(i))){
                tmp += test[i];
            }
            else if (test.at(i) == ' ')
            {
                i++;
                break;
            }
            i++;
        }
        words.push_back(tmp);
    }

    cout << words[0] << '\n';
    cout << words[1] << '\n';
    cout << words[2] << '\n';
    cout << words[3] << '\n';
}

All you have to do is then replace the test sentence with your user input. You were forgetting to increment i in the while loop so it was examining the same character every time and getting stuck infinitely.

I tried to use as much of your original code as possible so don't assume that this is the most efficient or elegant solution to the problem

Hope this helps :)

Sam
  • 104
  • 4
0

You can use character array with scanf() `

#include<bits/stdc++.h>
    using namespace std;
    int main()
   {
       char s[1000];
       scanf("%[^' ']%s", s);
       cout<<s;
   }

This will stop taking input untill you hit enter but this will store string upto only till first occurence of space.

Ankit Mishra
  • 530
  • 8
  • 16
  • [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “`using namespace std;`” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Ted Lyngmo Apr 10 '20 at 17:25