+ 1

Challange 2: Determine the User Input .

Get an Input from user and check whether the entered Input is Integer, String, Digit, Character, Alpha Numeric or Special Character ? If input is none of the above then display " Not a Valid Input " . An input is : a Digit ( not char ) a Character ( not a digit ) a String an Integer Number (can be any +'ve of -'ve) an Alpha Numeric input or can be a Special Character input. Quite a Simple one..... just to revice the basics .

4th Sep 2017, 5:00 PM
Gaurav
Gaurav - avatar
6 Respostas
4th Sep 2017, 6:19 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
@R- Ry I think there is an error in your code. See the std::getline() call. You used (input,128), where the new getline accepts a stream and a string object. (128, isn't a stream object, but a size). I think the call should be : std::cin.getline(input,128);
6th Sep 2017, 6:52 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
https://code.sololearn.com/cnxi7DID8NnK/#cpp Here's My Submission for the challenge. Please review.
6th Sep 2017, 8:12 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
@R- Ry, I think he means that an input is a digit a char (not a digit) an integer (not a digit) (you can add a floating number (neither integer nor digit)) a string (neither of the previous ones)
4th Sep 2017, 6:25 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
There's a small problem with this. A string can be any amount of characters or digits so basocally any type can represent a string if converted. A digit is also a character. But to a limited extent, we can check the input: #include <cstdio> #include <cstring> #include <iostream> char input[128]; bool is_string = false, is_int = false, is_char = false; char a[128]; int b; int main(){ std::getline(input, 128); if(sscanf(input, "%s", a) == 1){ is_sting = true; } if(sscanf(input, "%d", &b) == 1){; is_int = true; if(strlen(input) == 1){ is_char = true; } return 0; } The function sscanf is like extracting from an input stream but it uses a character string instead. The value it returns is the number of values extracted from the string. p.s. you could also check if it's a digit or alpha-numeric but just to keep things shorter, I left those out.
4th Sep 2017, 6:22 PM
R- Ry
R- Ry - avatar