The basics of programming in c++ for beginners

Data output to the screen and keyboard input

data output and data inputFirst, let's talk about the data output to a monitor, since you, partly, since it faced in previous lessons. As you remember, using the key words cout, we can bring to the monitor line, which we write in “quotes”, or the value of the variable, referring to her by name . As you are familiar with the operator endl, which allows you to translate the output to the next line. And one more important point – we can combine the output data using the operator << several times aftercout:

  cout << “The value of the variable var_name = ” <<  var_name <<  endl;

Now we meet with several special character sequences, which will help us to manipulate the data output to the screen. Here is a list:

output, basics of programming
Fig.1 – Character sequences, output

You look and think )))  Why use a backslash? Consider the example. We need to display a quote from the film. Just let the alarm sounds, which will attract the user's attention on the screen. Centered will place the title and name of the film, from which we quote, and lower –  quote.At screen should look like this:

output

Try to dial the code yourself! The solution below:

Why do we need a backslash? It tells the compiler: “Attention, for me is not just a symbol, and a special! He will tell you, what should be done! :)”  That line 8, if we did not use you backslash, the screen would go to five characters and a conclusion. А так, we hear “great music”.  In string 9 also interesting: two tabs, Further it is necessary to display a back slash, but in fact it is necessary to write two, well, take the name of the movie in quotes. At the end of each lineinstead of the flow manipulator endl (<< endl) which allows  go to a new line (actually an analogue of the Enter key  your keyboard) more convenient and shorter, in this case, use the control  symbol \n. 

Now let's talk about how to enter data from the keyboard by the user. We already know – to assign a value to a variable, it can be initialized when you create or assign a value to the downstream program: name of variable = value;  And now we learn to write down in the value of a variable, which introduces the user with the keyboard. We can organize the data entry operators usingcin  and>> . The syntax is:  cin >> nameofvariable.  Consider an example of this opportunity:

Run the program and note – program will command the 11-th row and, reaching the operatorcin >>, stop and start waiting for action from the user. It is necessary to enter the value and press Enter. Once the variable has the value, Permission from the keyboard, the program will continue execution.

data input

It is worth noting, whatcin understand and distinguish between the types of variables. And if you type in int  symbol, rather than the figure, value of the variable does not change. And if you enter an invalid value in a variable, which is not initialized when you create, is displayed on the screen any residual “garbage” of variable. To enter characters– Variables need to be declared type char. We will soon learn how to you to check user input is correct. And also learn how to enter the keyboard is not a single character, and the whole line.

The additional effort required from you – view video lessons :) They presented and no additional information is considered in the article. See all! For you it is only +

Желательно после прочтения теории приступить к практике – task here.

Share this article with your friends. We will appreciate! All your questions, ask in the comments.

31 thoughts on “Data output to the screen and keyboard input

  1. I wrote the following code:

    int amount_of_apples1=0;
    int amount_of_apples2=0;
    int inBox = 0; //recording the total amount of
    cin >> amount_of_apples1;
    cin >> amount_of_apples2;
    ….
    ….

    When the program starts, to a request entered is not a number but a string, it is clear that the value has not changed amount_of_apples1, remained 0. But this statement cin >> amount_of_apples2 was ignored, why. Meaning amount_of_apples2 not request and therefore remains too 0.

  2. #include
    using namespace std;

    int main()

    {
    setlocale(LC_ALL,”rus”);

    cout << " Why is it in a restaurant will never like it,\n";
    cout << " She ordered that the, and always like what I ordered?\n";
    cout << " And she begins to eat from my plate. I tell her:\n";
    cout << " <>.She says:<< What for?\n";
    cout <>.And eats half. N”;
    cout << " \t\t\\к.ф.\"О чем говорят мужчины\" \\ \n";

    return 0;

    }

    3 exercise made

  3. Why when I run the program instead of ordinary letters gets some sort of incomprehensible garbage?

    1. And because, when you want to answer the question, this “garbage” We need to show (copy), not to talk about it “on fingers”.

    1. code:
      #include
      #include

      char bufrus[256];
      char * rus(const char* text) {
      CharToOem(text, bufrus);
      return bufrus;
      }
      int main()
      {
      setlocale(LC_CTYPE, “rus”);
      std:: cout<<rus("Великий могучий русский язык\n");
      std:: cout<<"\nВеликий могучий русский язык\n";
      std::cout << "Hello world!" ;
      system("pause");
      return 0;
      }

  4. fatal error C1083: Can not open file inclusion: iostrem: No such file or directory
    What to do?

  5. Русские буквы вывод Windows XP

    code:
    #include
    #include

    char bufrus[256];
    char * rus(const char* text) {
    CharToOem(text, bufrus);
    return bufrus;
    }
    int main()
    {
    setlocale(LC_CTYPE, “rus”);
    std:: cout<<rus("Великий могучий русский язык\n");
    std:: cout<<"\nВеликий могучий русский язык\n";
    std::cout << "Hello world!" ;
    system("pause");
    return 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *