0
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i=0;
    char a[100][100];
    do { 
        cin>>a[i];
        i++;
    }while( strcmp(a[i],"\n") !=0 );


    for(int j=0;j<i;i++) 
    {
        cout<<a[i]<<endl;
    }
    return 0;
}

Here , i want to exit the do while loop as the users hits enter .But, the code doesn't come out of the loop..

Hubert Bratek
  • 894
  • 10
  • 30
Great Person
  • 62
  • 10
  • 2
    Your code looks like it's mostly C, but your tag says C++. You should consider using `std::string` for your strings. – BobMorane Apr 01 '18 at 20:54
  • 1
    The easiest way to read everything up to and including `'\n'` is to use [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) – Bo Persson Apr 01 '18 at 21:04
  • The number 100 has nothing to do with the problem. That is what we call a BRF (big red flag). – Jive Dadson Apr 02 '18 at 00:05
  • 1
    Where did you learn `#include` and the C-style string stuff? Consider getting a [good book](https://stackoverflow.com/q/388242/9254539). – eesiraed Apr 02 '18 at 01:17
  • 2
    @Fei Xiang - Teaching bad C programming and calling it C++ is standard operating procedure at most schools. Instructors force students to write bad code by forbidding them to use C++ features before the instructor introduces them, which he does far too late if at all. – Jive Dadson Apr 02 '18 at 01:24
  • @JiveDadson I know... There are so many questions here causes by absolutely terrible teaching. – eesiraed Apr 02 '18 at 01:25
  • @Fei Xiang - You are right about the OP's question. I deleted my first answer and added a new one. – Jive Dadson Apr 02 '18 at 02:32
  • Operator `>>` stops when it encounters whitespace. If you want a complete line to be read, it is necessary to use another technique that stops when it encounters a newline but NOT when it encounters other whitespace, such as `std::getline()`. – Peter Apr 02 '18 at 03:53
  • @FeiXiang I appreciate your concern about telling it is not good to use c stuff with c++ thing .. i don't know why you posted all other rubbish comments .. be supportive or don't get into these things. I don't want your comments if this is how you want to say ..keep it with yourself – Great Person Jun 03 '18 at 06:22

3 Answers3

1

The following reads one line and splits it on white-space. This code is not something one would normally expect a beginner to write from scratch. However, searching on Duckduckgo or Stackoverflow will reveal lots of variations on this theme. When progamming, know that you are probably not the first to need the functionality you seek. The engineering way is to find the best and learn from it. Study the code below. From one tiny example, you will learn about getline, string-streams, iterators, copy, back_inserter, and more. What a bargain!

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
int main() {

    using namespace std;
    vector<string> tokens;
    {
        string line;
        getline(cin, line);
        istringstream stream(line);
        copy(istream_iterator<string>(stream),
            istream_iterator<string>(),
            back_inserter(tokens));
    }

    for (auto s : tokens) {
        cout << s << '\n';
    }
    return 0;
}
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
0

You canuse getline to read ENTER, run on windows:

//#include<bits/stdc++.h>
#include <iostream>
#include <string> // for getline()
using namespace std;

int main()
{
    int i = 0;
    char a[100][100];
    string temp;
    do {
        getline(std::cin, temp);
        if (temp.empty())
            break;
        strcpy_s(a[i], temp.substr(0, 100).c_str());
    } while (++i < 100);


    for (int j = 0; j<i; j++)
    {
        cout << a[j] << endl;
    }
    return 0;
}

While each getline will got a whole line, like "hello world" will be read once, you can split it, just see this post.

Brent81
  • 1,152
  • 1
  • 13
  • 19
0

First of all, we need to read the line until the '\n' character, which we can do with getline(). The extraction operator >> won't work here, since it will also stop reading input upon reaching a space. Once we get the whole line, we can put it into a stringstream and use cin >> str or getline(cin, str, ' ') to read the individual strings.

Another approach might be to take advantage of the fact that the extraction operator will leave the delimiter in the stream. We can then check if it's a '\n' with cin.peek().

Here's the code for the first approach:

#include <iostream> //include the standard library files individually
#include <vector>   //#include <bits/stdc++.h> is terrible practice.
#include <sstream>

int main()
{
    std::vector<std::string> words; //vector to store the strings
    std::string line;
    std::getline(std::cin, line); //get the whole line
    std::stringstream ss(line); //create stringstream containing the line
    std::string str;
    while(std::getline(ss, str, ' ')) //loops until the input fails (when ss is empty)
    {
        words.push_back(str);
    }
    for(std::string &s : words)
    {
        std::cout << s << '\n';
    }
}

And for the second approach:

#include <iostream> //include the standard library files individually
#include <vector>   //#include <bits/stdc++.h> is terrible practice.

int main()
{
    std::vector<std::string> words; //vector to store the strings
    while(std::cin.peek() != '\n') //loop until next character to be read is '\n'
    {
        std::string str; //read a word
        std::cin >> str;
        words.push_back(str);
    }
    for(std::string &s : words)
    {
        std::cout << s << '\n';
    }
}
eesiraed
  • 4,626
  • 4
  • 16
  • 34