I am learning C: a little problem with a simple source code

Z

zemir

Hi, this is my first message on this group............ In these days i
am studing C language and i wrote some codes. Today i am writing a
small programm to calculate the perimeter and the area of a circle;
this is:

#include <stdio.h>;
#include <math.h>;
float area(float r)
{ /*define area
function*/
return(r*r*3.14);
}
float perimeter(float r)
{ /*define perimeter
function*/
return(2*3.14*r);
}
void main() {
system("cls");
float r=0; int c=121 /*define radius and c variable used to
control the behaviour of the
program;
it is set to 121 value (y
char)*/
printf("This programm calculate Area and Perimenter of a Circle");
do {
printf("\n\nPlease insert radius value (in cm): ");
scanf("%f", &r); /*input of the radius value*/
if(r>0) {
printf("Area is cm2: ");
printf("%.2f", area(r)); /
*call area function*/
printf("\nPerimeter is cm: ");
printf("%.2f", perimeter(r)); /*call
perimeter function*/
} else printf("Value must to be a valid number");
printf("\nDo you want to continue? (y or elsewhere to exit)");
c=getch();
} while(c==121);
printf("\nEND\n");
}

I use Tcc compiler on Windosw XP. The program works fine, i wrote it
to get positive value number else it print a warning message. However
also in the case i make a mistake and write a string or a char it
display a warning message but if i try to remake the calculate it does
not work as well as i should want
Why?.
I have tried to resete r value to zero:
else {
r=0;
printf("Value must to be a valid number");
}
but it is the same.
I hope in your help.
Thanks!
 
M

Mark Bluemel

Hi, this is my first message on this group............ In these days i
am studing C language and i wrote some codes. Today i am writing a
small programm to calculate the perimeter and the area of a circle;
this is:

#include<stdio.h>;
#include<math.h>;
float area(float r)
{ /*define area
function*/
return(r*r*3.14);
}
float perimeter(float r)
{ /*define perimeter
function*/
return(2*3.14*r);
}
void main() {
system("cls");
float r=0; int c=121 /*define radius and c variable used to
control the behaviour of the
program;
it is set to 121 value (y
char)*/
printf("This programm calculate Area and Perimenter of a Circle");
do {
printf("\n\nPlease insert radius value (in cm): ");
scanf("%f",&r); /*input of the radius value*/
if(r>0) {
printf("Area is cm2: ");
printf("%.2f", area(r)); /
*call area function*/
printf("\nPerimeter is cm: ");
printf("%.2f", perimeter(r)); /*call
perimeter function*/
} else printf("Value must to be a valid number");
printf("\nDo you want to continue? (y or elsewhere to exit)");
c=getch();
} while(c==121);
printf("\nEND\n");
}

I use Tcc compiler on Windosw XP. The program works fine, i wrote it
to get positive value number else it print a warning message. However
also in the case i make a mistake and write a string or a char it
display a warning message but if i try to remake the calculate it does
not work as well as i should want
Why?.
I have tried to resete r value to zero:
else {
r=0;
printf("Value must to be a valid number");
}
but it is the same.
I hope in your help.
Thanks!

Using scanf for interactive input is (almost?) always a mistake...

Read the C FAQ at <http://c-faq.com> Section 12...
 
N

nroberts

        printf("This programm calculate Area and Perimenter of a Circle");
        do {
                printf("\n\nPlease insert radius value (in cm): ");
                scanf("%f", &r);  /*input of the radiusvalue*/

scanf leaves the input on the stream if it can't read it into the type
it expects. Try reading the entire line with fgets and then using
sscanf on the string buffer you read into.
 
J

Jens Thoms Toerring

zemir said:
Hi, this is my first message on this group............ In these days i
am studing C language and i wrote some codes. Today i am writing a
small programm to calculate the perimeter and the area of a circle;
this is:
#include <stdio.h>;
#include <math.h>;
float area(float r)
{
return(r*r*3.14);
}
float perimeter(float r)
{
return(2*3.14*r);
}

I would perfer to have a global variable like this

const double Pi = 3.14159265358979323846;

or a define 3.14159265358979323846

#define PI

Also, why not use double instead of float? They need a bit
more memory but tend to be faster and then you got a lot more
precision.
void main() {

The correct form of main() is either

int main( void )

or

int main( int argc, char ** arhv )
system("cls");

Don't do that, first of all "cls" is not a command on all systems
out there and second it annoys most users when a program clears the
screen without a very good reason.
float r=0; int c=121

Why the initialization of 'c'? And when you insist in it why
not initioalize it to something readable (and more portable),
i.e. with

c = 'y';

printf("This programm calculate Area and Perimenter of a Circle");
do {
printf("\n\nPlease insert radius value (in cm): ");

Please note: unless you have a '\n' at the end of the output
line you better make sure the string has made it to the user.
It may not have done so and may still be in some internal
buffers. This, in this case, call

flush( stdout );

after that line.
scanf("%f", &r); /*input of the radius value*/

scanf() can fail to read a number, as you found out, e.g. when
the input didn't consist of just a number. In that case scanf()
leaves the input buffer unmodified and it returns 0 (the return
value tells you how many items it could read. Thus it would be
prudent to check the return value also and if it isn't 1 (indi-
cating that it did read something that can be interpreted as a
number) you have to clean out the input buffer - otherwise the
next time round it will try to read again what it just rejected.
The simplest way to clear the input buffer is to call getc()
until it returns the end-of-line character '\n' (or EOF in
case the user closed the input stream).
if(r>0) {
printf("Area is cm2: ");
printf("%.2f", area(r)); /
*call area function*/
printf("\nPerimeter is cm: ");
printf("%.2f", perimeter(r)); /*call
perimeter function*/
} else printf("Value must to be a valid number");
printf("\nDo you want to continue? (y or elsewhere to exit)");
c=getch();
} while(c==121);
printf("\nEND\n");
}
I use Tcc compiler on Windosw XP. The program works fine, i wrote it
to get positive value number else it print a warning message. However
also in the case i make a mistake and write a string or a char it
display a warning message but if i try to remake the calculate it does
not work as well as i should want
Why?.

See above.
I have tried to resete r value to zero:
else {
r=0;

That won't help since the problem are the data remaining in
the input buffer. So do something like this

if ( scanf( "%f", &r ) != 1 )
{
int in;

while ( ( in = getc( ) ) != '\n' && in != EOF )
/* empty */ ;

if ( in == EOF )
return 0;
}

to clean out the input buffer if the user passed you something
that's not a number. Note that 'in' is an int, not a char. That
is what 'getc() returns (a char can not be tested for EOF, which
indicates in your case that the input has been closed - if you
would read from a file that would tell you that you reached the
end of the file). For that reason it might be prudent to also
make 'c' an int instead of a char.

Regards, Jens
 
M

Malcolm McLean

I use Tcc compiler on Windosw XP. The program works fine, i wrote it
to get positive value number else it print a warning message. However
also in the case i make a mistake and write a string or a char it
display a warning message but if i try to remake the calculate it does
not work as well as i should want
Why?.
scanf() returns the number of arguments successfully converted.

do this

while(scanf("%f\n", &r) != 1)
{
printf("Can't read that number, try again\n');
}
 
I

Ike Naar

scanf() returns the number of arguments successfully converted.

do this

while(scanf("%f\n", &r) != 1)
{
printf("Can't read that number, try again\n');
}

Apart from the typo in the closing quote of the printf
argument, this will loop forever if the input is not a
valid float, e.g. if the stream contains "pqr".
You probably want to flush the erroneous input before
retrying the scanf operation.
 
Z

zemir

Hi all and thanks for your suggestions, but i have solved. I think my
solution it is not good solution and more of you will be not very
happy but it seems to work fine, very fine. Please read:

#include <stdio.h>;
#include <math.h>;
const float Pi = 3.14159265358979323846;
float area(float r)
{ /*define area
function*/
return(r*r*Pi);
}
float perimeter(float r) { /
*define perimeter function*/
return(2*Pi*r);
}
void main() {
system("cls");
float r=0; int c;/*define radius and c variable used to control the
behaviour of program*/
printf("This programm calculate Area and Perimenter of a Circle");
printf("\n\nPlease insert radius value (in cm): "); /*input of the
radius value*/
scanf("%f", &r);
if(r>0) {
printf("Area is cm2: ");
printf("%.2f", area(r)); /*call area
function*/
printf("\nPerimeter is cm: ");
printf("%.2f", perimeter(r)); /*call perimeter
function*/
} else {
printf("Value must to be a valid number\n");
printf("Press anykey to continue");
c=getch();
system("circle");
return 0;
}
printf("\nDo you want to continue? (y or elsewhere to exit)");
c=getch();
if(c==121) {
system("circle");
return 0;
}
}

Then i have another question about variable c: the y value of int c, i
think, it is ascii code so it is the same in all the system?
 
Z

zemir

Hi all and thanks for your suggestions, but i have solved. I think my
solution it is not good solution and more of you will be not very
happy but it seems to work fine, very fine. Please read:

#include <stdio.h>;
#include <math.h>;
const float Pi = 3.14159265358979323846;
float area(float r)
{ /*define area
function*/
return(r*r*Pi);
}
float perimeter(float r) { /
*define perimeter function*/
return(2*Pi*r);
}
void main() {
system("cls");
float r=0; int c;/*define radius and c variable used to control the
behaviour of program*/
printf("This programm calculate Area and Perimenter of a Circle");
printf("\n\nPlease insert radius value (in cm): "); /*input of the
radius value*/
scanf("%f", &r);
if(r>0) {
printf("Area is cm2: ");
printf("%.2f", area(r)); /*call area
function*/
printf("\nPerimeter is cm: ");
printf("%.2f", perimeter(r)); /*call perimeter
function*/
} else {
printf("Value must to be a valid number\n");
printf("Press anykey to continue");
c=getch();
system("circle");
return 0;
}
printf("\nDo you want to continue? (y or elsewhere to exit)");
c=getch();
if(c==121) {
system("circle");
return 0;
}
}

Then i have another question about variable c: the y value of int c, i
think, it is ascii code so it is the same in all the system?
 
J

James Kuyper

Hi all and thanks for your suggestions, but i have solved. I think my
solution it is not good solution and more of you will be not very
happy but it seems to work fine, very fine. Please read:

#include <stdio.h>;
#include <math.h>;

On most machines, storing Pi with this many digits in a float is a waste
of digits. You should use 'double', or even better, 'long double'.
const float Pi = 3.14159265358979323846;
float area(float r)
{ /*define area
function*/
return(r*r*Pi);
}
float perimeter(float r) { /
*define perimeter function*/
return(2*Pi*r);
}
void main() {
system("cls");
float r=0; int c;/*define radius and c variable used to control the
behaviour of program*/
printf("This programm calculate Area and Perimenter of a Circle");
printf("\n\nPlease insert radius value (in cm): "); /*input of the
radius value*/
scanf("%f", &r);

You haven't bothered to check whether or not scanf() succeeded. You do
realize that it can fail, don't you? What do you want your program to do
when that it is the case?
if(r>0) {
printf("Area is cm2: ");
printf("%.2f", area(r)); /*call area
function*/
printf("\nPerimeter is cm: ");
printf("%.2f", perimeter(r)); /*call perimeter
function*/
} else {
printf("Value must to be a valid number\n");
printf("Press anykey to continue");
c=getch();

You also haven't bothered to check for the possibility that getch()
might have failed.
system("circle");
return 0;
}
printf("\nDo you want to continue? (y or elsewhere to exit)");
c=getch();
if(c==121) {
system("circle");

You also haven't bothered to check whether any of your system() calls
failed. Whether or not that's reasonable depends upon whether or not
"cls" or "circle" are capable of failing - which is outside the scope of
the C standard.
return 0;
}
}

Then i have another question about variable c: the y value of int c, i
think, it is ascii code so it is the same in all the system?

No, 'y' is a integer constant with a value that depends upon which
encoding is being used by the C implementation. It will be the ASCII
code value only if the implementation use ASCII or an ASCII-compatible
encoding such as Unicode (which is most modern systems, but it's nowhere
near to being all systems).

The thing is, 'y' will be the correct value to use, even on systems
where it's not the ASCII code for 'y', because on such systems, when the
user types 'y' on his keyboard, getch() should return exactly the same
non-ASCII value that is represented by 'y'.
 
I

Ian Collins

Hi all and thanks for your suggestions, but i have solved. I think my
solution it is not good solution and more of you will be not very
happy but it seems to work fine, very fine. Please read:

#include<stdio.h>;
#include<math.h>;
const float Pi = 3.14159265358979323846;

Why don't you use doubles?
float area(float r)
{ /*define area
function*/
return(r*r*Pi);
}
float perimeter(float r) { /
*define perimeter function*/
return(2*Pi*r);
}
void main() {

This should be int main(). On modern compilers, this won't compile
because you are returning values.
system("cls");

This won't do anything on a system without a "cls" command.
float r=0; int c;/*define radius and c variable used to control the
behaviour of program*/
printf("This programm calculate Area and Perimenter of a Circle");
printf("\n\nPlease insert radius value (in cm): "); /*input of the
radius value*/
scanf("%f",&r);
if(r>0) {

It would be better to test the return of scanf rather than r.
printf("Area is cm2: ");
printf("%.2f", area(r)); /*call area
function*/
printf("\nPerimeter is cm: ");
printf("%.2f", perimeter(r)); /*call perimeter
function*/
} else {
printf("Value must to be a valid number\n");
printf("Press anykey to continue");
c=getch();

getch() is non-standard.
system("circle");

Are you trying to get the program to invoke its self? Not a good idea!
return 0;
}
printf("\nDo you want to continue? (y or elsewhere to exit)");
c=getch();
if(c==121) {

Why 121? Why don't you use a character ('y')?
system("circle");
return 0;
}
}

Then i have another question about variable c: the y value of int c, i
think, it is ascii code so it is the same in all the system?

As long as the system uses ASCII! Never use character values, use the
character.
 
Z

zemir

I have tried to resolve with other ways as well as i understand but i
was not able to resolve it; only this way works such as i want. I have
changed cost Pi from float to bouble and i have changed c control
value from '121' to 'y'. Now all works fine. I think this binary can
run well on any other windows pc as well as run on my pc. The only
problem is if anyone want to change the name of binary but i think
this is not a common choice.

This should be int main(). On modern compilers, this won't compile
because you are returning values.
Yes, of course........... int main() is more right but my choice is
such common that it is not more a error......
 
Z

zemir

I have tried to resolve with other ways as well as i understand but i
was not able to resolve it; only this way works such as i want. I have
changed cost Pi from float to bouble and i have changed c control
value from '121' to 'y'. Now all works fine. I think this binary can
run well on any other windows pc as well as run on my pc. The only
problem is if anyone want to change the name of binary but i think
this is not a common choice.

This should be int main(). On modern compilers, this won't compile
because you are returning values.
Yes, of course........... int main() is more right but my choice is
such common that it is not more a error......
 
B

Ben Bacarisse

James Kuyper said:
On 11/01/2011 02:51 PM, zemir wrote:

You also haven't bothered to check for the possibility that getch()
might have failed.

If it's the getch I remember, it can't fail -- by which I mean there is
no error return that can tell you anything.

<snip>
 
Z

zemir

I have tried to resolve with other ways as well as i understand but i
was not able to resolve it; only this way works such as i want. I have
changed cost Pi from float to bouble and i have changed c control
value from '121' to 'y'. Now all works fine. I think this binary can
run well on any other windows pc as well as run on my pc. The only
problem is if anyone want to change the name of binary but i think
this is not a common choice.

This should be int main(). On modern compilers, this won't compile
because you are returning values.
Yes, of course........... int main() is more right but my choice is
such common that it is not more a error......
 
J

James Kuyper

On 11/01/2011 04:17 PM, zemir wrote:
....
Yes, of course........... int main() is more right but my choice is
such common that it is not more a error......

Just because it's a popular error doesn't make it cease to be an error.
Get it right now, while you're still learning. The fewer bad habits you
pick up now, while you're still new to the language, the less you'll
have to unlearn before you can be employable.
 
I

Ian Collins

I have tried to resolve with other ways as well as i understand but i
was not able to resolve it; only this way works such as i want. I have
changed cost Pi from float to bouble and i have changed c control
value from '121' to 'y'. Now all works fine. I think this binary can
run well on any other windows pc as well as run on my pc. The only
problem is if anyone want to change the name of binary but i think
this is not a common choice.


Yes, of course........... int main() is more right but my choice is
such common that it is not more a error......

Well it is an error if you use a decent compiler. In addition to being
simply wrong, a void function can't return a value!
 
Z

zemir

On 11/01/2011 04:17 PM, zemir wrote:
...


Just because it's a popular error doesn't make it cease to be an error.
Get it right now, while you're still learning. The fewer bad habits you
pick up now, while you're still new to the language, the less you'll
have to unlearn before you can be employable.

Ok, from now and in the future i will use always "int main()"
I am sure thai it is the right choice.
 
Z

zemir

On 11/01/2011 04:17 PM, zemir wrote:
...


Just because it's a popular error doesn't make it cease to be an error.
Get it right now, while you're still learning. The fewer bad habits you
pick up now, while you're still new to the language, the less you'll
have to unlearn before you can be employable.

Ok, from now and in the future i will use always "int main()"
I am sure thai it is the right choice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top