2

Consider this code:

#include <iostream>
#include <string>
using namespace std;
int main(){
   int n = 1;
   string data[13];
   while(n > 0){
      cin >> n;
      for(int i = 0; i < n; i++){
         getline(cin,data[i]);
         cout << data[i] << endl;
      }
   }
}

i compiled this in cppdroid on my android. I want to hold n lines in the array. (n <= 13). Everything is fine about that. But when I input an integer in the first line of program, it prints one blank line and on the third line the program takes input for lines. My console window looks like this:

2

This is line 1.
This is line 1.
And this is line 2.
And this is line 2.

I want to remove unwanted spaces.

3 Answers3

4

The

cin >> n;

consumes only the digits you've entered, and leaves a newline (at the least) in the input buffer.

This newline is then read and output by the first executed

getline(cin,data[i]);
cout << data[i] << endl;

To avoid it you can use getline of a string also for the integer input, and e.g. convert to integer via stoi. Or, less robust, you can call the ignore method on the stream to ignore everything up to and including the next newline. It's less robust because formatted input of an integer can put the stream in an error mode where it ignores further input operations until the error mode is cleared.

Re the array, better use a std::vector. Then you don't need to decide on a fixed capacity.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Tried that.cppdroid gives a compiler error. stoi not declared. need a update version may be? :/ – Redwanul Haque Sourave Dec 13 '15 at 15:35
  • @RedwanulSourav: [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) was introduced with C++11 (we're now at C++14), and resides in the `` header. With g++ you need to use `-std=c++11` or `-std=c++14`. – Cheers and hth. - Alf Dec 13 '15 at 16:01
1

cin >> n reads the number but leaves the newline in the input stream. Your first call to getline reads that, so you start up with an empty line.

0

The problem is you are mixing calls to getline() with the use of the operator >>. Remember that operator >> ignored leading white space so will correctly continue across lines boundaries. But stops reading after the input has successfully been retrieved and thus will not swallow trailing '\n' characters. Thus if you use a getline() after a >> you usually get the wrong thing unless you are careful (to first remove the '\n' character that was not read).

The trick is to not use both types of input. If you're using getline after cin >> yourdata, you need to flush the newline out of the buffer in between.

    #include <iostream>
    #include <string>
    using namespace std;
    int main(){
        int n = 1;
        string data[13];
        while(n > 0){
            cin >> n;
            cin.ignore(10,'\n'); // insert this into your code
            for(int i = 0; i < n; i++){
                getline(cin,data[i]);
                cout << data[i] << endl;
            }
       }
       return 0;
    }
Anand
  • 119
  • 1
  • 11