C++ Lesson 6 – Storage of information through user interactions and VARIABLES

HomeProgramming LanguageC++

C++ Lesson 6 – Storage of information through user interactions and VARIABLES

C++ Lesson 3 – Setting the background PROGRAMMING for LINUX.
C++ full tutorial | How to start C++ | Part 001
C++ Lesson 4 – Get ready for the first program

Check all C++ tutorials from here – C++ Tut pack

Well, we learned in previous lessons how to view the information we offer through the program and how to add comments to our program. But how can we do it if we want some information outside the program?

This is what we will learn today. When we retrieve information from outside the program, we should be able to store it in the program. So we use variables to store this information. Here we use different types of variables to store different types of information. To store this information we must first introduce the Ganga variable into the compiler. Let’s say declare a variable. So there is a method used to declare this variable. First we have to give the data type of the variable we are using and then the name we give to that variable.

Some of the most basic types of data we use are char, int and double. In this type of character data we can only store one character, and in this type of int data we can only store integers and in this double type of data we can store numbers with decimal places. So for all data types, this name is the keyword we use when declaring a variable.

Declaring variables in C++

As I said before using a variable, we must first declare that variable. So the rule when declaring a variable is first the data type and then the variable name.

If we take a small example:

int whole_number;
char letter;
double number_with_decimals;

And we can declare this way if we have multiple variables of the same type.

int a, b, c, d;

Variables are used

Well now we know how to declare a variable to the compiler. Now let’s see how to use this declared variable. So we need results to store this variable in our variable. It’s like the opposite side of Cout. Well, let’s see how we can use cin to store data for our variable.

#include <iostream>
using namespace std;
int main ()
{
    int thisisanumber;
    cout << “Please enter a number: “;
    cin >> thisisanumber;
    cout << “You entered: ” << thisisanumber << “\n”;
}

Let’s take the line and see what happens. I won’t tell you about the original line 3, because you already know it. We will see it soon.

int thisisanumber;

This is to declare this variable number in the int data type.

Qin >> thisisanumber;

cin >> This function stores the value of the user type in this variable number.

Earlier I told you in the program that if you open your program in cmd, it will exit quickly. But you can add this line in this program and the problem persists. This is because the “cin.get ()” line indicates that the program is waiting for the user to press the key. But in this program you have to memorize a value for the variable and type the value and press the Enter key where the signal pressed key takes this line “cin.get ()” so that the program exits quickly. We do this by adding another line to the program.

cin.ignore();
    cin.get()
}

This is done by ignoring the first key pressed and waiting for another key pressed.

Well, then you need to run this program. You can also use the other data types instead of the int data type where you only need to change the data type so you don’t have to change anything else in the code. We will get there in the next lesson.

COMMENTS

WORDPRESS: 0
DISQUS: 0
error: Content is protected !!