Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<stdio.h>
int main()
{
    float num1,num2;
    printf("Enter 1,2 float: ");
    scanf("%.2f %.2f",&num1, &num2);
    printf("Result:%.2f %.2f\n",num1,num2);
    return 0;
}


What I have tried:

Output is 0.00 whatever the input I give. Why can't I find the output I give in inout?.in codeblock I tried.
Posted
Updated 11-Nov-23 21:02pm
v2

You cannot specifiy width or precision values on numbers that are to be read. The scanf function will attempt to read all valid characters (digits and decimal point) that make up a number, and convert it to the appropriate type. So change your scanf line to:
C++
scanf("%f %f",&num1, &num2);
 
Share this answer
 
Comments
CPallini 13-Nov-23 2:05am    
5.
The precision specifier (%.2f) is used in the 'printf' function instead of 'scanf' to control the number of decimal places when displaying the float values.
For example, %10.4f displays a number at least 10 characters wide in total and four of them are decimal places.
 
Share this answer
 
Comments
CPallini 13-Nov-23 2:05am    
5.
Have also a look at the documentation: function scanf - cppreference[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900