In a previous articleData output on the screen and in theentray data from the keyboardWe 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.
Show code
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intdigit=0;
doubledigit2=0;
charsymbol=0;
booltrueOrFalse=0;
cout<<"Введите целое число: ";
cin>>digit;
cout<<"Введите вещественное число: ";
cin>>digit2;
cout<<"Введите символ: ";
cin>>symbol;
// в переменную типа bool с помощью cin можно ввести
// только числа 0 (интерпретируется как false) и 1 (true)
cout<<"Введите 0 или 1: ";
cin>>trueOrFalse;
cout<<endl<<endl;
cout<<"Целое число: "<<digit<<endl;
cout<<"Вещественное число: "<<digit2<<endl;
cout<<"Символ: "<<symbol<<endl;
cout<<"bool: "<<trueOrFalse<<endl;
return0;
}
Result:
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)
In future, we will learn how to work with strings with you and we will not have to keep the words in different variables and display them on the screen spell.
3. Display the text in this form:
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.
Show code
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
floatnum1=0;
floatnum2=0;
cout<<"Введите первое число: ";
cin>>num1;
cout<<"Введите второе число: ";
cin>>num2;
cout<<num1<<" + "<<num2<<" = "<<num1+num2<<endl;
cout<<num1<<" - "<<num2<<" = "<<num1-num2<<endl;
cout<<num1<<" * "<<num2<<" = "<<num1*num2<<endl;
cout<<"Средне арифметическое: ";
cout<<num1<<" + "<<num2<<" / "<<2<<" = ";
cout<<(num1+num2)/2<<endl;
cout<<endl;
return0;
}
Questions to ask in the comments.
4.4
57
37 thoughts on “Tasks: Data input and output”
for some reason some of the code is not inserted
I'll try again
#include #include using namespace std;
int main() { setlocale(LC_ALL, “rus”);
int a = 0; int b = 0;
cout << "Показать на экране произведение введенных двух чисел, "Show on the screen the product of the entered two numbers, "Show on the screen the product of the entered two numbers. " << endl; cout << "Введите два числа " <> a >> b;
cout << "Вы ввели: " << a << " и "<< b << endl;
cout << "Произведение двух чисел равно: " << a * b << endl; cout << "Сумма двух чисел равна: " << a + b << endl; cout << "Разница двух чисел равна: " << a – b << endl; cout << "Среднее арифметическое число введенных двух чисел: " << (a + b) / 2 << endl;
Solving all three problems in one code. Don't judge strictly ,I'm a teapot. #include using namespace std;
int main() { setlocale(LC_ALL, “rus”); // create nine variables to enter the user's personal data int name = 0; int surname = 0; int data = 0; int number = 0; char a = 0; char b = 0; char c = 0; char d = 0; char e = 0; // requesting data input from the keyboard cout <> name; cout << endl; cout <> surname; cout << endl; cout <> data; cout << endl; cout <> number; cout << endl; // display the received data on the screen cout << "Ваше цифровое имя: " << name << endl; cout << "Ваша цифровая фамилия: " << surname << endl; cout << "Цифровая дата твоего рождения: " << data << endl; cout << "Ваш личный цифровой номер: " << number << endl; // please enter your username character by character cout << "Просим ввести пятизначное слово пароль!\nYou need to enter one character at a time. In Latin!: " <> a; cin >> b; cin >> c; cin >> d; cin >> e; // display the code name on the screen cout << "Кодовое имя пользователя: " << a << b << c << d << e << "." << endl; // display the phrase on the screen cout << "А почему в ресторане ей никогда не нравится то,\n\nчто заказала она, and always like it, what did I order?\n\n"; cout << "И она начинает есть у меня из тарелки. I tell her:\n\n\"Закажи себе то же самое\". She says: \"Зачем?\n\nЯ только попробовать\". И сьедает половину.\n\n"; cout << "\t\\\\ к.ф. \"О чем говорят мужчины\"\\\\\t" << endl; // and now a mini calculator! Let's count the sum ,difference and product of two numbers // create variables of type int int number1 = 0; int number2 = 0; // we ask you to enter two numbers cout <> number1; cin >> number2; // count and display the results cout << "Сумма заданных чисел: " << number1 + number2 << " !" << endl; cout << "Разница заданных чисел: " << number1 – number2 << " !" << endl; cout << "Произведение заданных чисел: " << number1 * number2 << " !" << endl;
#include using namespace std; int main() { setlocale(LC_ALL, “RUS”); cout << "А почему в ресторане eй никогда не нравится то,"<<'\n'; cout << "что заказала она,"<<"и всегда нравится то,"<<"что заказал я?"<<'\n'; cout << "И она начинает есть у меня из тарелки."<< "Я ей говорю:"<<'\n'; cout << "<>.” <<"Она говорит:"<<"Зачем?"<<'\n'; cout <>.”<<"И съедает половину."<<'\n'; cout << '\/' << '\/'<<"к.ф." <<"Очем говорят мужчины"<< '\/' << '\/'<<'\n'<<endl<<endl<<endl;}
for some reason some of the code is not inserted
I'll try again
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “rus”);
int a = 0;
int b = 0;
cout << "Показать на экране произведение введенных двух чисел, "Show on the screen the product of the entered two numbers, "Show on the screen the product of the entered two numbers. " << endl;
cout << "Введите два числа " <> a >> b;
cout << "Вы ввели: " << a << " и "<< b << endl;
cout << "Произведение двух чисел равно: " << a * b << endl;
cout << "Сумма двух чисел равна: " << a + b << endl;
cout << "Разница двух чисел равна: " << a – b << endl;
cout << "Среднее арифметическое число введенных двух чисел: " << (a + b) / 2 << endl;
cout << endl;
return 0;
}
cin>>a;
cin>>b;
Solving all three problems in one code.
Don't judge strictly ,I'm a teapot.
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “rus”);
// create nine variables to enter the user's personal data
int name = 0;
int surname = 0;
int data = 0;
int number = 0;
char a = 0;
char b = 0;
char c = 0;
char d = 0;
char e = 0;
// requesting data input from the keyboard
cout <> name;
cout << endl;
cout <> surname;
cout << endl;
cout <> data;
cout << endl;
cout <> number;
cout << endl;
// display the received data on the screen
cout << "Ваше цифровое имя: " << name << endl;
cout << "Ваша цифровая фамилия: " << surname << endl;
cout << "Цифровая дата твоего рождения: " << data << endl;
cout << "Ваш личный цифровой номер: " << number << endl;
// please enter your username character by character
cout << "Просим ввести пятизначное слово пароль!\nYou need to enter one character at a time. In Latin!: " <> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
// display the code name on the screen
cout << "Кодовое имя пользователя: " << a << b << c << d << e << "." << endl;
// display the phrase on the screen
cout << "А почему в ресторане ей никогда не нравится то,\n\nчто заказала она, and always like it, what did I order?\n\n";
cout << "И она начинает есть у меня из тарелки. I tell her:\n\n\"Закажи себе то же самое\". She says: \"Зачем?\n\nЯ только попробовать\". И сьедает половину.\n\n";
cout << "\t\\\\ к.ф. \"О чем говорят мужчины\"\\\\\t" << endl;
// and now a mini calculator! Let's count the sum ,difference and product of two numbers
// create variables of type int
int number1 = 0;
int number2 = 0;
// we ask you to enter two numbers
cout <> number1;
cin >> number2;
// count and display the results
cout << "Сумма заданных чисел: " << number1 + number2 << " !" << endl;
cout << "Разница заданных чисел: " << number1 – number2 << " !" << endl;
cout << "Произведение заданных чисел: " << number1 * number2 << " !" << endl;
system("PAUSE");
return 0;
}
in the second problem,where is the place in memory where the user will enter the word?
#include
using namespace std;
int main()
{ setlocale(LC_ALL, “RUS”);
cout << "А почему в ресторане eй никогда не нравится то,"<<'\n';
cout << "что заказала она,"<<"и всегда нравится то,"<<"что заказал я?"<<'\n';
cout << "И она начинает есть у меня из тарелки."<< "Я ей говорю:"<<'\n';
cout << "<>.” <<"Она говорит:"<<"Зачем?"<<'\n';
cout <>.”<<"И съедает половину."<<'\n';
cout << '\/' << '\/'<<"к.ф." <<"Очем говорят мужчины"<< '\/' << '\/'<<'\n'<<endl<<endl<<endl;}
// project1.cpp : This file contains the function “main”. Execution of the program begins and ends here.
//
#include “pch.h”
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “Rus”);
int a;
float b;
char c;
double d;
cout <> a;
cout <> b;
cout <> c;
cout <> d;
cout << endl;
cout << "Переменная а = " << a << endl;
cout << "Переменная b = " << b << endl;
cout << "Переменная c = " << c << endl;
cout << "Переменная d = " << d << endl;
}
//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.