switch or if else....how to check for error

This is a discussion on switch or if else....how to check for error within the C Programming forums, part of the General Programming Boards category; for the following program is it better to use if else statment or a switch statment? and how should i ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    4

    Question switch or if else....how to check for error

    for the following program is it better to use if else statment or a switch statment?
    and how should i print out the error statment if one of the input values in the files have invalid numbers (ie. -2)?

    sample input file

    Rectangle 5 7.8
    Square 6.8
    Circle 5.8
    Rectangle 5.3 3
    Trepazoid 2 6 4
    Rectangle 10 20
    Rectangle 131 40
    Circle -4

    please help give some hints to help me and thank you.

    clear

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define CIRCLE "Circle"
    #define SQUARE "Square"
    #define RECTANGLE "Rectangle"
    #define TRIANGLE "Triangle"
    
    void main()
    {
    const double PI = 3.15;
    char shapeName[100];
    float x, y, h, ls, rs;
    
    FILE *inFile = fopen("input1.txt", "r");
    FILE *outFile = fopen("output1.txt", "w");
    
    
    
    
    
    while (fscanf(inFile, "%s", shapeName) != EOF)
    {
    if ( 0 == strcmp(shapeName, SQUARE) )
    {
    fscanf(inFile, "%f", &x);
    printf("%s: Side=%6.2f Perimeter=%6.2f Area=%6.2f\n"
    , shapeName, x ,x * 4, x*x );
    fprintf(outFile,
    "%s: Side=%6.2f Perimeter=%6.2f Area=%6.2f\n"
    , shapeName, x ,x * 4, x*x );
    
    }
    else if ( 0 == strcmp(shapeName, RECTANGLE) )
    {
    fscanf(inFile, "%f%f", &x, &y);
    printf ("%s: Width=%6.2f Height=%6.2f Perimeter=%6.2f Area=%6.2f\n"
    , shapeName, x , y, (x+y)*2, x*y );
    fprintf (outFile,
    "%s: Width=%6.2f Height=%6.2f Perimeter=%6.2f Area=%6.2f\n"
    , shapeName, x , y, (x+y)*2, x*y );
    }
    else if ( 0 == strcmp(shapeName, CIRCLE))
    {
    fscanf(inFile, "%f", &x);
    printf("%s: radius=%6.2f circumference=%6.2f Area=%6.2f\n"
    , shapeName, x ,2 * PI * x, 2 * PI * x * x );
    fprintf(outFile, "%s: radius=%6.2f circumference=%6.2f Area=%6.2f\n"
    , shapeName, x ,2 * PI * x, 2 * PI * x * x );
    
    }
    else if ( 0 == strcmp(shapeName, TRIANGLE))
    {
    fscanf(inFile, "%f %f %f %f", &x, &h, &ls, &rs);
    printf("%s: perimeter=%6.2f area=%6.2f\n"
    , shapeName, x + ls + rs, (x*h)/2 );
    fprintf(outFile, "%s: perimeter=%6.2f area=%6.2f\n"
    , shapeName, x + ls + rs, (x*h)/2 );
    }
    
    else
    {
    
    printf("%s", shapeName);
    fgets(shapeName, 99, inFile);
    printf("%s\n", shapeName);
    }
    
    
    }
    fclose(inFile);
    fclose(outFile);
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    4

    Talking

    thanks salem for your pointers...
    clear..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. switch() inside while()
    By Bleech in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 02:34 AM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 09:29 PM
  4. Check application visibility
    By 3saul in forum Linux Programming
    Replies: 2
    Last Post: 02-13-2006, 04:13 PM
  5. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21