Thread: Basic calculator

  1. #1
    Registered User Khaled's Avatar
    Join Date
    Apr 2011
    Location
    Lebanon, Beirut
    Posts
    2

    Basic calculator

    hello everyone i am new to programming, learned it alone from the internet so i am still weak in C++ and in programming in general. i am working on a small very primitive code just for fun and exersice: a calculator.

    i already done the code :

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include "conio.h"
    #include <iomanip>
    #include <math.h>
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    {
    	while(1){
    
    		cout<<"Enter A to perform an addition\n";
    		cout<<"Enter B to perform an substraction\n";
    		cout<<"Enter C to perform a multiplication\n";
    		cout<<"Enter D to perform a division\n";
    		cout<<"Enter E to recover the square root\n";
    		cout<<"Enter F to recover a power\n";
    		cout<<"Enter G to recover the inverse\n";
    		cout<<"Enter H to exit the application\n";
    
    		char x,z;
    		cin>>z;
    		switch(z){
    
    			case 'A':{
    				
    				system("CLS");
    
    				cout<<"Enter first value a followed by the second value b to return a+b"<<endl;
    
    				int a;
    				int b;
    
    				cin>>a>>b;
    
    				cout<<"The sum of the 2 integers is "<<a+b<<endl;
    
    					 system("Pause");
    					 
    					 break;}
    
    			case 'B':{
    				
    				system("CLS");
    
    				cout<<"Enter first value a followed by the second value b to return a-b"<<endl;
    
    				int a;
    				int b;
    
    				cin>>a>>b;
    
    				cout<<"The difference of the 2 integers is "<<a-b<<endl;
    					 
    					system("Pause");
    					
    					break;}
    
    			case 'C':{
    				
    				system("CLS");
    
    				cout<<"Enter first value a followed by the second value b to return a*b"<<endl;
    
    				int a;
    				int b;
    
    				cin>>a>>b;
    
    				cout<<"The product of the 2 integers is "<<a*b<<endl;
    					 
    					system("Pause");
    					
    					break;}
    
    			case 'D':{
    				
    				system("CLS");
    
    				cout<<"Enter first value a followed by the second value b to return a/b"<<endl;
    
    				int a;
    				int b;
    
    				cin>>a>>b;
    
    				cout<<"The division of the 2 integers is "<<a/b<<endl;
    					 
    					system("Pause");
    					
    					break;}
    
    			case 'E':{
    				
    				system("CLS");
    
    				cout<<"Enter the value to receive its square-root"<<endl;
    
    				double a;
    			
    				cin>>a;
    
    				cout<<"The square-root of the given value is "<<sqrt(a)<<endl;
    					 
    					system("Pause");
    					
    					break;}
    
    			case 'F':{
    				
    				system("CLS");
    
    				cout<<"Enter first value a followed by the second value b to return a^b"<<endl;
    
    				double a;
    				int b;
    
    				cin>>a>>b;
    
    				cout<<"The difference of the 2 integers is "<<pow(a,b)<<endl;
    					 
    					system("Pause");
    					
    					break;}
    
    			case 'G':{
    				
    				system("CLS");
    
    				cout<<"Enter the value to receive its inverse"<<endl;
    
    				double a;
    			
    				cin>>a;
    
    				cout<<"The inverse of the given value is "<<1/a<<endl;
    					 
    					system("Pause");
    					
    					break;}
    
    			case 'H':{
    				
    					x=z;
    					
    					break;
    					
    					 }
    
    			default:{
    			
    				cerr<<"Check youre input\n";
    				
    				system("Pause");
    				
    				break;}
    		}
    		
    		if(x='H'){break;}
    
    	}
    
    
    
    	getch();
    	return 0;
    }
    the problem i am having with the switch is the following: when the user inputs the letter H, the while loop break as well as the whole program, that is what is intended. but the thing is that even when you input another letter (A, B or C etc) the program breaks as well when the program shoold return to the beginning of the while loop and re-demand the input...

    PS: i intend to make a history and add other things to the application that is why i even bother to break the loop again

    thank you in advance for your help..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall the difference between = and ==
    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 Khaled's Avatar
    Join Date
    Apr 2011
    Location
    Lebanon, Beirut
    Posts
    2
    Thank you laserlight it worked, but i had another problem that i solved directly... when i set if(x=='H') the variable x wouldnt be initialised so it generated and error. i just initialised the x to 0 and it worked. thank you very much for your fast replay ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Sum Calculator
    By ScoutDavid in forum C Programming
    Replies: 14
    Last Post: 09-26-2010, 02:01 AM
  2. A Basic Calculator.
    By bijan311 in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2009, 01:53 PM
  3. Basic Calculator
    By Surge in forum C Programming
    Replies: 18
    Last Post: 12-02-2006, 10:20 PM
  4. Basic calculator program in C++
    By linkofazeroth in forum C++ Programming
    Replies: 70
    Last Post: 08-28-2005, 04:23 PM
  5. need help with my basic c++ calculator
    By iCouch_Potato in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2004, 06:23 PM