Thread: Help with loops in functions?

  1. #1
    Registered User
    Join Date
    Aug 2015
    Location
    Manchester
    Posts
    2

    Question Help with loops in functions?

    Hi all, i'm new to programming. This is a code to calculate the areas of different shapes. The user should choose the shape, pick whether they want the measurement in cm or m, then be asked how many shapes they are calculating the area of.

    I am wondering whether I can make this code with with the for loops in the functions, or will I have to move them back up to the main body.
    I have also tried making the return types void.

    SQUARE: Will ask how many shapes you want to calculate the area of but will only calculate the area for one regardless
    CIRCLE/TRIANGLE: Will ask if you want the answer in cm or m but will then loop back to the main menu.

    Sorry this is such a long code!

    Code:
    double squareCM (double a,double b);
    double squareM (double a, double b);
    double circleCM (double a);
    double circleM (double a);
    double triangleCM (double a,double b);
    double triangleM (double a, double b);
    
    
    double a,b;
    main()
    {
        char ch,pa;
    
        while(ch!='0')
        {
            system("cls");
            printf("\n 1 - Rectangle");
            printf("\n 2 - Circle");
            printf("\n 3 - Triangle");
            printf("\n 0 - Exit");
            printf("\nPlease enter your choice");
    
    
            ch=getche();
            system("cls");
    
    
            if(ch=='1')
            {
                printf("\n 1 - Answer in CM");
                printf("\n 2 - Answer in M");
                printf("\n 0 - Exit");
                printf("\n Please enter your choice");
    
    
                pa=getche();
                system("cls");
    
                if(pa=='1')
                {
                    squareCM(a,b); 
                }
                else if(pa=='2')
                {
                    squareM(a,b);
                }
            }
            
                else if(ch=='2')
                {
                printf("\n 1 - Answer in CM");
                printf("\n 2 - Answer in M");
                printf("\n 0 - Exit");
                printf("\n Please enter your choice");
    
    
                pa=getche();
                system("cls");
    
    
                if(pa=='1')
                {
                    circleCM(a); 
                }
                else if(pa=='2')
                {
                    circleM(a);
                }
            }
    
    
            else if(ch=='3')
            {
                printf("\n 1 - Answer in CM");
                printf("\n 2 - Answer in M");
                printf("\n 0 - Exit");
                printf("\n Please enter your choice");
    
    
                pa=getche();
                system("cls");
    
    
                if(pa=='1')
                {
                    triangleCM(a,b);
                }
                else if(pa=='2')
                {
                    triangleM(a,b);
                }
            }
    
    
        }
    
    
    
    
    }
    double squareCM(double a,double b)
    {
        int counter,d;
    
    
        printf("Please enter the amount of shapes you are calculating the area of\n");
        scanf("%d",&counter);
    
    
        for(d=1; d<=counter; d++)
        {
            printf("Please enter length of rectangle\n");
            scanf("%lf",&a);
            printf("Please enter width of rectangle\n");
            scanf("%lf",&b);
            double num;
            num=(a*b);
            return num;
    
    
        }
    
    
    }
    
    
    double squareM (double a,double b)
    {
        double num;
        num=((a*b)/100);
        return num;
    
    
        int counter,d;
    
    
        printf("Please enter the amount of shapes you are calculating the area of\n");
        scanf("%d",&counter);
    
    
        for(d=1; d<=counter; d++)
        {
            printf("Please enter length of rectangle\n");
            scanf("%lf",&a);
            printf("Please enter width of rectangle\n");
            scanf("%lf",&b);
    
    
            printf("%lf\n",squareM(a,b));
            getch();
        }
    }
    double circleCM (double a)
    {
        double num;
        double pi;
        num=(pi*a*a);
        return num;
    
    
        int counter,d;
        double area;
    
    
        printf("Please enter the amount of shapes you are calculating the area of\n");
        scanf("%d\n",&counter);
    
    
        for(d=1; d<=counter; d++)
        {
            printf("Please enter the radius of the circle\n");
            scanf("%lf",a);
            area = pi*a*a;
            printf("The area of the circle is: %lf\n", circleCM(a));
            getch();
        }
    }
    double circleM (double a)
    {
        double num;
        double pi;
        num=((pi*a*a)/100);
        return num;
    
    
        int counter,d;
        double area;
    
    
        printf("Please enter the amount of shapes you are calculating the area of\n");
        scanf("%d\n",&counter);
    
    
        for(d=1; d<=counter; d++)
        {
            printf("Please enter the radius of the circle\n");
            scanf("%lf",a);
            area = ((pi*a*a)/100);
            printf("The area of the circle is: %lf\n", circleM(a));
            getch();
        }
    }
    double triangleCM (double a, double b)
    {
        double num;
        num=((a*b)*0.5);
        return num;
    
    
        int counter,d;
        double area;
    
    
        printf("Please enter the amount of shapes you are calculating the area of\n");
        scanf("%d\n",&counter);
    
    
        for(d=1; d<=counter; d++)
        {
            printf("Please enter the base of your triangle\n");
            scanf("%lf\n",&a);
            printf("Please enter the height of your triangle\n");
            scanf("%lf\n", &b);
            area=(a*b)*0.5;
    
    
            printf("The area of the triangle is: %lf\n",triangleCM(a,b));
            getch();
        }
    }
    double triangleM (double a, double b)
    {
        double num;
        num=(((a*b)*0.5)/100);
        return num;
    
    
        int counter, d;
        double area;
    
    
        printf("Please enter the amount of shapes you are calculating the area of\n");
        scanf("%d\n",&counter);
    
    
        for(d=1; d<=counter; d++)
        {
            printf("Please enter the base of your triangle\n");
            scanf("%lf",&a);
            printf("Please enter the height of your triangle\n");
            scanf("%lf", &b);
            area=(((a*b)*0.5)/100);
    
    
            printf("The area of the triangle is: %lf\n",triangleM(a,b));
            getch();
    
    
        }
    }

    Thanks,
    Katie

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,416
    There are a number of problems with your code, some of which I am listing here:
    • Where are your header inclusions? You use system, so you should #include <stdlib.h>. You use printf, so you should #include <stdio.h>. You also use the non-standard getche, so you should #include <conio.h>, which is a non-standard header, or use some other method of reading user input that uses the standard library instead (e.g., getchar, scanf, fgets with sscanf).
    • You declared two global variables:
      Code:
      double a,b;
      Generally, global variables should be avoided as they tend to make it more difficult for you to reason about your code. As we shall see later, you do not need these global variables at all.
    • This:
      Code:
      main()
      should be:
      Code:
      int main(void)
    • You declared ch and then compared it to '0' before giving it an initial value.
    • Looking at your squareCM function, you do not actually use the parameters a and b as parameters. Rather, you use them as you would any other ordinary local variables, i.e., to store user input within the function. Therefore, you do not need them as parameters, and hence you do not need the global variables to pass as arguments.
    • In various places in your code, you called scanf without checking its return value. This can be problematic because an invalid input might be entered by the user.
    • Note that unless you are using the loop index and hence it would be more convenient otherwise, it is conventional to loop starting from 0:
      Code:
      for (d = 0; d < counter; d++)
    • Notice that the loop in squareCM only loops at most once because you return unconditionally from within the loop body.
    • squareM has similiar problemas squareCM, except that this time you returned a value so early that the rest of the function is unreachable.
    • Why CM and M? You should name your functions with descriptive names, or if that is truly impractical, comment.
    • In circleCM, you use pi without having given it a value. Just naming a variable pi is insufficient to make it π
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2015
    Location
    Manchester
    Posts
    2
    Hi laserlight, thanks for your fast reply.

    Code:
     double a ,b;
    I declared this as a global variable because I received an error stating that I had too few arguments and that 'a' and 'b' were undeclared. Which it is still doing now, i'm not sure how to resolve this issue.

    Sorry if this is a silly question:
    - I am taking the ch value from the user with scanf so I thought you don't need to initialize it?

    Katie
    Last edited by KatieS; 08-18-2015 at 04:55 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,416
    Quote Originally Posted by KatieS
    I declared this as a global variable because I received an error stating that I had two few arguments and that 'a' and 'b' were undeclared. Which it is still doing now, i'm not sure how to resolve this issue.
    Parameters are properly either used to receive input from the caller (i.e., the value of the parameter is used in the function), or used to send output to the caller (i.e., instead of using the return value, a pointer is passed such that the function can modify what the pointer points to, and hence the caller can access what the pointer points to), or both. In your case, you are not using the parameters as you should, so the solution is to have no parameters: just declare local variables named a and b within the various functions.

    Quote Originally Posted by KatieS
    I am taking the ch value from the user with scanf so I thought you don't need to initialize it?
    Look carefully:
    Code:
    char ch,pa;
    
    while(ch!='0')
    What is the value of ch at this point, before control has entered the loop body for the first time?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >I declared this as a global variable because I received an error stating that I had too few arguments and that 'a' and 'b' were undeclared. Which it is still doing now, i'm not sure how to resolve this issue.

    Could you post the error message you get.
    As a matter of fact there are a lot of things which need to be fixed in you code to get it working. I suggest that you break down the problem into parts and work on to get atleast one thing working and then you can start building on top of it.
    To start with why don't you get then function squareCM working?

    And to quickly point out squareCM takes two parameters and it gets called in main. The arguments you send from main are a,b. Which are not defined in the main scope. Why dont you declare those two variables in main and read values for those variables from user usinf scanf and pass it onto squareCM to calculate? Does that makes sense?

    ~H
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is happening in the == with functions and loops?
    By PCBeginner in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2012, 01:32 PM
  2. C functions using loops
    By Noobpoint in forum C Programming
    Replies: 19
    Last Post: 02-13-2012, 10:31 PM
  3. Loops or recursive functions?
    By mariano_donati in forum C Programming
    Replies: 5
    Last Post: 05-12-2008, 12:41 PM
  4. Loops in functions?
    By AmbliKai in forum C Programming
    Replies: 16
    Last Post: 11-29-2007, 06:33 AM
  5. new to Void functions and loops
    By Loraine13 in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 10:00 AM

Tags for this Thread