The basics of programming in c++ for beginners

Tasks: Data input and output

In a previous articleData output on the screen and in theentray data from the keyboard We examined in detail this topic. It's time to practice – because we have a lot of work, to develop certain skills, needed a programmer and develop their logical thinking quietly.

1. Create 4 variables with different types of data and ask the user to enter values ​​in them. After entering the, display them on the screen.

2. Create 5 variables of type char, ask the user to enter a word of five letters, and show these symbols (the word) the screen. (Enter characters in Latin, tk. cyrillic is displayed incorrectly. Why? This will be discussed in one of our next lessons)

3. Display the text in this form:

data display

4. The user must enter 2 numbers. You need to show on the screen the product of these numbers, sum and difference. Show the same arithmetic mean of these numbers entered.


Questions to ask in the comments.

74 thoughts on “Tasks: Data input and output

    1. it later, that the function setlocale(LC_ALL, “rus”); the ability to correctly display the Cyrillic only conclusion to the screen. During commissioning it does not meet. We'll consider, how to configure the console to work correctly in one of the articles. Be patient )))
      Thank you, which does not allow me to relax. I write to my bold wishes all readers ))

    1. That's the make and add the code in the comments! Let everyone see, you, this task is accomplished by )))

  1. You may be able to improve the calculator appending this code
    cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;

  2. #include
    #include
    using namespace std;

    int main()
    {
    setlocale(0, "");
    cout < < "А почему в ресторане ей никогда не нравится то,\n" << "что заказала она, и всегда нравится то, что заказал я?\n" << "И она начинает есть у меня из тарелки. Я ей говорю: Зачем?\n" << "Она говорит: Я только попробую...\n" << "И съедает половину!\n" << "// к.ф \"О чем говорят мужчины\" //" << endl; getch(); return 0; }

    1. 1) Why do not all require the use endl? Or vice versa the latter replaced the endl not n?

      2) getch() is from conio? I recommend to replace cin.get() in the name of fenshuya.

  3. 3 the problem I have got so:

    #include

    int main()
    {
    setlocale(LC_ALL, "rus");
    std::cout << "\t \t А почему в ресторане ей никогда не нравится то, \n";
    std::cout << "\t \t что заказала она, и всегда нравится то, что заказал я? \n";
    std::cout << "\t \t И она начинает есть у меня из тарелки. Я ей говорю: \n";
    std::cout << "\t \t \"Закажи себе то же самое \". Она говорит: \"Зачем? \n";
    std::cout << "\t \t Я только попробовать \". И съедает половину. \n";
    std::cout << "\t \t \t //к.ф. \"О чем говорят мужчины \" // \n";
    return 0;
    }

  4. Improved code for solving the first problem.
    Look who is interested.

    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");
    long long value0(0LL);
    double value1(0);
    int value2(0);
    char value3(0);
    const int max(45);
    char indicator(0);
    char trial[9][max]
    {
    "Продолжить (y/n)? ", //[0]
    "Введите целое число: ", //[1]
    "Введите нецелое число: ", //[2]
    "Введите целое число не больше 2-х млрд.: ",//[3]
    "Введите символ: ", //[4]
    "Значение типа long long = ", //[5]
    "Значение типа double = ", //[6]
    "Значение типа int = ", //[7]
    "Значение типа char = " //[8]

    };
    do
    {
    cout << value0;
    if (value0 > 10000000000)
    {
    cout << "Вы ввели значение больше 10-ти млрд.\n";
    cout << "По умолчанию изменено на 0.\n";
    value0 = 0;
    }
    cout << value1;
    cout << value2;
    if (value2 > 2000000000)
    {
    cout << "Это число больше 2-х млрд.\n";
    cout << "По умолчанию изменено на 0.\n";
    value2 = 0;
    }
    cout << value3;
    cout << trial[5] << value0 << "\n";
    cout << trial[6] << value1 << "\n";
    cout << trial[7] << value2 << "\n";
    cout << trial[8] << value3 << "\n";
    cout <> indicator;
    } while ((indicator == 'y') || (indicator == 'Y'));
    Console::ReadLine();
    return 0;
    }

  5. My solution to the challenge


    #include
    #include
    #define line cout << "---------------------------------------" << endl;
    #define taskcls system("cls");

    using namespace std;

    int main()
    {
    setlocale(0, "russian");
    /* 1 задача */

    int integer;
    double doubleVar;
    bool boolVar;
    char charVar;

    cout << integer;
    cout << doubleVar;
    cout << boolVar;
    cout << charVar;

    cout << "Integer: " << integer
    << "\nDouble: " << doubleVar
    << "\nFloat: " << boolVar
    << "\nChar: " << charVar;
    cout << endl;
    taskcls
    /* 2 задача */

    char var1, var2, var3, var4, var5;
    cout << var1 >> var2 >> var3 >> var4 >> var5;
    cout << var1 << var2 << var3 << var4 << var5;
    cout << endl;
    system("pause");
    taskcls
    /* 4 задача */
    int n1, n2, result;
    cout << n1 >> n2;
    result = n1 * n2;
    cout << "Произведение чисел n1 и n2: \t" << result << endl;
    line
    result = n1 + n2;
    cout << "Сумма чисел n1 и n2: \t\t" << result << endl;
    line
    result = n1 - n2;
    cout << "Разность чисел n1 и n2: \t" << result << endl;
    line

    _getch();
    return 0;
    }

  6. #include
    #include
    using namespace std;
    int main()
    {
    setlocale (LC_ALL,"Russian");
    double a,b,c;
    cout << "Введите a:" <> a;
    cout << "Введите b:" <> b;
    cout << "Введите c:" <> c;
    double D, x1, x2;
    D = b * b - 4*a*c;
    cout << "Дискриминант =" << D << endl;
    x1 = (-b + sqrt(D))/(2*a);
    x2 = (-b - sqrt(D))/(2*a);
    if (D < 0) {
    cout << "Дискриминант меньше нуля, корней нет." << endl;
    } else if (D == 0) {
    cout << "Дискриминант равен нулю, 1 корень." << x1 << endl;
    } else {
    cout << "Дискриминант больше нуля, два корня." << endl;
    cout << "x1=" << x1 << endl;
    cout << "x2=" << x2 << endl;
    }
    return 0;
    }

    Is it possible to improve this program possible?

Leave a Reply

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