Problem with While condition in C++

Asked

Viewed 325 times

1

Hello, I’m studying from Bjarne’s book "PROGRAMMING Principles and Pratice Using C++". I’m trying to make Drill number one chapter four. The exercise tells you to make a program that consists of a while-loop that reads and prints two integers and stops when it is written |, however, when I write | (or anything other than a number) the program is in an infinite loop. If anyone can explain to me what is wrong with my code, I have tried to read in the book about conversions from int to char and vice versa and yet 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";
    }
}
  • 1

    Is there anything that says that the variables need to be int? Can you put the exercise statement? Or is that all there is to it?

  • /*write a program consisting of a while-loop that reads two int and ends when is written | */ is this, I will comment below the original of the book.

  • Write a program that consists of a while-loop that (each time Around the loop) reads in two int and then prints them. Exit the program when a terminating '|' is entered.

  • 1

    I’m trying to understand the intention of doing this to give an answer.

  • OK thanks so much while doing a review on Cap if I find something posted here @bigown

  • 1

    This was asked in the OS, but it seems that the exercise is another :)

  • It’s the Gomiero passed the link

Show 2 more comments

1 answer

1


According to the OS post (in English):

https://stackoverflow.com/questions/10349857/how-to-handle-wrong-data-type-input

The reason the program enters an infinite looping is because of one flag (invalid device input flag) Std::Cin.

What needs to be done is 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" input is occurring because of reading a (type) character "| " in a variable of the numeric type n or n1.

For more details (in English):

C++ FAQ

Other possibilities to correct:

  • read the content | for a type variable char
  • read each value for a type buffer string and then convert to whole with the function Std::stoi

Updating:

After applying the second correction suggestion, the looping is 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 reading suggestion to better understand how the C data input and output system works++:

Data input and output

  • Thank you very much Gomiero I will test and read the FAQ to get further inside.

  • well the infinite looping has resolved to however for min not mute nothing is as if the condition of the while does not exist now so write | it simply discards the | and asks to read another digit in the place instead of finishing the while, i already tried to read the content | on a char I will test making the conversation from string to int now that was the only option I haven’t tried so far. once again for the help

  • my teacher commented the following "If you do not change the value of one of the variables within the loop. She will always have the valo | and so the loop will be repeated...."

  • converted to string afterwards for vlw int helped a lot more the links thank you very much.

  • perfect exactly this logic that I used thanks again

  • saw I always come here to take a look at this explanation she solves high things yet XD have to take more care in these things thanks again man

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.