Thread: c calculator

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool c calculator

    here's a simple c calculator:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    
    int main()
    {
    	double num1, num2, result;
    	char operator;
    
    	clrscr();
    
    	printf("Enter two numbers and an operator in this form: \n");
    	printf("<number1> <operator> <number2> (e.g. 7 + 4) \n");
    
    	scanf("%lf %c %lf", &num1, &operator, &num2);
    
    	if (operator == '+') {
    	result = num1 + num2;
    	printf("%g %c %g = %g\n", num1, operator, num2, result);
    	}
    	else if (operator == '-' ) {
    	result = num1 - num2;
    	printf("%g %c %g = %g\n", num1, operator, num2, result);
    	}
    	else if (operator == '*' ) {
    	result = num1 * num2;
    	printf("%g %c %g = %g\n", num1, operator, num2, result);
    	}
    	else if (operator == '/' ) {
    	result = num1 / num2;
    	if (num1||num2 == 0) printf("Cannot divide with zero!");
    	printf("%g %c %g = %g\n", num1, operator, num2, result);
    	} else
    	printf("Invalid operator used.");
    
    	return 0;
    }
    ...but i have some problems with it.. can any1 please help me?

    ...i wanna use the switch() statement instead of using if's and else if's, but i don't know much about it...

    ...also, i want this program to run in an infinite loop (...while(1) or for(;...), until the user presses a hot-key.. like Ctrl-Q or Alt-X... but, sadly, i dnt also know how... srry..

    ..thanks in advance for your help... i'll appreciate them for sure... hehehe... -_+

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    ...i wanna use the switch() statement instead of using if's and else if's, but i don't know much about it...
    There is a C tutorial available on switch case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    
    int main()
    {
    	double num1, num2, result;
    	char operator;
    
    	clrscr();
    
    	printf("Enter two numbers and an operator in this form: \n");
    	printf("<number1> <operator> <number2> (e.g. 7 + 4) \n");
    
    	while((scanf("%lf %c %lf", &num1, &operator, &num2)) != -1)
    {
    	switch(operator)
    	{
        case '+':
        {
    	result = num1 + num2;
    	printf("%g %c %g = %g\n", num1, operator, num2, result);
    	break;
    	}
    	case '-':
        {
    	result = num1 - num2;
    	printf("%g %c %g = %g\n", num1, operator, num2, result);
    	break;
    	}
    	case '*':
        {
    	result = num1 * num2;
    	printf("%g %c %g = %g\n", num1, operator, num2, result);
    	break;
    	}
    	case  '/':
        {
    	result = num1 / num2;
    	if (num1 == 0||num2 == 0) printf("Cannot divide with zero!");
    	printf("%g %c %g = %g\n", num1, operator, num2, result);
    	break;
    	} 
        default:
        {
    	printf("Invalid operator used.");
    	break;
    }
    }
    }
    return 0;
    }
    control + z on windows or control + d on UNIX would stop the loop and end the program.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool

    ..ok,, thank you very much for the help..

    i tried what you've said, and it worked.. thanks..


    ..one more question about this program please.... ^.^

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    
    int main()
    {
        double num1, num2, result;
    	char operator;
    
    	while(1)
    	{
    		clrscr();
    
    		printf("SF7-CaLc by SeViLLa [aka:SF7]");
    		printf("\n\nEnter two numbers and an operator in this form: \n");
    		printf("<number1> <operator> <number2> (e.g. 7 + 4) \n");
    		printf("\n\nEnter here: ");
    
    		scanf("%lf %c %lf", &num1, &operator, &num2);
    
    		switch(operator)
    		{
    			case '+':
    			{
    			result = num1 + num2;
    			printf("\nOutput: %g %c %g = %g\n", num1, operator, num2, result);
    			getch();
    			break;
    			}
    
    			case '-':
    			{
    			result = num1 - num2;
    			printf("\nOutput: %g %c %g = %g\n", num1, operator, num2, result);
    			getch();
    			break;
    			}
    
    			case '*':
    			{
    			result = num1 * num2;
    			printf("\nOutput: %g %c %g = %g\n", num1, operator, num2, result);
    			getch();
    			break;
    			}
    
    			case  '/':
    			{
    			result = num1 / num2;
    			printf("\nOutput: %g %c %g = %g\n", num1, operator, num2, result);
    			getch();
    			break;
    			}
    
    			default:
    			{
    			printf("\nInvalid operator used.");
    			getch();
    			break;
    			}
    
    		}
    	}
    
    	return 0;
    }
    ...i want the program to exit using Alt+X ...is it possible for me to do that? if yes, please tell me how.... if you can post tutorials/help/tips regarding of the use of hot-keys in c, i would really appreciate it... thanks again... ^.^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 12:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM