0

I cant use the s1[40] for the second time or longer in the circle and it's always full,

and cin.getline(s1,40) is ignored at later times

char s1[40], ans = 'y';
while (ans == 'y')
{       
system("cls");
cout << "\n Enter a sentence : ";
cin.getline(s1, 40);
fflush(stdin);

cout << "\n________________________________________\n\n Again?(y/n)";
cin >> ans;
fflush(stdin);
};
MPERSIA
  • 187
  • 1
  • 15

1 Answers1

0

cin >> ans; will actually not remove eol so next getline will read an empty line and cin >> ans; will read first symbol of line. You should make ans an array as well use getline twice:

for(;;)
{
    char s1[40]{};
    system("cls");
    cout << "\n Enter a sentence : ";
    cin.getline(s1, 40);
    fflush(stdin);
    if(cin.fail() or cin.bad())
    {
        cout << "fail" << endl;
        break;
    }
    cout << "\n________________________________________\n\n Again?(y/n)";
    char ans[2]{};
    cin.getline(ans, 2);
    fflush(stdin);
    if(cin.fail() or cin.bad())
    {
        cout << "fail" << endl;
        break;
    }
    if(0 != strcmp("y", ans))
    {
        break;
    }
}
user7860670
  • 35,849
  • 4
  • 58
  • 84