0

I want to Know that is there any way to get input from user without data type in C or C++. Generally we take any variable and store input in that particular variable.

I want to check data type of input after getting from user.

  • 1
    Read the input as a string, then parse the string. – Jesper Juhl Sep 02 '18 at 13:05
  • If the user inputs a sequence of digits, how can you tell whether that should be interpreted as a string, or an integer, or a telephone number, or a date, or a time, or a postal code, or a personal identification number, or something else? – molbdnilo Sep 02 '18 at 13:46
  • The sugested answer doesn't answer the question. Try using fread with stdin as FILE stream. en.cppreference.com/w/cpp/io/c/fread – Tobias Sep 12 '18 at 17:06

1 Answers1

2

You don't need to "check data type of input after getting from user" because the type of all input from user is C-string, known in advance. If you want to treat it differently, e.g. to convert "0.1" to double, the compiler has not idea how to do this. You should provide this info, e.g. you can request user to input what type it should be converted to and a value. Or to implement rules of automatica conversion specific for your application.

Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112
  • My faculty told me. it is possible using (void *) and told me to search on that. I try to find answer but didn't get it. So it is possible using void * to get input from user without data type? – Dharmendra Bhanushali Sep 02 '18 at 13:14
  • @DharmendraBhanushali I don't see how `void *` could be used here. Again, read input as string. – HolyBlackCat Sep 02 '18 at 13:15
  • 1
    @DharmendraBhanushali See https://stackoverflow.com/questions/52125956/why-is-void-considered-unsafe-in-c/52126248#52126248 please. And no, at least for input you need to have a data type. `std::string` is the most flexible thing, to take any kind of input first. – πάντα ῥεῖ Sep 02 '18 at 13:16
  • You can use void * as a pointer to memory where the user input is. However, you would then also need to know the length of the user input. When you have it you can cast it to unsigned char userInput[length] and then do whatever you like with the content. –  Sep 02 '18 at 14:35