3

I am attempting to learn C and I have written a small program which calculates the area for various shapes. Here is the source code:

#include "stdafx.h"

struct entry
{
    float x;
    float y;
};

void square()
{
    printf("\nPlease enter the size of one of the square's sides\n");

    struct entry sq;
    scanf_s("%f\n", &sq.x);

    printf("\nThe area of the square is %.2f\n\n", sq.x * sq.x);
}

void rectangle()
{
    printf("\nPlease enter the length and breadth of the rectangle\n");

    struct entry rec;
    scanf_s("%f\n", &rec.x);
    scanf_s("%f\n", &rec.y);

    printf("\nThe area of the rectangle is %.2f\n\n", rec.x * rec.y);
}

void triangle()
{
    printf("\nPlease enter the width and height of the triangle\n");

    struct entry tri;
    scanf_s("Width: %f\n", &tri.x);
    scanf_s("Height: %f\n", &tri.y);

    printf("\nThe area of the triangle is %.2f\n\n", (tri.x * tri.y)/2);
}

void circle()
{
    printf("\nPlease enter the radius of the circle\n");

    struct entry circ;
    scanf_s("%f\n", &circ.x);

    printf("\nThe area of the circle is %.2f\n\n", 3.14 * circ.x * circ.x);
}


void main()
{
    int input = 0;

    do
    {
        printf("---Area of a Shape---\n\n");
        printf("1. Square\n");
        printf("2. Rectangle\n");
        printf("3. Triangle\n");
        printf("4. Circle\n");
        printf("5. Exit\n");
        printf("\n\n");

        printf("Please make a choice\n");
        scanf_s("%d", &input);

        switch(input)
        {
            case 1: square();
                    break;

            case 2: rectangle();
                    break;

            case 3: triangle();
                    break;

            case 4: circle();
                    break;

            case 5: printf("\n\nThank you for using this program!  Press enter to exit!\n");
                    break;

            default: printf("\nInvalid choice!\n\n");
                    break;
        }       
    }
    while(input != 5);

    getchar();
    getchar();
}

The only problem that I have with the program is that after inputting the size of the sides in order to calculate the area, it waits for me to input another number (to make the choice) before displaying the answer. I don't know where the problem is. I thought that before moving to another function, C makes sure that it executes the whole function at hand. Could you please help me solve this problem? Thanks.

1 Answer 1

7

In your scanf formats,

scanf_s("%f\n", &rec.x);

the '\n' doesn't stand for a literal '\n', but for an arbitrary sequence of whitespace characters. Thus the scanf_s call doesn't end until you input a non-whitespace character after the number (and whitespace) [the input is probably not sent to your program until you type a newline after that]. Just remove the '\n' from the formats.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.