[Begginer] Help with a C++ calculator code, and some advice. menu

Shout-Out

User Tag List

Results 1 to 3 of 3
  1. #1
    Dobbs's Avatar Established Member
    Reputation
    72
    Join Date
    Aug 2009
    Posts
    321
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Begginer] Help with a C++ calculator code, and some advice.

    Hey guys, just started to learn programming, and I've come across some guide etc. but as i was following one to make a calculator, my compiler won't compile it. However it will compile other simple things for instance
    [spoiler]
    Code:
    #include <stdio.h>
    
    int main()
    {
     print( "This is some simple text.\n" );
     printf( "Press Enter to continue.\n" );
     getchar();
     printf( "I'm adding another test line.\n" );
     getchar();
     printf( "I'm adding even more test lines just to check.\n" );
     getchar();
     printf( "Press Enter to exit program.\n" );
     getchar();
     return 0;
    }
    [/spoiler]

    It can compile that and other similar things fine, anyway I'm just wondering if its the code or compiler. Here's the code [spoiler]
    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main()
    {
    
     float num1;
     float num2;
     int num3;
     float num4;
     
     cout << "Enter a number:" << endl;
     cin >> num1;
     
     cout << "Enter a second number:" << endl;
     cin >> num2;
     
     count << "Choose an operation." << endl;
     count << "Enter 1 to add, 2 to subtract, 3 to multiply, 4 to divide:" <<endl;
     cin >> num3;
     
     if (Num3 >4 || num3 <1)
     {
      cout << "Your operation choice isn't valid! Please run the program again." << endl;
      cout << "Press Enter to end program." <<endl;
      getchar()
      return 0;
     }
     else
     {
      if (num3 == 1)
      {
       num 4 = num1 + num2;
       cout << "Result is: "<< num4 << endl;
      }
      else if (num3 == 2)
      {
       num4 = num1 - num2;
       cout << "Result is: "<< num4 << endl;
      }
      else if (num3 == 3)
      {
       num4 = num1 * num 2;
       cout << "Result is: "<< num4 << endl;
      }
      else if (num 3 == 4)
      }
       num4 = num1 / num2;
       cout << "Result is: "<< num4 <<endl;
      }
      
     }  
      
     cout << "Press enter to end program." << endl;
     getchar();
     
     
     return 0;
    }
    [/spoiler]

    and the compiler I'm using is Bloodshed Dev-C++.
    If you guys know a better Compiler please let me know.

    [Begginer] Help with a C++ calculator code, and some advice.
  2. #2
    Zedane's Avatar Private
    Reputation
    7
    Join Date
    Oct 2010
    Posts
    2
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To start things off, I'll say you should definitely get Microsoft Visual Studio C++ Express Edition. It doesn't really matter whether you get 2008 or 2010, I prefer 2010 but it's up to you. The difference between the two isn't prevalent in console applications because you're not using .Net components. You could also "obtain" a copy of the full Visual Studio if your heart really desired.(I suggest doing so if you're serious about getting into coding.)

    I'll also point out that while following tutorials is a great way to learn, we have a term for people who just copy paste code without really understanding what it does or why it works. =)

    I'll do my best to explain step by step why things work they way they do.

    Step 1> Creating a Visual C++ Solution
    Load up Visual C++ Express. In the top left corner you'll see an icon that says "New Project" Click it. The Type of project you want is a Win32 Console Application. When you click OK you will be taken into the wizard. Click next. Here you'll see a couple option. Under additional options, you should uncheck Precompiled header and check Empty project Then click finish.

    Step 2> Adding Source Files
    In the solution explorer,(commonly on the left hand side) You'll see a folder labeled "Source Files". Right click that, click Add, then new item. In the list, find "C++ Source File." Call the file "Main" and hit ok.

    Step 3> Includes
    To get input or write to the console you have to use std and iostream, which stands for Standard Library. So you don't have to use std:: before all your function calls, at the top of the file type "using namespace std;"

    Your file should look like this:
    Code:
    #include <iostream>
    using namespace std;
    Step 4> Main method
    What return type your main should have is based on whether you want exit codes and stuff *Note there are other uses, but this is most common. For now, we'll be using a void return type, meaning after the main runs the program exits without returning a value.

    Here's the basic definition for a main method:
    Code:
    void main()
    {
    
    }
    Using this defines the entry point for the program, or where the code will start executing.

    If you wanted to set up command line arguments to parse you could do that like this:
    Code:
    void main(int argc, char ** arv)
    {
    
    }
    Command line arguments are brought in as an array. Argc is the Argument Count and argv is the Argument varlue which is brought in as an array of characters.

    Step 5> The code
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void main()
    {
    	//If we've selected to exit the program we want to exit the loop
    	bool done = false;
    	//This will be a variable to determine what action we're doing
    	int selection;
    	//These are our math variables.  We don't need floats because we're not doing precision
    	//floating point math like if you were doing project matrix math or something.
    	//However, we don't want to use ints because sometimes you'll get decimals while dividing
    	//so we need to use doubles.
    	double input1, input2, output;
    
    
    	//This will be our main loop.  While we're NOT done performing operations, Display our message
    	while (!done)
    	{
    		system("cls");
    		//cout is the command used to represent the output stream
    		//the syntax is cout << "string"  
    		//Remember that cout doesn't go to the next line automatically 
    		//So we can do one of two things.  We can either use endl which stands for 
    		//end line or we can use "\n" to indicate a new line.
    		cout << "#################################################" << endl;
    		cout << "# Caluculator Application" << endl;
    		cout << "# Usage: 1 - Addition | 2 - Subtraction |\n";
    		cout << "# 3 - Multiplication | 4 - Division | 0 - Exit" << endl;
    		cout << "#################################################" << endl;
    		cout << "> "; cin >> selection;
    
    		switch(selection)
    		{
    		case 0:
    			//User has entered 0, we're done and will exit the loop
    			done = true;
    			break;
    		case 1:
    			//User has Selected Addition
    			//Prompt the user to enter the first number
    			//cin will read the input stream until the first NULL symbol is reached, or the first "space"
    			cout <<"Enter the First Number > "; cin >> input1;
    			cout <<"Enter the Second Number > "; cin >> input2;
    			output = input1 + input2; //Do our calculations
    			cout <<"> The sum of " << input1 << " and " << input2 << " is " << output << endl;
    			system("pause");
    			break;
    		case 2:
    			//User has selected Subtraction;
    			//Prompt the user to enter the first number
    			cout <<"Enter the First Number > "; cin >> input1;
    			cout <<"Enter the Second Number > "; cin >> input2; //promt for var 2
    			output = input1 - input2; //Do our calculations
    			cout <<"> The difference between " << input1 << " and " << input2 << " is " << output << endl; //Output
    			system("pause");
    			break;
    		case 3:
    			//User has selected Multiplication
    			cout <<"Enter the First Number > "; cin >> input1;
    			cout <<"Enter the Second Number > "; cin >> input2; //promt for var 2
    			output = input1 * input2; //Do our calculations
    			cout <<"> The product of " << input1 << " and " << input2 << " is " << output << endl; //Output
    			system("pause");
    			break;
    		case 4:
    			//User has selected division
    			cout <<"Enter the First Number > "; cin >> input1;
    			cout <<"Enter the Second Number > "; cin >> input2; //promt for var 2
    			//Now what if we try to divibe by zero?  The program will encounter an exception 
    			//and crash out on you, wee need to check if the input is 0
    			if (input1 == 0)
    			{
    				cout << "--The numerator cannot be 0, please enter it again > "; cin >> input1;
    			}
    			else
    			{
    				output = input1/input2;
    			}
    			cout <<"> The quotient of " << input1 << " and " << input2 << " is " << output << endl; //Output
    			system("pause");
    
    			break;
    		case 5:
    			break;
    		default:
    			cout << "Invalid Input, try again" << endl;
    			system("pause");
    			break;
    		}
    
    	}
    
    	//When we exit the loop, we want to give the user a chance to read data on the screen
    	//Just in case there's something important they need.
    	system("pause");
    }
    Hopefully this gives you an excellent basic understanding of how C++ console output and input work. If you have any more questions, just let me know and I'll try to answer them.

  3. #3
    Dobbs's Avatar Established Member
    Reputation
    72
    Join Date
    Aug 2009
    Posts
    321
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for this man, +rep for all your hard work, however since posting this I've been advised to learn VB, as it's easy and a common language. I use Microsoft Visual Basic 2010 Express and hope to start learning C++ when got to grips with VB, as nearly got the calculator thing coded, also tried to code the last couple of buttons with out much help to to try and learn, if you got anymore great advice please post here or pm me. Thanks
    Last edited by Dobbs; 10-31-2010 at 10:36 AM.

Similar Threads

  1. [Request] Help with coördinates walking / source code.
    By Dronelol in forum WoW Bots Questions & Requests
    Replies: 1
    Last Post: 10-14-2014, 09:54 AM
  2. [Selling] RoS account with ALL classes lvl 70 and some gg items! Check it out!
    By jigok123 in forum Diablo 3 Buy Sell Trade
    Replies: 0
    Last Post: 06-01-2014, 06:03 PM
  3. [Buying] LF rift acc with a 60 on it and some alts
    By andypanz in forum Rift Buy Sell Trade
    Replies: 1
    Last Post: 06-24-2013, 08:44 AM
  4. i need help with a LUA teleport item and buff item...
    By trujillo in forum WoW EMU Questions & Requests
    Replies: 17
    Last Post: 09-16-2009, 02:05 AM
  5. help with getting TBC blood elfs and draenei
    By darknytte in forum WoW EMU Questions & Requests
    Replies: 5
    Last Post: 11-04-2008, 02:29 PM
All times are GMT -5. The time now is 08:50 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search