TillApril
TillApril

Reputation: 39

Infinite Loop in C++ function

I have written a very little program that adds two integers:

#include <iostream>
using namespace std;

int inpInt() {            //ask for an input in integer format
    int number = 0;
    cin >> number;       
    while (!cin) {        //keep asking if the input was not an integer
        cin >> number;
    }
    return number;
}

int main() {
    int summand1, summand2, sum;
    cout << "Summand 1: "; summand1 = inpInt();
    cout << "Summand 2: "; summand2 = inpInt();
    sum = summand1 + summand2;
    cout << "Sum: " << sum << "\n";
}

The problem is that when I do not enter an integer, I am trapped in an infinite loop where I cannot enter neither an integer nor anything else. The strange thing is that when I include the integer testing loop in main(), the code works.

Thank you very much for any help!

Upvotes: 3

Views: 143

Answers (4)

Syed Mohib Uddin
Syed Mohib Uddin

Reputation: 716

Can try this one converting number to char and checking its ascii code as number starts from 48 in ascii.

#include <iostream>
using namespace std;

int inpInt() {            //ask for an input in integer format
    char number = 48;
    cin >> number;
    
    int c = char(number);
    while (c < 48 || c > 58) {       
        cin >> number;
        c = char(number);
    }
    return number;
}

int main() {
    int summand1, summand2, sum;
    cout << "Summand 1: "; summand1 = inpInt();
    cout << "Summand 2: "; summand2 = inpInt();
    sum = summand1 + summand2;
    cout << "Sum: " << sum << "\n";
}

Upvotes: 0

TillApril
TillApril

Reputation: 39

Ok. Thank you very much for your suggestions. My problem is that I don't have enough knowledge about istreams yet. Clearing cin was a solution.

Another solution, as Myname suggested, is using isdigit. The program now reads a string from cin, tests it with isdigit and then transforms it with atoi to int. In case that another newbie reads this discussion, this is the code that worked:

#include <iostream>
using namespace std;

bool checkInt(string str) {
   for (int i = 0; i < str.length(); i++) {
       if (isdigit(str[i]) == false) {
       return false;
    }
   }
   return true;
}

int getInteger () {
    string inpStr;
    bool checkIntFailed = true;
    while (checkIntFailed) {
        cin >> inpStr;
        if (checkInt(inpStr))
            checkIntFailed = false;
        else cout << "Error: no integer. Try again: ";

    }
    return atoi(inpStr.c_str());
}

int main () {
    int iSummand1, iSummand2, iSum;
    cout << "1st summand: "; iSummand1 = getInteger();
    cout << "2nd summand: "; iSummand2 = getInteger();
    iSum = iSummand1 + iSummand2,
    cout << "Sum: " << iSum << "\n";
}

Have a nice weekend!

Upvotes: 0

Raildex
Raildex

Reputation: 4766

You have to explicitly clear cin and ignore newlines (newline appears when you hit return)

int inpInt() {            //ask for an input in integer format
    int number = 0;
    while (!(cin >> number)) {
        cin.clear(); // clear cin
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); // ignore new lines. see: http://www.cplusplus.com/reference/istream/istream/ignore/
    }
    return number;
}

int main() {
    int summand1, summand2, sum;
    cout << "Summand 1: "; summand1 = inpInt();
    cout << "Summand 2: "; summand2 = inpInt();
    sum = summand1 + summand2;
    cout << "Sum: " << sum << "\n";
}

Upvotes: 3

Myname
Myname

Reputation: 1

Try using the function isdigit().

#include <iostream>
using namespace std;

int inpInt() {            //ask for an input in integer format
    int number = 0;
    cin >> number;       
    while (isdigit(number))
     {        //keep asking if the input was not an integer
        cin >> number;
    }
    return number;
}

int main() {
    int summand1, summand2, sum;
    cout << "Summand 1: "; summand1 = inpInt();
    cout << "Summand 2: "; summand2 = inpInt();
    sum = summand1 + summand2;
    cout << "Sum: " << sum << "\n";
}

Upvotes: -1

Related Questions