Direct Download of Video(For mobile / offline viewing)(right click > save target as)

#include <string>
#include <iostream>

using namespace std;

int main(){
   //Write a program that allows a user to enter three numbers, store the sum of them in a variable called total, then output the total.
   //Ex. Credit:  re-write that program without the total variable.

   //Variables -- int / long,  double / float, and string / char
   string str = "Hello World";
   cout &lt; &lt; "Str = " &lt;&lt; str &lt;&lt; endl;
   int myNumber = 75000; // Whole numbers only
   cout &lt;&lt; "MyNumber = " &lt;&lt; myNumber &lt;&lt; endl;

   double myDouble = 5.5555;
   cout &lt;&lt; "MyDouble = " &lt;&lt; myDouble &lt;&lt; endl;

   int userValue = 0;
   cout &lt;&lt; "Please Enter a value: "; cin &gt;&gt; userValue;

   cout &lt; &lt; "userValue = " &lt;&lt; userValue &lt;&lt; endl;

   cout &lt;&lt; endl;
   system("PAUSE");
   return 0;
}

 

Download link for Visual Studio Express 2013 for Windows Desktop (This is needed to compile your code)

Homework is described and should be submitted here: http://beginnerscpp.com/forums/index.php/topic,51.0.html
–Hello World–
This lesson opened with an example of programming that’s literally as old as programming. Let’s look at the simplest program in C++ and then break it down into it’s parts.

#include
using namespace std;
int main(){

cout < < "Hello World" << endl;

return 0;
} //end main

 

#include
This line is telling the compiler that we’re going to be including a library called iostream. This library contains cout, cin, and a few other lesser-used input / output options for programs.


using namespace std;
This line tells the compiler that we’re going to be using items from the standard C++ namespace, this makes it so we don’t need to prepend std:: to commands like cin or cout.


int main(){
This line is fairly simple, it shows where the start of the “main” part of our program is. I want you guys to take special notice to the { though. That’s known as a Scope Bracket, these can make your life easier or harder based on the habits you develop with them now. You guys should use this as something of a rule when it comes to programming: “Every line I write should end with either a ; or an { or } . This will make your life much easier, especially in the early stages of programming.


cout < < “Hello World”<<endl;
This line calls the function “cout” from the iostream library, and passes in the text string “Hello World”. The part after that (endl) is just to make a new line after that. We’ll get more into how this works in subsequent lessons, or go here for more examples


return 0;
This line tells the compiler that it can “return” (to the command prompt) if everything went well. The return command will make more sense later. The reason we’re using return 0 and not some other number is because that means “The program exited normally” to most host OS’s.


} //end main
This is really just a closing bracket } , the //end main after it is what’s known as a comment. Comments are used by programmers to keep track of what’s what in a program. In this case when you have a longer program with many brackets, you might want to know that this bracket is closing your int main() opening bracket.

Concepts learned:

  • Including libraries (#include
  • Variables (type variableName)
  • Types (see below)
  • Initialization
  • Simple input / output

Types used:

int, long
These types are used to represent whole numbers.
double / float
These types are used to represent floating point values (numbers that may have decimal points).
string / char
String and char are two types that are used to represent letter and number values.

 

Last modified: April 13, 2019

Author