r/learnc • u/Lance_2564_ • 16d ago
What's the c equivalent of this python code?
def get_float(prompt: str):
while True:
try:
return float(input(prompt))
except ValueError:
print("Please enter a valid number!")The code essentially just runs till the user gives a float or a int since float('a') throws ValueError which is caught by the except block.
What's the C equivalent of this?
3
Upvotes
2
u/barkingcat 16d ago
I'd break this into at least 3 parts.
The loop
The prompt and accepting data from user safely
The float conversion.
In c I don't think user can type in a float/int because that's a numeric data type. You will need to convert chars into floats and there's a lot of different ways to do it, but I imagine you could do the verification at the same time.
1
u/Poddster 15d ago edited 14d ago