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

Discussion in 'C Programming' started by zemir, Nov 1, 2011.

  1. zemir

    zemir Guest

    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!
    zemir, Nov 1, 2011
    #1
    1. Advertising

  2. zemir

    Mark Bluemel Guest

    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...
    Mark Bluemel, Nov 1, 2011
    #2
    1. Advertising

  3. zemir

    nroberts Guest

    On Nov 1, 8:27 am, zemir <> 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!
    nroberts, Nov 1, 2011
    #3
  4. 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)
    > {
    > 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://toerring.de
    Jens Thoms Toerring, Nov 1, 2011
    #4
  5. On Nov 1, 5:27 pm, zemir <> 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
    Malcolm McLean, Nov 1, 2011
    #5
  6. zemir

    Ike Naar Guest

    On 2011-11-01, Malcolm McLean <> wrote:
    > On Nov 1, 5:27?pm, zemir <> 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.
    Ike Naar, Nov 1, 2011
    #6
  7. zemir

    zemir Guest

    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?
    zemir, Nov 1, 2011
    #7
  8. zemir

    zemir Guest

    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?
    zemir, Nov 1, 2011
    #8
  9. zemir

    James Kuyper Guest

    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'.
    James Kuyper, Nov 1, 2011
    #9
  10. zemir

    Ian Collins Guest

    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
    Ian Collins, Nov 1, 2011
    #10
  11. zemir

    zemir Guest

    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.

    On Nov 1, 8:06 pm, Ian Collins <> wrote:
    > > void main() {

    >
    > 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......
    zemir, Nov 1, 2011
    #11
  12. zemir

    zemir Guest

    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.

    On Nov 1, 8:06 pm, Ian Collins <> wrote:
    > > void main() {

    >
    > 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......
    zemir, Nov 1, 2011
    #12
  13. James Kuyper <> writes:
    > On 11/01/2011 02:51 PM, zemir wrote:

    <snip>
    >> c=getch();

    >
    > 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>
    --
    Ben.
    Ben Bacarisse, Nov 1, 2011
    #13
  14. zemir

    zemir Guest

    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.

    On Nov 1, 8:06 pm, Ian Collins <> wrote:
    > > void main() {

    >
    > 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......
    zemir, Nov 1, 2011
    #14
  15. zemir

    James Kuyper Guest

    On 11/01/2011 04:17 PM, zemir wrote:
    ....
    > On Nov 1, 8:06 pm, Ian Collins <> wrote:
    >>> void main() {

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


    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.
    James Kuyper, Nov 1, 2011
    #15
  16. zemir

    Ian Collins Guest

    On 11/ 2/11 09:19 AM, zemir wrote:
    > 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.
    >
    > On Nov 1, 8:06 pm, Ian Collins<> wrote:
    >>> void main() {

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


    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!

    --
    Ian Collins
    Ian Collins, Nov 1, 2011
    #16
  17. zemir

    zemir Guest

    On Nov 1, 9:24 pm, James Kuyper <> wrote:
    > On 11/01/2011 04:17 PM, zemir wrote:
    > ...
    >
    > > On Nov 1, 8:06 pm, Ian Collins <> wrote:
    > >>> void main() {

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

    >
    > 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.
    zemir, Nov 1, 2011
    #17
  18. zemir

    Ian Collins Guest

    On 11/ 2/11 09:23 AM, zemir wrote:
    > I have tried to resolve with other ways as well as i...


    You don't have to post 3 times!

    --
    Ian Collins
    Ian Collins, Nov 1, 2011
    #18
  19. zemir

    zemir Guest

    On Nov 1, 9:24 pm, James Kuyper <> wrote:
    > On 11/01/2011 04:17 PM, zemir wrote:
    > ...
    >
    > > On Nov 1, 8:06 pm, Ian Collins <> wrote:
    > >>> void main() {

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

    >
    > 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.
    zemir, Nov 1, 2011
    #19
  20. zemir

    zemir Guest

    On Nov 1, 9:27 pm, Ian Collins <> wrote:
    > On 11/ 2/11 09:23 AM, zemir wrote:
    >
    > > I have tried to resolve with other ways as well as i...

    >
    > You don't have to post 3 times!
    >
    > --
    > Ian Collins


    ok, i have a little problem with my browser.........now i have solved,
    i hope.....
    zemir, Nov 1, 2011
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jester.dev

    Learning c, need a little help with compiling.

    jester.dev, Feb 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    346
    jester.dev
    Feb 16, 2004
  2. ThaDoctor
    Replies:
    3
    Views:
    380
    Alan Woodland
    Sep 28, 2007
  3. Andrew Savige
    Replies:
    10
    Views:
    445
    Hrvoje Niksic
    Jan 11, 2008
  4. timr
    Replies:
    2
    Views:
    192
  5. Daniel
    Replies:
    1
    Views:
    209
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page