Thread: help with simple debugging

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    6

    help with simple debugging

    The code compiles fine, but when it is run, the menu will come up twice and it will say invalid keypressed even when nothing is pressed. When it does the computations it always gives the incorrect answer of 0.00..Thanks in advance for any help!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define pi 3.141592
    
    void GetInput (void);
    void GetOption (void);
    void Radius (void);
    void Circumference (void);
    void SurfaceArea (void);
    void Volume (void);
    
    
    int main ()
    {
      GetInput();
      GetOption();
      return 0;
    }
    
    void GetInput (void)
    {
      float diameter;
      printf("\n\nEnter diameter in meters: ");
      scanf("%f",&diameter);
      while ( diameter < 0.0 )
        {
             printf("\nMust be greater than 0.0. Try again\n");
             printf("\nEnter diameter in meters: ");
             scanf("%f",&diameter);
        }
    
    }
    
    
    void GetOption(void)
    {
      char choice;
     start:
      printf("Compute what?:\n");
      printf("\t -> (R)adius\n");
      printf("\t -> (C)ircumerfece\n");
      printf("\t -> (S)urface Area\n");
      printf("\t -> (V)olume\n");
      printf("\t -> (Q)uit\n");
      printf("Your choice:  ");
      scanf("%c",&choice);
      printf("------------\n");
    
      switch(choice)
        {
        case 'r': case 'R':
          Radius();
          GetOption();
        case 'c': case 'C':
          Circumference();
          GetOption();
        case 's': case 'S':
          SurfaceArea();
          GetOption();
        case 'v': case 'V':
          Volume();
          GetOption();
        case 'q': case 'Q':
          exit(EXIT_SUCCESS);
        default:
          printf("Invalid key pressed, Try Again\n");
          goto start;
          }
    }
    
    void Radius(void)
    {
      float diameter,ans;
      ans = ( diameter / 2 );
      printf("Radius is %f meters\n",ans);
    }
    void Circumference(void)
    {
      float diameter,ans;
      ans = 2 * pi * diameter ;
      printf("Circumference is %f meters",ans);
    }
    void SurfaceArea(void)
    {
      float diameter,ans,radius;
      radius = (diameter / 2);
      ans = pi * (radius * radius);
      printf("Surface Area is %f square meters",ans);
    }
    void Volume(void)
    {
      float diameter,ans,ans2;
      ans = pi  * (diameter / 2) * diameter;
      ans2 = ans * ans;
      printf("Volume is %f cubic meters",ans2);
    }

  2. #2
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Re: help with simple debugging

    Originally posted by Wexy
    The code compiles fine, but when it is run, the menu will come up twice and it will say invalid keypressed even when nothing is pressed. When it does the computations it always gives the incorrect answer of 0.00..Thanks in advance for any help!

    Code:
     start:
      printf("Compute what?:\n");
      printf("\t -> (R)adius\n");
      printf("\t -> (C)ircumerfece\n");
      printf("\t -> (S)urface Area\n");
      printf("\t -> (V)olume\n");
      printf("\t -> (Q)uit\n");
      printf("Your choice:  ");
      scanf("%c",&choice);
      printf("------------\n");
    
      switch(choice)
        {
        case 'r': case 'R':
          Radius();
          GetOption();
        case 'c': case 'C':
          Circumference();
          GetOption();
        case 's': case 'S':
          SurfaceArea();
          GetOption();
        case 'v': case 'V':
          Volume();
          GetOption();
        case 'q': case 'Q':
          exit(EXIT_SUCCESS);
        default:
          printf("Invalid key pressed, Try Again\n");
          goto start;  // NEVER REALLY A GOOD IDEA, ERROR!!
          }
    }
    For pne you used GOTO which is the cause for your double output menu. simply because the choices you make are invalid and it jumps back to start again. Figure out another way w/o using goto().
    later,
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    in your function getinput() where the scanf is make this modification
    Code:
    scanf("\n%c",&choice);
    Okay your are always getting 0.00 that because you arnt passing daimeter to any of the functions..

    Modify your program so that functions raduis() and so on... take a float varaible daimeter ....
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  4. #4
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    i think it should work and yes please GOTO statements arnt ment for newbies .....only use them in huge programmes ..There is always a structured way to do it .....C is a procedural programming lang keep it that way ....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define pi 3.141592
    
    void GetInput ( float *);
    void GetOption (float );
    void Radius (float );
    void Circumference (float);
    void SurfaceArea (float);
    void Volume (float);
    
    
    int main ()
    {
     float diameter;
    
      GetInput(&diameter);
      GetOption(diameter);
    
      system("pause");
      return 0;
    }
    
    void GetInput (float *diameter)
    {
      printf("\n\nEnter diameter in meters: ");
      scanf("%f",diameter);
      while ( *diameter <= 0.0 )
        {
             printf("\nMust be greater than 0.0. Try again\n");
             printf("\nEnter diameter in meters: ");
             scanf("%f",&diameter);
        }
    
    }
    
    
    void GetOption(float diameter)
    {
      char choice;
     start:
      printf("Compute what?:\n");
      printf("\t -> (R)adius\n");
      printf("\t -> (C)ircumerfece\n");
      printf("\t -> (S)urface Area\n");
      printf("\t -> (V)olume\n");
      printf("\t -> (Q)uit\n");
      printf("Your choice:  ");
      scanf("\n%c",&choice);
      printf("------------\n");
    
      switch(choice)
        {
        case 'r': case 'R':
          Radius(diameter);
          GetOption(diameter);
        case 'c': case 'C':
          Circumference(diameter);
          GetOption(diameter);
        case 's': case 'S':
          SurfaceArea(diameter);
          GetOption(diameter);
        case 'v': case 'V':
          Volume(diameter);
          GetOption(diameter);
        case 'q': case 'Q':
          exit(EXIT_SUCCESS);
        default:
          printf("Invalid key pressed, Try Again\n");
          goto start;
          }
    }
    
    void Radius(float diameter)
    {
      float ans;
      ans = ( diameter / 2 );
      printf("Radius is %f meters\n",ans);
    }
    void Circumference(float diameter)
    {
      float ans;
      ans = 2 * pi * diameter ;
      printf("Circumference is %f meters",ans);
    }
    void SurfaceArea(float diameter)
    {
      float ans,radius;
      radius = (diameter / 2);
      ans = pi * (radius * radius);
      printf("Surface Area is %f square meters",ans);
    }
    void Volume(float diameter)
    {
      float ans,ans2;
      ans = pi  * (diameter / 2) * diameter;
      ans2 = ans * ans;
      printf("Volume is %f cubic meters",ans2);
    }
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  3. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM