0

Don't hate cause of Turbo, I already hate my school!

I wish to show an error msg if a character is entered instead of an int or float in some file such as age or percentage. I wrote this function:

template <class Type>
Type modcin(Type var) {
    take_input:  //Label
    int count = 0;
    cin>>var;
    if(!cin){
        cin.clear();
        cin.ignore();
        for ( ; count < 1; count++) { //Printed only once
            cout<<"\n Invalid input! Try again: ";
        }
        goto take_input;
    }
    return var;
}

but the output is not desirable: OUTPUT SCREEN

How do I stop the error msg from being repeated multiple times? Is there a better method?


NOTE: Please make sure that this is TurboC++ that we are talking about, I tried using the approach in this question, but even after including limits.h, it doesn't work.

  • 1
    This is example of bad use of goto. Don't construct loops with goto. Use actual loops. – hyde Oct 10 '17 at 13:58
  • @hyde I understand that, I used goto after I tried for and do-while loops, I was trying different things to eliminate the multiple prints. I posted the goto code cause thats the last try I made. – Ankur Singh Oct 11 '17 at 03:54
  • @AnkurSingh you are goto every pass so it is an endless loop... Add `if` so the goto is executed only if you have the wrong input .... but `for` would be nicer with probably much less code ... – Spektre Oct 11 '17 at 08:06
  • @Spektre I understand the point but I’ve tried using for and while. It was a while back but I couldn’t get rid of the multiple prints when entering strings into console. I even tried putting cin>>var; out of the loop and checking it’s state in a while loop but that gave me multiple outputs too for some reasons. – Alpha Mineron Oct 19 '17 at 18:03
  • @AlphaMineron are you using TurboC++ too? – Ankur Singh Oct 21 '17 at 17:27
  • @Spektre The goto isn't happening on everypass, its not endless. It's only encountered when the input is incorrect. The goto is within the if statement. I've fixed the indent issue. – Ankur Singh Oct 21 '17 at 17:30
  • @AnkurSingh I know a bit of both. – Alpha Mineron Oct 21 '17 at 21:01

1 Answers1

1

Here, a code snippet in C++.

template <class Type>
Type modcin(Type var) {
    int i=0;
    do{
        cin>>var;
        int count = 0;
        if(!cin) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            for ( ; count < 1; count++) {  //Printed only once
                cout<<"\n Invalid input! Try again: ";
                cin>>var;
            }
        }
    } while (!cin);
    return var;
}

The variables are tailored to match yours' so you can understand better. This code isn't perfect though.

  • It can't handle cases like "1fff", here you would just get a 1 in return. I tried solving it but then a infinite loop was being encountered, when I'll fix it, I'll update the code.
  • It also can't function in TurboC++ effectively. I don't know if there are alternatives but the numeric_limits<streamsize>::max() argument gives a compiler error ('undefined symbol' error for numeric_limits & streamsize and 'prototype must be defined' error for max()) in Turbo C++.

So, to make it work in Turbo C++. Replace the numeric_limits<streamsize>::max() argument with some big int value such as 100. This will make it so that the buffer is only ignored/cleared till 100 characters are reached or '\n' (enter button/newline character) is pressed.

EDIT


The following code can be executed on both Turbo C++ or proper C++. The comments are provided to explain the functioning:

template <class Type>             //Data Integrity Maintenance Function
Type modcin(Type var) {           //for data types: int, float, double
    cin >> var;
    if (cin) {  //Extracted an int, but it is unknown if more input exists
         //---- The following code covers cases: 12sfds** -----//
        char c;
        if (cin.get(c)) {  // Or: cin >> c, depending on how you want to handle whitespace.
            cin.putback(c);  //More input exists.
            if (c != '\n') {  // Doesn't work if you use cin >> c above.
                cout << "\nType Error!\t Try Again: ";
                cin.clear();  //Clears the error state of cin stream
                cin.ignore(100, '\n');  //NOTE: Buffer Flushed <|>
                var = modcin(var);  //Recursive Repeatation
            }
        }
    }
    else {    //In case, some unexpected operation occurs [Covers cases: abc**]
        cout << "\nType Error!\t Try Again: ";
        cin.clear();  //Clears the error state of cin stream
        cin.ignore(100, '\n');  //NOTE: Buffer Flushed <|>
        var = modcin(var);
    }
    return var;

//NOTE: The '**' represent any values from ASCII. Decimal, characters, numbers, etc.
}
Alpha Mineron
  • 348
  • 1
  • 2
  • 13