Thread: Simple Calculator

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Simple Calculator

    this is a calculator which adds, subtracts, divides, multiplies, calculates areas and perimeters of a circle. although all appears fine to me, whenever i choose to add subtract or whatever, this is what happens. Example:

    input:
    2 + 3

    output:
    1972340 + 2348912 = blah blah...

    aditionally, the management of pi number doesn't seem to work. It all stops when you click option 5 and 6, while it is supposed to ask you for a value to pi or to use the default one.

    Code:
    /*
    //  SimpleCal
    //
    //  Created by Mario Nascimento on 2/26/12.
    //  <dimCode>
    */
    
    
    #include <stdio.h>
    #include "linech.h"
    #include <stdlib.h>
    double pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491;
    
    
    int sum();
    int subtract();
    int mult();
    int divi();
    int circa();
    int circac();
    int circp();
    int circpc();
    void exitt(void);
    int main()
    {
    	putchar('\n');
    	line(25,'*');
    	puts("\nWELCOME TO MARIOTUTORIALS\n");
    	line(25,'*');
    	int i = 0;
    	do
    	{
    		printf("\n\t[1.] \tSum");
    		printf("\n\t[2.] \tSubtraction");
    		printf("\n\t[3.] \tMultiplication");
    		printf("\n\t[4.] \tDivision");
    		printf("\n\t[5.] \tArea od Circle");
    		printf("\n\t[6.] \tPerimeter of Circle");
    		printf("\n\t[7.] \tEXIT");
    		printf("\nChoose a number:\n $ ");
    		scanf(" %d", &i);
    		fflush(stdin);
    		switch(i)
    		{
    			case 1 : sum(); break;
    			case 2 : subtract(); break;
    			case 3 : divi(); break;
    			case 4 : mult(); break;
    			case 5 : circa(); break;;
    			case 6 : circp(); break;
    			case 7 : exitt();
    		}
    		getchar();
    	}
    	while(i != 3);
    	getchar();
    }
    int sum()
    {
        
    	double n1 = 0.0;
    	double n2 = 0.0;
    	printf("Insert two numbers (separated by + sign): ");
    	scanf("%d + %d", &n1, &n2);
    	
    	printf("%d + %d = %d", n1, n2, (n1+n2));
    	getchar();
        printf("\nPress Enter to Return to Main Menu");
        getchar();
    	main(system("clear"));
        
    }
    int subtract()
    {
        
    	double n1 = 0.0;
    	double n2 = 0.0;
    	printf("Insert two numbers (separated by - sign): ");
    	scanf("%d - %d", &n1, &n2);
    	
    	printf("%d - %d = %d", n1, n2, (n1-n2));
    	getchar();
        printf("\nPress Enter to Return to Main Menu");
        getchar();
    	main(system("clear"));
    }
    
    
    int mult()
    {
    	double n1 = 0.0;
    	double n2 = 0.0;
    	printf("Insert two numbers (separated by * sign): ");
    	scanf("%d * %d", &n1, &n2);
    	
    	printf("%d * %d = %d", n1, n2, (n1*n2));
    	getchar();
        printf("\nPress Enter to Return to Main Menu");
        getchar();
    	main(system("clear"));
    }
    
    
    int divi()
    {
    	double n1 = 0.0;
    	double n2 = 0.0;
    	printf("Insert two numbers (separated by / sign): ");
    	scanf("%d / %d", &n1, &n2);
    	
    	printf("%d / %d = %d", n1, n2, (n1/n2));
    	getchar();
        printf("\nPress Enter to Return to Main Menu");
        getchar();
    	main(system("clear"));
    }
    
    
    int circa()
    {
    	double j = 0.0;
    	printf("Enter a value for pi (enter 1 for default 500-digit) $ ");
    	scanf("%d",&j);
    	if(j==1)
    	{
    		circac();
    	}
    	else
    		pi == j;
    		circac();
    }
    circac()
    {
    	double n1 = 0.0;
    	printf("Insert radius of circle: ");
    	scanf("%d", &n1);
    	printf("Area of Circle = %d",(pi * n1 * n1) );
    	getchar();
        printf("\nPress Enter to Return to Main Menu");
        getchar();
    	main(system("clear"));
    }
    
    
    int circp()
    {
    	double k = 0.0;
    	printf("Enter a value for pi (enter 1 for default 500-digit) $ ");
    	scanf("ß%d",&k);
    	if(k==1)
    	{
    		circpc();
    	}
    	else
    		pi == k;
    		circpc();
    }
    
    
    circpc()
    {
    	double n1 = 0.0;
    	printf("Insert radius of circle: ");
    	scanf("%d", &n1);
    	printf("Perimeter of Circle = %d",(pi * n1 * 2));
    	getchar();
        printf("\nPress Enter to Return to Main Menu");
        getchar();
    	main(system("clear"));
    }
    
    
    void exitt(void)
    {
    	printf("\nPress [Enter] to exit");
    }

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Sorry here it is!

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A bit hard to tell, as you haven't shown any code. The obvious explanation would be some uninitialised variables. A common mistake is assuming that uninitialised variables start with the value zero: except in special cases, the value held by uninitialised variables is indeterminate.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    I have now put the code. I initialized them. As they are doubles I initializes as: double n1 = 0.0;

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    %d is the wrong format specifier for reading or printing floats.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    oh my god how stupid.... Thank you. I've corrected and used %f but now when I input, say 5 + 1 the result is: 0.0000 + 0.0000 = 0.0000

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The scanf() format specifier for double is not "%f" it is "%lf" that is a lower case L not a 1.

    Jim

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Sadly, doubles don't have 500 decimal digits of precision. They only have about 15 or so.

    This is insane
    Code:
    main(system("clear"));
    In general you don't call main recursively. And what's wrong with return? And why are you passing the return value of the system call? (And, assuming windows, isn't the command cls?)

    Where did you learn to program like this?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Thank you, I had no idea actually I thought it was the same as float. It works now. Everything except the area and perimeter of circle. I can't seem to find any error in the code. And by the way, is there anyway like in pascal to limit the amount of decimal points it prints out? Thank you.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    I'm on a mac (: the library works fine for macs just need to change cls to clear. I did that so that when it returns to main it clears the screen. thanks about the 500-digit (: what do you suggest instead of main(system("clear")); ?

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Change all of your function return types (except main) to void. Then at the end of each just do:
    Code:
    system("clear");
    return;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    that looks simpler, thank you. How about I make a void function lets call it clsret() the has both those command in it and then I just call it every time I need a return for my function.

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That won't work because the return will just return from THAT function. You'd still need a return in the original function.

    But you could put this code that's repeated in each of your functions after your switch statement in main
    Code:
        getchar();
        printf("\nPress Enter to Return to Main Menu");
        getchar();
        system("clear");
    And note that your while condition in main should be i != 7 (not 3).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    yeah you are indeed right. It's because before there were only add, subtract and exit. Although the effect is the same! I added more and forgot Thankx (:
    Last edited by mmario10; 04-05-2012 at 10:32 PM.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Quote Originally Posted by oogabooga View Post
    That won't work because the return will just return from THAT function. You'd still need a return in the original function.

    But you could put this code that's repeated in each of your functions after your switch statement in main
    Code:
        getchar();
        printf("\nPress Enter to Return to Main Menu");
        getchar();
        system("clear");
    And note that your while condition in main should be i != 7 (not 3).

    I still have a problem though. two actually. This one is solved but the other problem is that when you choose option 5 or 6 it will ask you for a value of pi. Now the end result is correct but whether you enter 10.2 as pi or 1.05 the result is the same. It is using the first value attributed to pi in the beginning. The last problem is that I wanted to sometimes make the printf() function only print numbers for example with 2 decimal points instead of 5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Calculator
    By Loke302 in forum C++ Programming
    Replies: 8
    Last Post: 12-02-2011, 04:46 AM
  2. simple calculator
    By symcy in forum C Programming
    Replies: 2
    Last Post: 02-05-2011, 12:04 PM
  3. simple calculator
    By symcy in forum C Programming
    Replies: 0
    Last Post: 02-05-2011, 11:21 AM
  4. very simple calculator
    By N_D_E in forum C Programming
    Replies: 4
    Last Post: 03-22-2010, 07:18 PM
  5. A simple calculator in C
    By AlphaMac in forum C Programming
    Replies: 11
    Last Post: 10-14-2006, 01:26 AM