Click here to Skip to main content
15,900,439 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My function is not calculating the expected value instead printing some negative integers as result.

What I have tried:

// code to find area of rectangle using functions
#include<stdio.h>
#include<stdlib.h>

// function for calculating the area of rectangle
         float areaofrect(float l,float b)
         {
             float area;
             area=l*b;
             return area;
         }

//Driver function
         int main()
         {
             float a,b,A;
             printf("enter length,breadth of the rectangle:");
             scanf("%f,%f",&a,&b);
             A=areaofrect(a,b);
             printf("the area of the rectangle is:%f",A);
             return 0;
         }
Posted
Updated 22-Oct-21 22:41pm
v2

The input is wrong, must be delimited with space. Better is to write clean code and seperate both inputs:
C++
int main()
         {
             float length, breadth, area;
             printf("enter length:");
             scanf("%f",&length,);
             printf("breadth of the rectangle:");
             scanf("%f",&breadth);
             area = areaofrect(length,breadth);
             printf("the area of the rectangle is:%f",area);
             return 0;
         }
it is clearer, easier and understandable and more fun to read ;-)
 
Share this answer
 
It works fine for me: I paste your code into on online compiler, and enter two numbers. I get the result I expect:
enter length,breadth of the rectangle:2.0,3.3
the area of the rectangle is:6.600000

...Program finished with exit code 0
Press ENTER to exit console

So what am I doing that is different to you?
Check you have compiled the app and are running the right EXE file.
 
Share this answer
 
Comments
Nithish Karthikeyan 22-Oct-21 15:41pm    
Instead of using a comma to separate the inputs I used a space..

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