- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find out the data type of user input in C++
In this problem, we are given input from the user. Our task is to create a program to find out the data type of user input in C++.
Problem Description − We will take input from the user and check the data type of the input value.
Let’s take an example to understand the problem,
Example 1:
Input − 34
Output − It is an integer
Example 2:
Input − tutorialspoint
Output − It is a string
Solution Approach:
We will check if the input string is a number or not a number.
If it is a number, we will check if it is an integer or a float value.
If it is not a number, we will check if it is a string.
Example
#include <math.h> #include <stdio.h> #include <stdlib.h> int main() { char input[50] = ""; double temp; int intVal; char stringVal[50] = ""; double val = 1e-12; fgets(input, 100, stdin); if (sscanf(input, "%lf", &temp) == 1) { intVal = (int)temp; if (fabs(temp - intVal) / temp > val) printf("The input is a floating point\n"); else printf("The input is an integer\n"); } else if (sscanf(input, "%s", stringVal) == 1) printf("The input is a string\n"); else printf("input not recognized\n"); }
Input:
452
Output:
The input is an integer
- Related Articles
- C program to find type of array entered by the user.
- C program to find array type entered by user using pointers.
- C++ set for user define data type
- C++ set for user defined data type?
- Swift Program to Get Input from the User
- Java Program to Get Input from the User
- Haskell program to get input from the user
- C++ map having key as a user defined data type
- Convert the input to a masked array of the given data-type in Numpy
- Java Program to fill an array of characters from user input
- C++ program to find the type of the given iterator
- C++ Program to find out the health of a character
- C++ program to find out the maximum value of i
- C++ Program to find out the total price
- Program to find out the number of non-zero submatrices in C++

Advertisements