Basic Calculator I made check it out if you want...

This is a discussion on Basic Calculator I made check it out if you want... within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; int main() { const string HEADER ( "\t\t\tBasic Calculator Program\n\n" ); const ...

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Basic Calculator I made check it out if you want...

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	const string HEADER ( "\t\t\tBasic Calculator Program\n\n" );
    	const string SUBHEAD ( "\t\t\tEnter in a math problem\n\n" );
    	const string line ( 80, '*' );
    	float FirstNumber; 
    	char Sign; 
    	float SecondNumber; 
    	float Total;
    
    	cout.precision(4);
    
    	cout << line << endl << endl;
    	cout << HEADER << SUBHEAD << line << endl;
    
    	while ( true ) {
    
    		cout << ">";
    		cin >> FirstNumber >> Sign >> SecondNumber;
    
    		switch ( Sign ) 
    		{
    
    		case '+':
    
    			Total = ( FirstNumber + SecondNumber );
    
    			cout << "\n" << FirstNumber << " + " << SecondNumber << " = " << Total << "\n\n";
    			break;
    
    		case '-':
    
    			Total = ( FirstNumber - SecondNumber );
    
    			cout << "\n" << FirstNumber << " - " << SecondNumber << " = " << Total << "\n\n";
    			break;
    
    		case '*':
    
    			Total = ( FirstNumber * SecondNumber );
    
    			cout << "\n" << FirstNumber << " * " << SecondNumber << " = " << Total << "\n\n";
    			break;
    
    		case '/':
    
    			Total = ( FirstNumber / SecondNumber );
    
    			if ( FirstNumber == 0 || SecondNumber == 0 ) 
    			{
    				cout << "\n\"Error you can't / by 0\" \n\n";
    				continue;
    
    			} else {
    
    				cout << "\n" << FirstNumber << " / " << SecondNumber << " = " << Total << "\n\n";
    				break;
    			}
    
    		default:
    
    			cout << "\n\"Error you can't do that \"\n\n";
    			cout << "Try again.\n\n";
    			continue;
    		}
    
    
    	}
    
    	system("pause");
    
    	return(0);
    }
    Something I made really quick Because I'm board...... Comment if you want. Any help would be nice but you don't have to waste your time.
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    In the case of a division, you first divide and then check for zero divide, that doesn't make much sense it should be the other way around. Also, you shouldn't check if the first term is equal to zero because that is a perfectly valid division. Zero divided by any term that is non-zero will always be zero.

    There's nothing else I see that could be problematic... Well there is the fact that you use "using namespace std" which is evil especially if you start making your own math functions, this could result in name clashing with the standard library. Also, I would advise against using system(), I think the FAQ has an entry on why system() is evil, you could look it up.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    A few tweaks
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	const string HEADER ( "\t\t\tBasic Calculator Program\n\n" );
    	const string SUBHEAD ( "\t\t\tEnter in a math problem\n\n" );
    	const string line ( 80, '*' );
    	float FirstNumber; 
    	char  op ; 
    	float SecondNumber; 
    	float Total;
    
    	cout.precision(4);
    
    	cout << line << endl << endl;
    	cout << HEADER << SUBHEAD << line << endl;
    
    	while ( true ) {
    
    		cout << ">";
    		cin >> FirstNumber >> op >> SecondNumber;
    
    		switch ( op ) 
    		{
    
    		case '-':
    
    			Total = FirstNumber - SecondNumber ;
    			break;
    
    		case '+':
    
    			Total = FirstNumber + SecondNumber ;
    			break;
    
    		case '*':
    		case 'x':
    		case 'X':
    
    			Total = FirstNumber * SecondNumber ;
    			break;
    
    		case '/':
    
    			if ( SecondNumber == 0 ) 
    			{
    				cout << "\n\"Error you can't / by 0\" \n\n";
    				continue;
    			} 
    
    			Total = FirstNumber / SecondNumber ;
    			break;
    
    		default:
    
    			cout << "\n\"Error you can't do that \"\n\n";
    			cout << "Try again.\n\n";
    			continue;
    		}
    		
    		
    		cout << "\n" << FirstNumber << " " << op << " " << SecondNumber << " = " << Total << "\n\n";
    
    	}
    
    	system("pause");
    	return(0);
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    Instead of using system.... How about this..... >
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	const string HEADER ( "\t\t\tBasic Calculator Program\n\n" );
    	const string SUBHEAD ( "\t\t\tEnter in a math problem\n\n" );
    	const string line ( 80, '*' );
    	float FirstNumber; 
    	char  sign ; 
    	float SecondNumber; 
    	float Total;
    
    	cout.precision(4);
    
    	cout << line << endl << endl;
    	cout << HEADER << SUBHEAD << line << endl;
    
    	while ( true ) {
    
    		cout << ">";
    		cin >> FirstNumber >> sign >> SecondNumber;
    		cin.ignore();
    
    		switch ( sign ) 
    		{
    
    		case '-':
    
    			Total = FirstNumber - SecondNumber ;
    			break;
    
    		case '+':
    
    			Total = FirstNumber + SecondNumber ;
    			break;
    
    		case '*':
    		case 'x':
    		case 'X':
    
    			Total = FirstNumber * SecondNumber ;
    			break;
    
    		case '/':
    
    			if ( SecondNumber == 0 ) 
    			{
    				cout << "\n\"Error you can't / by 0\" \n\n";
    				continue;
    			} 
    
    			Total = FirstNumber / SecondNumber ;
    			break;
    
    		default:
    
    			cout << "\n\"Error you can't do that \"\n\n";
    			cout << "Try again.\n\n";
    			continue;
    		}
    		
    		
    		cout << "\n" << FirstNumber << " " << sign << " " << SecondNumber << " = " << Total << "\n\n";
    
    		
    
    	}
    
    	cin.get();
    	return(0);
    }
    Last edited by Sshakey6791; 01-06-2009 at 09:39 PM.
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  5. #5
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    I like it. Thanks for checkout my program..... Thanks for the help, Both of you
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    How about using an even better solution than cin.get (IMHO)?
    http://apps.sourceforge.net/mediawik...=Pause_console
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    I imagine that using namespace std could be made less evil just by putting it within main(). This way you confine the possible plague of ambiguity to one function, and lose no functionality.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Not using it at all is even better. Typing std:: is only five characters and there is no possible confusion neither for the compiler or anyone that reads your code.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by Desolation
    Not using it at all is even better.
    We have had this discussion before, and considering that Sutter and Alexandrescu disagree with you (but then I disagree with them on the finer details, but perhaps they merely were not explicit enough in C++ Coding Standards), it is clear that this is a case of style where infinite are the arguments of mages.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 04:13 AM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  4. Visual Basic Adodc Problem
    By rahat in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 06:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21