Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > I am learning C: a little problem with a simple source code

Reply
Thread Tools

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

 
 
zemir
Guest
Posts: n/a
 
      11-01-2011
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!
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      11-01-2011
On 11/01/2011 03:27 PM, zemir wrote:
> 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...
 
Reply With Quote
 
 
 
 
nroberts
Guest
Posts: n/a
 
      11-01-2011
On Nov 1, 8:27*am, zemir <(E-Mail Removed)> wrote:

> * * * * 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.

> *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!


 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-01-2011
zemir <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      11-01-2011
On Nov 1, 5:27*pm, zemir <(E-Mail Removed)> wrote:
>
> 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');
}
--
Lots of C programming resources
http://www.malcolmmclean.site11.com/www



 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      11-01-2011
On 2011-11-01, Malcolm McLean <(E-Mail Removed)> wrote:
> On Nov 1, 5:27?pm, zemir <(E-Mail Removed)> wrote:
>>
>> 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');
> }


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.
 
Reply With Quote
 
zemir
Guest
Posts: n/a
 
      11-01-2011
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?
 
Reply With Quote
 
zemir
Guest
Posts: n/a
 
      11-01-2011
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?
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-01-2011
On 11/01/2011 02:51 PM, zemir wrote:
> 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'.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-01-2011
On 11/ 2/11 07:51 AM, zemir wrote:
> 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.

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[I'm learning C]: Learning to use ucontext Andrey Popp C Programming 5 01-31-2012 01:05 AM
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
having a little problem with some code for a little game I am creating. ThaDoctor C++ 3 09-28-2007 03:28 PM
Learning C and Learning Make/Configure/Building/Linking Hal Vaughan C Programming 7 03-21-2006 05:07 PM
e-learning, (collaborative learning environment) collinm Java 1 09-08-2005 09:52 PM



Advertisments