[C++] Question regarding input data type. Helppp :|

In summary, the conversation discusses overloading functions and the issue of an unknown parameter's data type. The code provided includes a class with two example functions, one for int and one for double, and a main function where the user inputs a value for the unknown parameter q. The conversation concludes that in order to use separate example functions for different input types, the input type must be checked before calling the function.
  • #1
cikon
2
0
Helo,

im doing overloading function.

example
class a
{
... example(int a)
... example(double b)
};

int main()
{
a. b;
cout << "Enter value";
cin>>q;
b.example(q)

}

what is q's data type ?
 
Technology news on Phys.org
  • #2
cikon said:
Helo,

im doing overloading function.

example
class a
{
... example(int a)
... example(double b)
};

int main()
{
a. b;
cout << "Enter value";
cin>>q;
b.example(q)

}

what is q's data type ?

You tell us - it's your code, which by the way won't compile, since q is not declared.
 
  • #3
im sorry for not making it clear. act i want to ask, what is the data type of unknown parameter. like q. it can be either int or double. and we do not know what is the type will be inserted by the user.
 
  • #4
No, it is not unknown - you have to declare it before using it in main(). Once it is declared and example(q) is called compiler already knows q type and calls the correct overload.

Edit: I think I know what you mean, you have no idea what the user will input beforehand. You have to write code that will analyze the input before storing the information in the variable. q type is not decided during runtime depending on what the user enters, it must be declared earlier. If you need separate example() functions for different input types, you need to call them AFTER checking what was the user input type.
 
Last edited:
  • #5


Hello, it seems like you are trying to create an overloaded function in C++ to handle different data types. In this case, the data type of q would depend on what the user inputs when prompted by the cin statement. If the user inputs an integer, q would be of type int. If the user inputs a decimal number, q would be of type double. It is important to make sure that the data type of q matches the parameter type in your overloaded function in order for it to work correctly. I hope this helps. Let me know if you have any further questions.
 

1. What is the input data type in C++?

The input data type in C++ is determined by the type of variable used to store the input. Some common data types include integers, floating-point numbers, characters, and strings. The type of input data is important for determining what operations can be performed on the input.

2. How do I specify the input data type in C++?

In C++, the data type of a variable can be specified by using a type specifier before the variable name. For example, to declare an integer variable named "num", you would use the syntax "int num;". This tells the computer to allocate memory for an integer value and associate it with the variable "num".

3. Can I change the input data type in C++?

Yes, it is possible to change the data type of a variable in C++. This is known as casting. For example, if you have an integer variable named "num" and you want to convert it to a floating-point number, you can use the syntax "float num = (float) num;". This will convert the value stored in "num" to a floating-point number and assign it to the variable "num".

4. What happens if I input a value of the wrong data type in C++?

If you input a value of the wrong data type in C++, it can lead to errors or unexpected results. For example, if a program is expecting an integer input and you provide a string, the program may crash or produce incorrect output. It is important to ensure that the input data type matches the expected data type to avoid these issues.

5. How do I handle user input of different data types in C++?

C++ provides a variety of techniques for handling user input of different data types. One approach is to use the cin object and the getline function to read in user input as a string, and then convert it to the desired data type using functions such as stoi (string to integer) or stof (string to float). Another approach is to use the scanf function, which allows you to specify the data type of the input in the format string. It is important to validate and handle user input carefully to avoid errors and unexpected behavior in the program.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
860
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
Replies
10
Views
950
  • Programming and Computer Science
Replies
6
Views
8K
Back
Top