HELP: C Prog calculator looping!

Discussion in 'Programming' started by Magina™, Jun 6, 2009.

  1. #1
    Hi guys can you help me with my calculator? I want to give loop to my codes using do while loop. Is it possible? and add "Q" for Quit/Exit.

    Here are my codes:

    Can you give me an I dea?
     
    Magina™, Jun 6, 2009 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    Following code compiles fine with Dev C++ (mingw)

    #include <stdio.h>
    #include <conio.h> //for getch
    #include <cstdlib> //for system
    int main()
    {
    	float left, right, answer;
    	float sum,difference,product,qoutient;
    	char oper;
    
    	char ch=0;
    
    	while(1) 
    	{
    		system("cls");
    		printf("Q) Quit / Exit\nAny other key to make calculation\n\n");
    		ch = getch();
    		
    		if(ch=='q' || ch=='Q') 
    			break;
    			
    		printf("\n\n enter new expression: ");
    		scanf("%f %c %f", &left, &oper, &right);
    
    		sum = left + right;
    		difference = left - right;
    		product = left * right;
    		qoutient = left / right;
    
    		if (oper == '+')
    			printf("\n Answer: %f ",sum);
    
    		else if (oper == '-')
    			printf("\n Anser: %f",difference);
    
    		else if (oper == '*')
    			printf("\n Answer: %f",product);
    
    		else if (oper == '/')
    			printf("\n Answer: %f",qoutient);
    
    		else
    			printf("\n Invalid operator!");
    
    		printf("\n\nPress Return to continue...");
    		getch();
    
    	}
    
    	return 0;
    }
    Code (markup):
    regards
     
    Vooler, Jun 9, 2009 IP