after stopping, entering inifinite loop becuase of char data being entered in int data type, The program is printing value which I am unable to figure

Viewed 30

Initially, I was trying to write a program to ask the user to enter only a negative integer value and until entered, it will be calling itself in a recursive way. Like:

#include <iostream>
using namespace std;

void printNegativeNum () {
    int neg_no;
    cout << "Enter a negative number: " << endl;
    cin >> neg_no;
    if (neg_no >= 0){
        cout << "Please enter only negative number." << endl;
        printNegativeNum();
    }
    cout << neg_no;
}

int main(){
    printNegativeNum();
    return 0;
}

This code on being given char input was entering infinite loop, leading to segmentation fault.

Then, I studied about dealing with this and found few solutions for while loop and tried to do the in above recursive function.

#include <iostream>
using namespace std;

void printNegativeNum () {
    int neg_no;
    cout << "Enter a negative number: " << endl;
    if(cin >> neg_no) {
        if (neg_no >= 0){
            cout << "Please enter only negative number." << endl;
            printNegativeNum();
        }
    }
    cout << neg_no << " ";
}

int main(){
    printNegativeNum();
    return 0;
}

This thing worked and no more entering the infinite loop. But, now all the previous integer values entered before the char value which leads to program termination, getting printed as if the court << neg_no << " "; is getting executed and it was carrying all the values entered before the char constant. I am unable to understand how?

I in trail and error used cin.clear() before the cout statement. But, that too was in vain.

Please Help.

1 Answers

The problem is that once the second call of the function printNegativeNum returns, the first call of the function printNegativeNum will finish executing, so it too will execute the line cout << neg_no;, which will print garbage.

I don't recommend that you use recursion for solving this problem. Instead, what you want is a loop.

Also, you should test the fail state of the stream, in order to determine whether the input was successfully converted. If the input is found to be invalid, then you should clear the fail state of the stream using cin.clear and should discard the bad input using cin.ignore.

#include <iostream>
#include <limits>
using namespace std;

void printNegativeNum () {
    int neg_no;

    //the following loop will run forever, until the input is
    //found to be valid
    while ( true )
    {
        //prompt user for input
        cout << "Enter a negative number: ";
        cin >> neg_no;

        //determine whether input is valid
        if ( !cin.fail() && neg_no < 0 )
            break;

        //print error message
        cout << "Please enter only negative number.\n";

        //clear flags on input stream
        cin.clear();

        //discard bad input
        cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }

    cout << neg_no;
}

int main(){
    printNegativeNum();
    return 0;
}

This program now has the following behavior:

Enter a negative number: test
Please enter only negative number.
Enter a negative number: 7
Please enter only negative number.
Enter a negative number: -5
-5
Related