Thread: how this C working?

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    1

    how this C working?

    i want to have two applications: that is to calculate area of triangle and rectangle. i want to make as the following form:

    Enter 1 to calculate the area of Triangle:
    Enter 2 to calculate the area of Rectangle:
    Enter 3 to Exit:

    then i enter 1 then i will put values to calculate area of triangle, so can show me such things?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So which bit is confusing you?
    - how to read the input
    - how to make a decision on that input
    - how to do the maths

    You've got to show us something first, we're not here to hand out answers on demand (see the forum rules).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    You should be more specific or/and show part of your code concerning the problem. May be you mean a menu to select operation...then I suggest this:


    Code:
    // function declarations goes here
    
    int main()
    {
    int selection=0;
    do
    {
     selection=menu(); //access function menu to get operation
    
    if (selection==1)
    //area of square
    else
    if(selection==2)
    //area of rectangle
    
    //note that it is better if your create another function
    //to tackle the area operation.
    .
    .
    .
    
    }while( selection!=3 );
     
    return 0;
    }
    
    
    int menu()
    {
     int choice=0;
     do
         {
          printf("\n\nMENU\n_______\n\n");
          printf("1.Area of square\n2.Area of rectangle\n3.Exit");
          printf("\n\nEnter selection number: ");
    
          scanf("%d",&choice);
          
             if ((choice==1)||(choice==2)||choice==3)
               return choice;
          
             else
             {
               printf("\n\nInvalid Request!\n");
               choice=0;
             }
         }
         while(choice==0);
    }
    Last edited by Sober; 05-27-2007 at 12:28 AM.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Or use a switch statement instead of else if's but that is style issue really
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM