Program to find the first largest number from the entered 5 number. When program says user to enter the first number then Check whether the entered statement is string or not if it is a string then call a user defined function in that is named as error() if it is a integer then store it and ask the user to enter second number.
Asked
Active
Viewed 79 times
-4
-
2Welcome to Stack Overflow. We are not code writing service. Try to do your homework on your own and if you stumble upon some problem, we'll be happy to help. – Yksisarvinen Jul 23 '18 at 13:42
-
1related/dupe: https://stackoverflow.com/questions/10828937/how-to-make-cin-take-only-numbers – NathanOliver Jul 23 '18 at 13:43
-
2Technically: everything the user inputs is a string. Even if such string contains characters, that resemble integer. – Algirdas Preidžius Jul 23 '18 at 13:43
-
[User Input of Integers - Error Handling](//stackoverflow.com/q/1283302) – 001 Jul 23 '18 at 13:45
-
Your assignment would be easier if you check that the input was an integer and call `error()` if not an integer. – Thomas Matthews Jul 23 '18 at 13:45
-
Related: https://stackoverflow.com/q/17982719/509868 – anatolyg Jul 23 '18 at 14:05
1 Answers
0
You can enter integers directly via cin
in <iostream>
:
int number;
std::cin >> number;
If the entered string is not a number, cin.fail()
will be set:
if(std::cin.fail()) {
// error handling...
}

npas
- 163
- 2
- 7
-
1Unfortunately, this does not work for cases like "5w" and "12.34". – Thomas Matthews Jul 23 '18 at 14:22
-
@ThomasMatthews I've written a [small test program](https://pastebin.com/pYcGadnU) and you seem to be right. However, in the very specific case of OP, it might just be enough. If you enter "344w" and `cin` parses `344`, it is good enough to work with for a largest-number program. Concerning your second example, since OP specified *integer*, I did not look for a floating-point solution. – npas Jul 23 '18 at 14:36