Problem with condition in While in C ++

1

Hello, I'm studying from Bjarne's "PROGRAMMING Principles and Pratice Using C ++" book. I'm trying to make Drill number 1 from chapter four. The exercise says to do a program that consists of a while-loop that reads and prints two integers and stops when it is written |, but when I write | (or anything other than a number) the program sits in an infinite loop. If someone can explain to me what is wrong with my code, I have already tried to read it in the book about conversions from int to char and vice versa and still the error persists, take a look.

#include <...\Projects\std_lib_facilities.h>

int main()
{
    //drill's
    //1 - inicio

    /*escrever um programa que consiste em um while-loop que leia dois int e termine quando 
é escrito |  */

    int n=0, n1=0;

    cout << "Write a int. To stop the program hit |";

    while (n != '|' || n1 != '|' )
    {
        cin >> n >> n1;

        system("cls");

        cout << n << " " << n1 << "\n";
    }
}
    
asked by anonymous 01.02.2016 / 19:21

1 answer

1

According to the post in the SO:

link

  

The reason the program goes into infinite looping is because of   an invalid flag (flag) of the device    std :: cin .

     

What needs to be done is to clear the flag and discard the invalid entry   of the input buffer.

After the correction, the code looks like this:

while (n != '|' || n1 != '|')
{
    // limpa a flag
    std::cin.clear(); 

    // Descarta a entrada
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    std::cin >> n >> n1;

    system("cls");

    std::cout << n << " " << n1 << "\n";
}

The "wrong" entry is occurring because of the reading of a (type) " | " character in a variable of numeric type n or n1 .

For more details (in English):

C ++ FAQ

Other possibilities to fix:

  • read the content | for a variable type char
  • read each value for a buffer of type string and then convert to integer with the std: : stoi

Update:

After applying the second correction suggestion, the looping looks like this:

int n = 0, n1 = 0;

std::string buffer;

std::cout << "Write a int. To stop the program hit |";
while (n != '|' || n1 != '|')
{
    // Lê a primeira entrada
    std::cin >> buffer;

    // Se for um "|", sai do looping
    if (buffer == "|")
        break;
    // Converte para inteiro
    n = std::stoi(buffer);

    // Lê a segunda entrada
    std::cin >> buffer;

    // Se for um "|", sai do looping
    if (buffer == "|")
        break;

    // Converte para inteiro
    n1 = std::stoi(buffer);

    // Limpa a fila std::cin
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    // std::cin >> n >> n1;

    //system("cls");

    std::cout << n << " " << n1 << "\n";
}

Even with this fix, the above code is still subject to other errors.

Below is a suggested reading to better understand how the C ++ data entry and exit system works:

Data entry and exit

    
01.02.2016 / 19:53