Thread: C - Help with making a simple calculator.

  1. #1
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14

    Question C - Help with making a simple calculator.

    I keep getting an error message when and I don't know why or what I'm doing wrong. Any help would be appreciated. Thanks.
    Not sure if this is the proper way to enter codes, I'm near here so bear with me.

    Ahmed_s.c
    Code:
    // Third Program: Simple Calculator
    // Author: Nurein Ahmed
    // Date: 01/30/2014
    // Section1
    
    
    #include<stdio.h>
    
    
    int main()
    {
    	int exit=0, iResponse=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("%f", &firstnum);
                            printf("Enter the Second Number= ");
                            scanf("%f", &secondnum);
                            ans = firstnum % secondnum;
                            printf("%.2f %% %.2f = %d.2f\n", firstnum, secondnum, 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");
    						break;
    				} /*End Switch*/
    		}
    	return 0;
    	system("PAUSE");
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As you might have noticed because it turned pink, exit is already a thing in C, so you should pick a different name. You didn't state what (other) errors you were getting, if any, so if you have errors you should tell us what they are.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Quote Originally Posted by tabstop View Post
    As you might have noticed because it turned pink, exit is already a thing in C, so you should pick a different name. You didn't state what (other) errors you were getting, if any, so if you have errors you should tell us what they are.
    I'm getting an error with the module section. More specifically, the line that says "ans = firstnum % secondnum;"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    % is only for integers; if you want modulus for floating point numbers, you need to use fmod.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Quote Originally Posted by tabstop View Post
    % is only for integers; if you want modulus for floating point numbers, you need to use fmod.
    Alright, where would I put fmod. How would it generally look?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Nurein Ahmed View Post
    Alright, where would I put fmod. How would it generally look?
    Generally when using a new function you should visit the documentation or for standard functions like fmod the manpages are a good resource

    man page fmod section 3

  7. #7
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Quote Originally Posted by c99tutorial View Post
    Generally when using a new function you should visit the documentation or for standard functions like fmod the manpages are a good resource

    man page fmod section 3
    The fmod function doesn't seem to do anything.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Nurein Ahmed View Post
    The fmod function doesn't seem to do anything.
    If it returns a value that you can print, it must have done something. What answers are you getting that you weren't expecting?

  9. #9
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Quote Originally Posted by tabstop View Post
    If it returns a value that you can print, it must have done something. What answers are you getting that you weren't expecting?
    I'm getting this without the fmod C - Help with making a simple calculator.-mod-jpg

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It is true that if you never type the word "fmod", fmod won't work. I strongly suggest that you do in fact type the word fmod (follow the link above for usage, or look in your textbook).

    Contrariwise, if the assignment requires the use of %, then the choice of float was incorrect and you must use ints instead.

  11. #11
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Contrariwise, if the assignment requires the use of %, then the choice of float was incorrect and you must use ints instead.
    I wanted to use ints but when multiplying numbers with decimals, the decimals don't show, which are within the assignments parameters.
    Last edited by Nurein Ahmed; 01-29-2014 at 12:19 PM. Reason: spelling

  12. #12
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Also, I don't think our professor would want us to use fmods yet, we haven't covered those yet

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Nurein Ahmed View Post
    Also, I don't think our professor would want us to use fmods yet, we haven't covered those yet
    Then you're sunk. You can use ints and %, or floats and fmod, but not floats and %.

    (As a last ditch thing, you can, once you are in the case of having to find modulus, to quickly create two ints, as in
    Code:
    int i1 = first_number;
    and then use % on those ints. You will lose any fractional part, though.)

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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.

  15. #15
    Registered User
    Join Date
    Jan 2014
    Location
    Kentucky
    Posts
    14
    Ah shoot, Thank you anyways! I appreciate the help!

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