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:
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.