0

Whenever I put in anything that isn't an int, it outputs good morning instead of the error under else

#include <iostream>
using namespace std;

int main() {
    int time;
    
    cout << "what time is it?\n";
    
    cin >> time;
    
    if (time < 12)
    {
        cout << "Good morning\n";
    }
    else if ( time >= 12)
    {
        cout << "Good afternoon\n";
    }
    else 
    {
        cout << "Sorry, i do not recognize that input\n";
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • When asking a question related to code, the very first tag you should add should be the one that indicates what programming language you're using. Please [edit] your post to add that tag. – Ken White Oct 03 '22 at 01:39
  • someone already did lol. Can u help tho – Mocode10 Oct 03 '22 at 01:40
  • 1
    You're reading `int`. `int` is always an integer. – Evg Oct 03 '22 at 01:44
  • 1
    Yes, I saw that someone did the work for you. Please keep it in mind in the future. When someone who can answer wants to do so, they will; using the proper tag helps get the question in front of them. My area of expertise isn't C++. I will ask, though - what happens if someone enters 33, or 55, or 65535, or -1, or -50? AFAIK, `cin` will only read an `int` value, because you've told it that's what it's supposed to read. – Ken White Oct 03 '22 at 02:14
  • Change `cin >> time;` to `if (!(cin >> time)) throw runtime_error("oops");` because what is happening is the input is failing and the code was not checking to see if the input succeeded. – Eljay Oct 03 '22 at 03:13

0 Answers0