Thread: C - Help with making a simple calculator.

  1. #16
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Quote Originally Posted by Matticus View Post
    Actually tabstop, your last suggestion there would seem to match the criteria of the program. Based on the "menu" string in the original post, the "modulus" option is expecting integers - so there shouldn't be any fractional parts if proper input specs are heeded. Casting the inputs to int would be an alternative, as well.
    Oh my.. you are right.. Can't believe I didn't realize this. Thank you!


    Quick Question... pretty much anything I input into the modulus function, it returns a 0. Is that normal?

  2. #17
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome.

    Quick Question... pretty much anything I input into the modulus function, it returns a 0. Is that normal?
    Not unless you keep passing values where one is a multiple of the other. Can you post the relevent code?

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Nurein Ahmed View Post


    Quick Question... pretty much anything I input into the modulus function, it returns a 0. Is that normal?
    Only if the first number is a multiple of the second.

  4. #19
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Quote Originally Posted by Matticus View Post
    You're welcome.



    Not unless you keep passing values where one is a multiple of the other. Can you post the relevent code?

    Code:
    
    #include<stdio.h>
    
    
    int main()
    {
    	int exit=0, iResponse=0, modnum=0, modnum2=0;
    	float firstnum=0, secondnum=0, ans=0;
    	printf("\nCalculator Menu: \n\n(1) Addition\n(2) Subraction\n(3) Multiplication\n(4) Division\n(5) Modulus (integers only)\n(6) Test if Prime (integers only)\n(7) Exit\n");
    	while(!exit)
    		{
    			printf("Please Choose an operation = ");
    			scanf("%d", &iResponse);
    				switch(iResponse)
    				{
    					case 1: /*Addition*/
    						printf("\nPlease Enter the First Number = ");
    						scanf("%f", &firstnum);
    						printf("Please Enter the Second Number = ");
    						scanf("%f", &secondnum);
    						ans = firstnum + secondnum;
    						printf("%.1f + %.1f = %.1f\n", firstnum, secondnum, ans);
    						break;					
    					case 2: /*Subtraction*/
    						printf("\nPlease Enter the First Number = ");
    						scanf("%f", &firstnum);
    						printf("Please Enter the Second Number = ");
    						scanf("%f", &secondnum);
    						ans = firstnum - secondnum;
    						printf("%.1f - %.1f = %.1f\n", firstnum, secondnum, ans);
    						break;
    					case 3: /*Multiplication*/
    						printf("\nPlease Enter the First Number = ");
    						scanf("%f", &firstnum);
    						printf("Please Enter the Second Number = ");
    						scanf("%f", &secondnum);
    						ans = firstnum*secondnum;
    						printf("%.2f*%.2f = %.2f\n", firstnum, secondnum, ans);
    						break;
    					case 4: /*Division*/
    						printf("\nPlease Enter the First Number = ");
    						scanf("%f", &firstnum);
    						printf("Please Enter the Second Number = ");
    						scanf("%f", &secondnum);
    						ans = firstnum/secondnum;
    						printf("%.2f/%.2f = %.2f\n", firstnum, secondnum, ans);
    						break;
    					case 5: /*Modulus*/
                                                    printf("Enter the First Number= ");
                                                    scanf("%d", &modnum);
                                                    printf("Enter the Second Number= ");
                                                    scanf("%d", &modnum2);
                                                    ans = modnum % modnum2;
                                                    printf("%d %% %d = %d\n", modnum, modnum2, ans);
                                                    break;
    					case 7: /*Exits the program*/
    						printf("\nGoodbye!\n");
    						exit=1;
    						break;
    					default:
    						printf("\n\nInvalid Input--Please choose a valid number from above!\n\n");
    						break;
    				} /*End Switch*/
    		}
    	return 0;
    	system("PAUSE");
    }

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't print a float with %d, and ans is still a float variable. You may want to use %.0f instead. (Your compiler should have told you this via something like
    Code:
    mods.c:53:17: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘double’ [-Wformat]

  6. #21
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Quote Originally Posted by tabstop View Post
    You can't print a float with %d, and ans is still a float variable. You may want to use %.0f instead.
    That helped a lot, took care of everything! Thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making calculator in C
    By HolyTurtle in forum C Programming
    Replies: 17
    Last Post: 12-30-2010, 08:05 PM
  2. Making a simple calculator
    By ChvyVele in forum C++ Programming
    Replies: 12
    Last Post: 05-12-2008, 05:05 PM
  3. Making a calculator (newbie)
    By Swerve in forum C++ Programming
    Replies: 4
    Last Post: 09-06-2007, 07:57 PM
  4. Making a calculator run again!
    By cookie in forum C Programming
    Replies: 15
    Last Post: 06-10-2007, 09:56 AM
  5. Replies: 2
    Last Post: 01-13-2003, 01:28 PM

Tags for this Thread