How to get a float number when the variable type is integer? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

How to get a float number when the variable type is integer?

Hello, I'm just a beginner to C++, so I'm already sorry if my question is boring or stupid. I have this program: https://code.sololearn.com/cZCHbfcOK8eN/?ref=app It gets the four values a, b, c, x and returns the y while y = ax² + bx + c. When I enter a float number as the given value to a/b/c/x the program terminates and the wanted operation would not be done, my question is what should I do, that if the user gives a float number, instead of terminating the whole operation, an alert pops up that "Please enter an integer number" and it again asks for a value. My efforts : I thought I may want to use an "If" that checks the variable type of (for example) a, but I have encountered two problems, 1. By using : #include <typeinfo> Int a; Typied(a).name() C++ returns i (stands for integer) even if I assign a float number to it. 2. I don't know how to define what I want, I tried If ( Typied(a).name() == i) ; Else cout << "enter an integer value"; Which obviously doesn't work.

6th Nov 2022, 6:19 AM
Root
Root - avatar
8 Antworten
+ 2
Well, yeah . If I understand your words correctly you want to take input as an int and if the variables are in float then it will take input again by showing that message. You need an infinite while loop to do that, it simply breaks if the condition is not met otherwise the loop is continued. https://code.sololearn.com/c2tcJ77aajH2/?ref=app I guess, there isn't a way to set the primary type as int if you take input inside those input variables or maybe I don't know the way.
6th Nov 2022, 7:36 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 2
We actually have declared "a" inside the parameter of the function as float. floatornot(float a) is a function that takes a float input .When the function is called returns true or false since the return type is bool. so when you call floatornot(b) the value of "b" becomes the value of "a" inside the function and do the task inside the function. The variable "a" declared in the floatornot function and the variable "a" declared in the main function are different. It doesn't change each other value. They are totally different. Both are the local variables of two different function which doesn't affect anyone even the name is same.
6th Nov 2022, 9:44 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
For this, you have to take input as a float first. Then you can check whether the value can be converted to Int or not using (int) typecast, in this case, 5.0 will be considered as an int variable but 5.1 won't.Here's the code: https://code.sololearn.com/c1ceeb6q2KHv/?ref=app Reference of my answer: https://stackoverflow.com/questions/20068234/c-how-to-check-if-the-number-is-integer-or-float
6th Nov 2022, 6:52 AM
The future is now thanks to science
The future is now thanks to science - avatar
6th Nov 2022, 7:43 AM
Root
Root - avatar
+ 1
The future is now thanks to science Can I ask one more question? include <iostream> #include <cmath> using namespace std; bool floatornot(float a){ if (a==(int)a){ return true; } else{ return false; } } why in this piece of code, despite we hadn't declared "a" before if, the program returns no error?
6th Nov 2022, 9:21 AM
Root
Root - avatar
6th Nov 2022, 1:48 PM
Root
Root - avatar
0
The road I'm thinking of to get me where I want could be wrong, I know, I'll be thankful if anyone can point me to the right direction.
6th Nov 2022, 6:19 AM
Root
Root - avatar
0
The future is now thanks to science Thanks for your help, if I want that after printing "enter a valid integer input" the program gets started again, I should use a loop or there's a statement to do it like cin.get() that is used for stoping the program? isn't there anyway to set the primary type as int?
6th Nov 2022, 7:04 AM
Root
Root - avatar