-2
int test, flag = 0;
cin >> test;
while (test--)
{
    if (flag == 0)
        cin.ignore(256, '\n'); // using because of whitespace reading due to cin
    string check;
    getline(cin, check);
    checkPangram(check, check.size());
    flag++;
}

If i remove line 4 then this program does't read few test cases string, so i used flag value to execute line 4 only once in starting. if you can help me find for any general method, to read string(with spaces) so that i can read even after cin or getline without any loss of input

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • Possible duplicate of [Why would we call cin.clear() and cin.ignore() after reading input?](https://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input) – Nick is tired Dec 31 '19 at 19:32
  • I would check here for more reasoning. [When and why do I need to use cin.ignore in c](https://stackoverflow.com/questions/25475384/when-and-why-do-i-need-to-use-cin-ignore-in-c) – patemotter Dec 31 '19 at 19:33
  • 1
    In general `ignore` after the operation that leaves unwanted data in the stream, not before the next operation. If you `ignore` before in case there is unwanted data in the stream, sooner or later you will ignore data you want because there was nothing in the stream that you didn't. Getting more general, if you keep related operations close together the intent of the code becomes more obvious and easier to work with. – user4581301 Dec 31 '19 at 19:53

2 Answers2

3

You can simply call cin.ignore() before entering the loop:

int test;
if (cin >> test)
{
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    while (test--)
    {
        string check;
        getline(cin, check);
        checkPangram(check, check.size());
    }
}

Alternatively, you could use std::getline() for all of the input, using a std::istringstream to read test from the 1st line:

int test;
string check;

getline(cin, check);
istringstream iss(check); 

if (iss >> test)
{
    if (test--)
    {
        getline(iss, check);
        checkPangram(check, check.size());

        while (test--)
        {
            getline(cin, check);
            checkPangram(check, check.size());
        }
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

Move it out of the loop

int test;
cin >> test;
cin.ignore(256, '\n');
while (test--)
{
    string check;
    getline(cin, check);
    checkPangram(check, check.size());
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62