Click here to Skip to main content
16,017,333 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i have this code, i don't know what is wrong in it, Can you help me to fix ? Thanks

C++
#include<stdio.h>
#include<conio.h>
#define PI 3.1415

float aria(float);
float perimetru(float);


void main(void)
{
float L, a, l, P;

printf("\nIntroduceti valoarea L: ");
scanf("%f", &l);
printf("\nIntroduceti valoarea l: ");
scanf("%f", &L);


printf("\n=======================================");

printf("\nPatratul are:");
printf("\nl = %f ", l);
printf("\nL = %f ", L);
printf("\nperimetru = %f ", perimetru(l, L));
printf("\naria = %f", aria(l && L));

getch();
}
float aria(float)
{ float a, l, L;
a = L * l;
return a;
}
float perimetru(float)
{ float P, l , L;
P = 2*(l+L);
return P;
}
Posted
Updated 23-Oct-13 18:46pm
v3
Comments
Andreas Gieriet 23-Oct-13 14:19pm    
What exectly is the problem? Which expression?
Cheers
Andi
Malasuerte94 23-Oct-13 14:22pm    
I think here is the problem
printf("\nperimetru = %f ", perimetru(l, L));
printf("\naria = %f", aria(l && L));

With this program i need to calculate the perimeter and area of ​​a square

Would you like a list?
Because the compiler will give you one for free, and you don't have to wait at all...

Lets start by looking at your functions for working out the results:
C#
float aria(float)
{ float a, l, L;
a = L * l;
return a;
}
float perimetru(float)
{ float P, l , L;
P = 2*(l+L);
return P;
}
What values do you expect these to return?
Because you are declaring all your variables locally, so aria is the equivalent of:
C++
float aria()
   {
   float a;
   a = 0.0 * 0.0;
   return a;
   }
You can't access L, a, L, or P from the declaration in your main function, because they are only in scope and available within the main function itself.
Instead, try this:
C++
float aria(float l1, float l2)
   {
   return l1 * l2;
   }
You will need to change the prototype to match.
Then call it like this instead:
C++
printf("\naria = %f", aria(l, L));

Then do the same fro perimetru.

And do yourself a favour: don't use single character variable names, and never, ever, ever use two variables that only differ by case! It is a very silly idea to have "l" and "L" in the same scope as it is far, far too easy to type the wrong one...
 
Share this answer
 
Comments
CPallini 23-Oct-13 15:23pm    
Let's rephrase it: never, ever, ever use single character variable names that only differ by case! :-D
I mean i is a perfect name for a variable.
And difference between fuel and FUEL is apparent.
(just kidding, of course)
Exact answer is.
C++
#include<stdio.h>
#include<conio.h>
#define PI 3.1415

float aria(float,float);
float perimetru(float,float);


void main(void)
{
float L, a, l, P;

printf("\nIntroduceti valoarea L: ");
scanf("%f", &l);
printf("\nIntroduceti valoarea l: ");
scanf("%f", &L);


printf("\n=======================================");

printf("\nPatratul are:");
printf("\nl = %f ", l);
printf("\nL = %f ", L);
printf("\nperimetru = %f ",perimetru(l, L));
printf("\naria = %f", aria(l,L));

getch();
}
float aria(float l1,float L1)
{float a;
a = L1*l1;
return a;
}
float perimetru(float l2,float L2)
{ float P;
P = 2*(l2+L2);
return P;
}
 
Share this answer
 
v2

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