I Want to know why this happens with getch?!

Hey guys, this is my code !

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
cout << "What is your favourite number?";
cout << endl;
int FavNumber;
cin >> FavNumber;
cout << "You favourite number is " << FavNumber;
cout << " !!!";
cout << endl;
getchar();
getchar ();
return 0;

}





For the sake if knowledge only, may you please tell me why that when i run my program then enter a letter for my favourite number instead of a number the program closes. It doesnt do that with getch! And Also when i ran the program and wrote lots of numbers for my favourite number, the program said my favourite number was '-858993460', Please tell me why! Thanks In Advance!
enter a letter for my favourite number instead of a number the program closes

because your program was expecting a number, on wrong input execution stops.. you can put a check on this as as an error.

wrote lots of numbers for my favourite number, the program said my favourite number was '-858993460

because signed int has limit -32767 to 32767, if input exceeds this limit you get error.
you can use long instead of int for this.
Last edited on
signed int has limit -32767 to 32767

Unless, you are working on an ancient machine, int and long are the same, and their range is from 2147483647 to -2147483648
Topic archived. No new replies allowed.