Beginner's Guide to Programming - guidetoprogramming.com

  • Increase font size
  • Default font size
  • Decrease font size

C++ Tutorial 2 - Variables, user input, If/Then statements, math, and output!

For our second tutorial, we will look at creating variables, getting user input to fill those variables, the IF/Then statement, math, and printing output.

Below is the code, not including comments:

#include <cstdio>
#include <iostream>
using namespace std;

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;

  cout << "Choose an operation." << endl;
  cout << "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)
        {
            num4 = 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 * num2;
            cout << "Result is: "<<  num4 << endl;
        }
        else if (num3 == 4)
        {
            num4 = num1 / num2;
            cout << "Result is: " <<  num4 << endl;
        }

  }

  cout << "Press Enter to end program." << endl;
  getchar();

  return 0;
}

 

Below is the code, commented to discuss the new items we are seeing:

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{

  //These are the variables we will be using
  //"int" tells the program to expect only integers
  //"float" tells the program that it may have to use floating point numbers (decimals)
  //Each line creates a variable we can use later.  In C++ you can't use a variable you haven't created.

  float num1;
  float num2;
  int num3;
  float num4;

  // "cout" prints a line out to the standard output (in this case the screen)
  // the output can be changed to a file, etc with methods we will learn in future tutorials

  cout << "Enter a number: " << endl;
  // "cin" reads from the screen.  It stops when Enter is pressed.
  //In this case, if the input isn't a number, the program will error out.

  cin >> num1;

  cout << "Enter a second number: " << endl;
  cin >> num2;

  cout << "Choose an operation." << endl;
  cout << "Enter 1 to add, 2 to subtract, 3 to multiply, 4 to divide: " << endl;
  //We use cin here to grab the user's selection as to the operation they want to perform
  //We are only collecting a number at this point, and will apply logic to the number later to match the user's intentions

  cin >> num3;

  //First, lets make sure the number is in our intended range (1-4)
  //We can use an if statement for this

  if (num3 >4 || num3 <1)
  //the above line checks to see if the number is greater than 4 or less than 1
  //If it is either of those, the program ends with the below message

  {
    cout << "Your operation choice isn't valid!  Please run the program again." << endl;
    cout << "Press Enter to end program." << endl;
    getchar();
    return 0;
  }
  //If the number is not greater than 4 or less than 1, we continue below
  else
  {
  //In each case here, we are checking to see which number the user has entered
  //We can use an if statement for this as well - notice that this if statement is still within the "else"
  //condition of the prior if statement
  //Once the if statement is satisfied, we are performing an action
  //First, we calculate num4 based on the user's selection
  //Then we print a line out (cout) which prints the text "Result is: " to the screen, then the calculated number

          if (num3 == 1)
        {
            num4 = 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 * num2;
            cout << "Result is: "<<  num4 << endl;
        }
        else if (num3 == 4)
        {
            num4 = num1 / num2;
            cout << "Result is: " <<  num4 << endl;
        }

  }

  //The below line is here to pause the program and allow the user to ponder the result
  //Once Enter is hit, the program ends

  cout << "Press Enter to end program." << endl;
  getchar();

  return 0;
}

 

Thinking through each line of the code should give you some feel for control structures (If/Then) and some ways of both getting data from the user and printing data back out to the screen.