[C++] How to make a calculator (the correct way!) menu

User Tag List

Results 1 to 8 of 8
  1. #1
    skatercam11's Avatar Member
    Reputation
    11
    Join Date
    Jun 2008
    Posts
    56
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [C++] How to make a calculator (the correct way!)

    So I tried to read that other thread regarding creating a calculator, and I couldn't see any longer it burnt my eyes so bad. Now I know I used some bad programming practices, but it was all just for simplicities sake on the kiddies that are just learning. If you see any problems with my code, or have any suggestions, I'm more than happy to take constructive criticism.

    Anyways, here is the code, and it's commented out quite nicely so you shouldn't need me to explain every bit of code in little sections. If that is required I'd be more than happy to do it, just make a reply here and I'll update the original post.

    Code:
    // These are called headers. These things are wrote to provide the many different functions available to
    // you such as std::cout, std::endl, std::string, and many other different ones. You can probably go to
    // google and find the different commands wrote for you in these libraries if you'd like, but these are 
    // the only required ones to make this very simple calculator.
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	// Doubles will make an integer value, but increase the number of bytes available. This will allow
    	// the client to use bigger numbers, if they wish of course. These also can become decimals when there
    	// is a remainder.
    	double number1;
    	double number2;
    	double total;
    
    	// Integers are values that are either negative or positive. You can use signed or unsigned before
    	// declaring the integer to state if they are allowed to be a negative number or not, but we won't
    	// get into that right now. 
    	int current_operation;
    	int keep_running;
    
    	// Booleans are very simple. True or False? We will create this one boolean for the simple sake of
    	// teaching loops and the such, and just to make our program exitable in general.
    	bool programRunning = true;
    
    	// If you do not declare if you are checking if the statement is true or false, and you are not using
    	// the negation operator, then it will automatically assume the following statement is true. Below is
    	// an example of what this comment actually means...
    	//
    	// while(programRunning) returns true
    	// while(programRunning == true) returns true
    	// while(programRunning != false) returns true
    	//
    	// while(!programRunning) returns false
    	// while(programRunning == false) returns false
    	// while(programRunning != true) returns false
    	//
    	// Hopefully this clarified what I was trying to say :-).
    	while(programRunning)
    	{
    		cout << "Welcome to my very first program!" << endl;
    		cout << "First, we need to declare two variables and preform an mathematical operation to determine what the total is!" << endl;
    		cout << endl;
    		cout << "First value: "; 
    		cin >> number1; // NOTE: When using cin >> to recieve input, it will automatically create a new line for you.
    		cout << "Second value: ";
    		cin >> number2;
    		cout << endl;
    		cout << "Great! Now all we have to do is declare which mathematical operation you would like to use!" << endl;
    		cout << endl;
    		cout << "Type 1 if you would like to add the two numbers." << endl;
    		cout << "Type 2 if you would like to subtract the two numbers." << endl;
    		cout << "Type 3 if you would like to multiply the two numbers." << endl;
    		cout << "Type 4 if you would like to divide the two numbers." << endl;
    		cout << endl;
    		cout << "Input: ";
    		
    		cin >> current_operation;
    
    		cout << endl; // NOTE: This is used to make it look all pretty! Don't think it's random :-P.
    
    		if(current_operation == 1)
    		{
    			total = number1 + number2;
    
    			cout << "Your total is " << total << "!" << endl;
    			cout << endl;
    			cout << "Type 1 if you would like to find another answer." << endl;
    			cout << "Type 2 to exit the program." << endl;
    			cout << endl;
    			cout << "Input: ";
    			
    			cin >> keep_running;
    
    			if(keep_running == 1)
    			{
    				system("CLS");
    			}
    			
    			if(keep_running == 2)
    			{
    				programRunning = false;
    			}
    		}
    
    		if(current_operation == 2)
    		{
    			total = number1 - number2;
    
    			cout << "Your total is " << total << "!" << endl;
    			cout << endl;
    			cout << "Type 1 if you would like to find another answer." << endl;
    			cout << "Type 2 to exit the program." << endl;
    			cout << endl;
    			cout << "Input: ";
    			
    			cin >> keep_running;
    
    			if(keep_running == 1)
    			{
    				system("CLS");
    			}
    			
    			if(keep_running == 2)
    			{
    				programRunning = false;
    			}
    		}
    
    		if(current_operation == 3)
    		{
    			total = number1 * number2;
    
    			cout << "Your total is " << total << "!" << endl;
    			cout << endl;
    			cout << "Type 1 if you would like to find another answer." << endl;
    			cout << "Type 2 to exit the program." << endl;
    			cout << endl;
    			cout << "Input: ";
    
    			cin >> keep_running;
    
    			if(keep_running == 1)
    			{
    				system("CLS");
    			}
    			
    			if(keep_running == 2)
    			{
    				programRunning = false;
    			}
    		}
    
    		if(current_operation == 4)
    		{
    			total = number1 / number2;
    
    			cout << "Your total is " << total << "!" << endl;
    			cout << endl;
    			cout << "Type 1 if you would like to find another answer." << endl;
    			cout << "Type 2 to exit the program." << endl;
    			cout << endl;
    			cout << "Input: ";
    			
    			cin >> keep_running;
    
    			if(keep_running == 1)
    			{
    				system("CLS");
    			}
    			
    			if(keep_running == 2)
    			{
    				programRunning = false;
    			}
    		}
    	}
    
    	// This is the very end of the program. If the program did what it was supposed to do, and checked
    	// to see if everything exited okay, and there were no errors, then it will return the value of 1.
    	return 1;
    }

    If you would like to modify the code and make it your own, then feel free to do that. Share what you have, and maybe others will learn from your own personal experiences you had!

    And again, if you have something to say about the code, then please make it somewhat intelligible. I would be more than happy to change my code if it does not meet the standards of some of the veteran programmers out there (though I think this is about as easy as it gets, so I don't see a problem with it at all).

    Enjoy!

    [C++] How to make a calculator (the correct way!)
  2. #2
    Garosie's Avatar Active Member
    Reputation
    18
    Join Date
    Jul 2007
    Posts
    68
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, i would have wrote that code like this:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    bool R = true; //Running
    
    void Calculator()
    {double number1, number2, total;
     int current_operation, Run;
    
    		cout << "First value: "; 
    		cin >> number1; 
    		cout << "Second value: ";
    		cin >> number2;
    
    		cout << endl;
    
    		cout << "Which operation would you like use?" << endl;
    		cout << "Type 1 for Plus." << endl;
    		cout << "Type 2 for Minus." << endl;
    		cout << "Type 3 for Divide." << endl;
    		cout << "Type 4 for Multiply.\n" << endl;
    		cout << "Input: ";
    		cin >> current_operation;
    		cout << endl; 
    
    		if(current_operation == 1)
    		{
    			total = number1 + number2;
    			cout << "Your total is " << total << "!\n" << endl;
    		}
    
    		if(current_operation == 2)
    		{
    			total = number1 - number2;
    			cout << "Your total is " << total << "!\n" << endl;
    		}
    
    		if(current_operation == 3)
    		{
    			total = number1 * number2;
    			cout << "Your total is " << total << "!\n" << endl;
    		}
    
    		if(current_operation == 4)
    		{
    			total = number1 / number2;
    			cout << "Your total is " << total << "!\n" << endl;
    		}
    
    		cout << "Type 1 for Re-entering Calculator." << endl;
    		cout << "Type 2 for Exitting program.\n" << endl;
    		cout << "Input: ";
    		cin >> Run;
    
    			if(Run == 1)
    			{
    				system("CLS");
    			}
    			
    			if(Run == 2)
    			{
    				R = false;
    			}
    }
    
    int main()
    {
    	while(R)
    	{
    		Calculator();
    	}
    }
    Easy, nice and simple.
    Last edited by Garosie; 07-18-2009 at 07:22 AM.

  3. #3
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1356
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BOTH of your calculators suck. Stop the epeen sword fighting beause you're both terrible programmers.

  4. #4
    insignia96's Avatar Banned
    Reputation
    33
    Join Date
    Oct 2008
    Posts
    304
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As usual we have the shattering truth from the legendaries!

  5. #5
    nothinglol's Avatar Contributor
    Reputation
    166
    Join Date
    Mar 2009
    Posts
    769
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doublekill.

  6. #6
    skatercam11's Avatar Member
    Reputation
    11
    Join Date
    Jun 2008
    Posts
    56
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Caroe View Post
    Ok, now im actually getting a bit curious How can u say my tutorials are "kiddie versions?" and then write:

    double number1;
    double number2;
    double total;

    Huh? What noob makes it like that? everybody would know to make it short and easy;

    double number1, number2, total;

    and btw, this code is just a different way of putting my own calculator (well my guides werent so big, as i figured out, that would be too much work, explaining) but i have 200 lines calculator i builded at my own. and ive gotten even more advanced. But don't u say that other's tutorials are "kiddie" version, when your making out, the noobs way. and btw. ALL TUTORIALS ARE GOOD, AS LONG AS PEOPLES TRIES TO LEARN OTHER PEOPLES..

    Thx for your time, reading this.
    LOL, this was made 5-22-09 dude, calm down. I don't think putting white-space in my code makes me any much more of a noob than you are by initializing variables on a single line. If you're worried about having "too much work" on writing a BEGINNERS tutorial, then you shouldn't be writing tutorials at all. Not all tutorials are good, and in fact, bad tutorials are worse then no tutorials. If you A) don't know what you're doing, B) too lazy to type it, or C) can't explain it clearly enough or don't know the correct terminology, then you should NOT be writing tutorials at ALL. If you write a horrible tutorial without explanation for beginners, they will start to write horrific code as displayed in your calculator "tutorial". I don't quite understand "peoples tries to learn other peoples", so I'll just let you figure out exactly what you mean there. I'm glad you "builded" a 200 line calculator, whatever helps you learn, but honestly, you don't need to be posting tutorials, or rather "samples" until you have a complete understanding of the fundamentals of C++.

    @Cypher: Yes, the calculator does suck and I have no desire to be throwing around any e-peen because I know I'm a terrible programmer. I've only been doing it for about three years now, off and on of course, and I only cleaned up what he was displaying. As you can tell I didn't try to create anything else and display it as I'm still learning, which again, all I was doing was cleaning up some of the shit he laid around. But, the good thing is, I'm taking your advice and learning my language before I jump into anything big, and I'm not displaying anything I don't know how to do already.

    @insignia96 & nothinglol: By all means, stick behind Cypher's words if you'd like. I have no reason to debate weather I'm a terrible programmer or not, considering I consider Cypher my god as well as a programmer, haha. But really, there is no need to troll in a thread this old -_-.

    Now please, just let the thread die already, and if it helped anyone out, you're welcome. Lol.
    Last edited by skatercam11; 07-18-2009 at 04:58 AM.

  7. #7
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1356
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Christ, both of you shut up. Both your programs are written awfully and with an obviously lack of STL and C++ knowledge.

    Next person to QQ about the other is getting a -rep for being a retard.

  8. #8
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1356
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Caroe View Post
    Well actually thats a bad setting. Its good to argue and make discussions. That way we learn from eachother, and we learn to handle problems on our own.

    Two idiots arguing doesn't help anyone.

Similar Threads

  1. How to make 2 realms(the real way)
    By volitle in forum WoW EMU Guides & Tutorials
    Replies: 114
    Last Post: 01-09-2012, 06:48 AM
  2. How to make Checksum return the correct Value
    By Xaser in forum Programming
    Replies: 2
    Last Post: 02-12-2009, 06:30 PM
  3. How to make sure your the biggest retard on your server.
    By h4t3 in forum World of Warcraft Guides
    Replies: 27
    Last Post: 05-22-2008, 07:59 AM
  4. How to make gold on the AH (needs 2 accounts)
    By waynepetiylana in forum World of Warcraft Guides
    Replies: 18
    Last Post: 05-17-2008, 02:07 AM
  5. How to make a movie (the basics)
    By Nemorosa in forum Community Chat
    Replies: 8
    Last Post: 01-25-2008, 05:48 AM
All times are GMT -5. The time now is 11:35 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search