Click here to Skip to main content
15,071,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 5hrs 10mins ago
v2

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.
   
Comments
   
Instead of using a comma to separate the inputs I used a space..
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 ;-)
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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