0

I am trying to ensure that the user inputs only int types to my program.

The code is below:

// Use namespace std to avoid needing to write things like std::cout
using namespace std;
// Include iostream for input / output interfacing
#include <iostream>
// Needed for error checking if inputs are actually ints
#include <cstdlib>

// Main function
int main(){
    // Introduction + instruction to input data
    cout << "Hello and welcome to Jerry's Triangle Test. " << endl << "Please submit three integers that represent the lengths of a triangle: \n";
    // Declare fundamental int types for user input
    int first_int, second_int, third_int;
    // Take in the three user inputs
    cin >> first_int;
    cin >> second_int;
    cin >> third_int;
    // Check to see if cin has taken in anything other than an int
    if (cin.fail()){
        // If it has, error the program out and return 1 to exit
        cout << "ERROR - Value was not an INTEGER. Please retry using an INTEGER.";
        return 1;
    // Otherwise, continue on
    }else{
        // Report back what the inputs were
        cout << "\nYour first integer was: " << first_int;
        cout << "\nYour second integer was: " << second_int;
        cout << "\nYour third integer was: " << third_int;

        // Perform logic to check what kind of triangle it must be from the entered dimensions.

        // If all three ints are equal, it is equilateral.
        if (first_int == second_int && second_int == third_int){
            cout << "\nEQUILATERAL";
        // If only two are equal, its isosceles.
        } else if (first_int == second_int || first_int == third_int || second_int == third_int){
            cout <<"\nISOSCELES";
        // If neither of those conditions are true, then none must match, so it is scalene.
        } else{
            cout <<"\nSCALENE";
        }
        // Return 0 for a successful program.
        return 0;
        }  

}

Entering 1,1,1 works as expected. However, entering: 1, 1, 0.2 yields ISOSCELES, when it should error out. Why does cin.fail() not notice that is incorrect?

But, if I enter 0.2 first or second, the program properly terminates.

I would hope that if the user enters a float ANYWHERE, the program errors out with "ERROR - Value was not an INTEGER. Please try using an INTEGER"

(It would be great to test each input individually to determine which was problematic, but I could not determine how to do that)

How can I correct my current codes to account for something as I am describing?

artemis
  • 6,857
  • 11
  • 46
  • 99
  • 1
    Input of `0.2` allows for correct extraction of `0`, the rest of the input remains in the stream for future reading – M.M Sep 02 '19 at 01:30
  • If I enter `0.2` first, it correctly errors out, but not when it is last @M.M It only occurs when the value is in the last position. – artemis Sep 02 '19 at 01:30
  • 1
    Yes, because if you extract `0` leaving `.2` in the stream , and then try to extract an int again, it is an error because `.` cannot form an `int` – M.M Sep 02 '19 at 01:31
  • I am sure you are correct but I do not understand why placing `0.2` at the end of the inputs breaks my logic, nor how to fix my logic to account for that. – artemis Sep 02 '19 at 01:32
  • Not on this issue, but sidenote: If you enter 1, 3, 5, then that is not a scalene triangle. That is not a triangle at all. I don't know if you want to/should extend it to test for that. – Daniel H Sep 02 '19 at 01:32
  • All of the steps work the same, entering `0.2` will accept the `0` and leave the `.2` for later. The last step doesn't have a "later" so you never notice the problem – M.M Sep 02 '19 at 01:33
  • My advice would be to read strings and then do string-to-int conversion – M.M Sep 02 '19 at 01:34
  • 1
    @DanielH I thought that too but following the instructions I was given here. – artemis Sep 02 '19 at 01:34
  • On the main issue: entering `1`, `1`, `1abc` will also work. It'll leave the abc in the stream in case the program later wants to read a string. This is doing the same thing, but with `.2` instead of `abc`. – Daniel H Sep 02 '19 at 01:34
  • How can I fix my program to account for these things? @DanielH – artemis Sep 02 '19 at 01:35
  • After the last `>>`, check `fail()`, and if not use `get()` to read the next character from `std::cin`. You should get a `'\n'`. If you get anything else, pretend that it's a `fail()`. – Sam Varshavchik Sep 02 '19 at 01:38
  • Can you please submit as an answer so I can test that out? @SamVarshavchik I am new and trying to look all of this stuff up but am feeling overwhelm – artemis Sep 02 '19 at 01:40
  • I'll do better than that, I closed this question as a dupe of a six-year old question that asked the same thing, and the answer in the linked question is what I would've written myself. I'll also add a few more dupes. – Sam Varshavchik Sep 02 '19 at 01:56

0 Answers0