-2
// 2.cpp : Sample Program with a menu
//Write a program that will use a menu 



#include "stdafx.h"
#include<stdio.h>

#define Pi 3.14159

int main(void)
{
int digit1;
int choice;
int a, b, c;    //user input for choice 3 & 4
int a1, a2;// User input for Choice 1
int divi;
int divisor;

/*Menu*/

printf("***** MENU *****\n");
printf("  1 - Greater Than, Less Than, Or Equal?\n");
printf("\t In this selection, you will enter two integers. \n");
printf("\t The program will return'Greater Than' if the first\n"); 
printf("\t integer entered is less than the second,'Less than'\n");
printf("\t if the first integer is greater than the second, or\n");
printf("\t'Equal' if the two integers entered are equal\n");
printf("  2 - Evenly Divisible\n");
printf("\t In this slection, you will enter two integers. \n");
printf("\t The program will test if the the first integer\n"); 
printf("\t is evenly divisible by the second. The program\n");
printf("\t will then return its result and display\n");
printf("\t the quotient rounded to the nearest thousandth\n");

printf("  3 - Calculations with 2 integers\n");
printf("  4 - Calculations with 3 integers\n");
printf("  5 - Calculations with circles\n");
printf("  6 - Quit\n");

printf("Please enter your choice: ");
scanf("%i",&choice);

printf("\n\n");

switch(choice)
{
    case 1:     //Greater than, less than, or equal

    printf("Please Enter two integers: \n");
    scanf("%i %i", &a1, &a2); 
    if (a1<a2)
    printf("Greater Than\n");
    else if (a1>a2)
    printf("Less Than\n");
    else if (a1=a2)
    printf("Equal\n");

    break;

    case 2: //Equally Divisible

I need help with this part of the code. Getting 0.000 for the quotient.Why? What about these cases is making it not receive the integers? I tried to localize the integers with the curly brackets. What am I doing wrong?

    printf("Please Enter two integers: \n");
    {
        int divi;
        int divisor;

        scanf("%i %i", &divi, &divisor);
        float modQuotient = divi%divisor;

        if (modQuotient!=0)
            {
            printf("%i is not evenly divisible by %i\n", divi,     divisor);
            printf("The quotient is %.3f\n", divi/divisor);
            }
        else 
            {
            printf("%i is evenly divisible by %i\n", divi, divisor);
            printf("The quotient is %.3f\n", divi/divisor);
            }


    }



     break;

     /*case 3:      /*Calculations with 2 integers*/


     case 4:    /*Calculations with 3 integers*/
        printf("  You have selected to do some calculations with 2     

integers\n");
        printf("Please enter your 3 integers: ");
        scanf("%i%i%i",&a, &b, &c);

        printf("The average of %i, %i and %i is %g\n\n",a, b, c, (a+b+c)/3.0);
        break;

     case 5:    /*Calculations with circles*/
         float radius;

         printf("Please enter the radius of a circle: ");
         scanf ("%g", &radius);
printf("\nThe diameter of a circle with a radius of %g units is  %.2f units\n\n",       

radius, radius*2);
         printf("The circumference is of the circle with a radius of %g 

units is %.2f units\n\n", radius, Pi*radius*2);
         printf("The area of a circle with a radius of %g units is %.2f 

square units\n\n", radius, Pi*radius*radius);
         break;

     case 6:    /*Quit*/
        printf("Thank you.  Good bye\n\n");
        break;

      default:  /*Invalid Entry*/
        printf("Invalid Entry...\n");
        break;
}

printf("The program will now end.  Have a great day!\n");


return 0;
}
  • http://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c – Retired Ninja Jun 17 '14 at 20:09
  • 1
    Don't use that inaccurate `Pi` constant. Use `M_PI` from `math.h` instead. – HolyBlackCat Jun 17 '14 at 20:15
  • 1
    @HolyBlackCat Confident that C does not specify that `M_PI` _must_ exist in `math.h` or elsewhere. Many versions of C do have a high precision macro there though. Agree OP should use a more precise value. See http://www.piday.org/million/ – chux - Reinstate Monica Jun 18 '14 at 03:34

2 Answers2

3

The modulus of two ints is another int. Also, dividing by zero causes undefined behaviour. So you should write:

if ( divisor == 0 )
    exit(EXIT_FAILURE);     // or some other error handling

int modQuotient = divi % divisor;

and remember to use %i or %d if you are printf'ing modQuotient.

In the line:

printf("The quotient is %.3f\n", divi/divisor);

there is a problem. divi and divisor are both int, therefore the result of binary operation on them is also int. With printf it does not do any conversion of arguments to match the format specifier - you have to manually ensure the match yourself. So this code causes undefined behaviour by using %f to try and print an int.

If you want to do a floating point division then you must make at least one of the operands be a float, e.g. (and separating into two lines for clarity):

float quot = (float)divi / divisor;
printf("The quotient is %.3f\n", quot);

NB. You have a logic error in this code:

else if (a1=a2)
    printf("Equal\n");

The = operator means assignment. This line changes a2 to be equal to a1. The operator to compare for equality is ==.

M.M
  • 138,810
  • 21
  • 208
  • 365
-1
printf("The quotient is %.3f\n", divi/divisor);

In the above code you are telling the program the format the output as a float with precision out to three decimal places. So when the division occurs, if the answer were to be 0, it will instead output as 0.000.

Vee
  • 729
  • 10
  • 27