Answer the question
In order to leave comments, you need to log in
C++ Why doesn't counter work?
I'm learning C++ from scratch. I came up with a multiplication table for myself using switch, for and if.
The goal was to show two options. 1 User number. 2. The whole multiplication table.
The second option would like to do with spaces between every 10 lines using if. But counter refuses to pass. I know that you can skip a line with cout << endl; Why counter doesn't work?
#include <iostream>
using namespace std;
int main() {
int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9, j = 10;
int my; // Число пользователя
char x; // Выбор для switch
int y; // Для for
int counter;
counter = 0;
setlocale(LC_ALL, "Rus");
cout << "Это таблица умножения." << endl;
cout << "Выберите условие." << endl;
cout << "1. Умножить свое число." << endl;
cout << "2. Показать таблицу умножения." << endl;
cin >> x;
switch (x) {
case '1':
cout << "Введите Ваше число" << endl;
cin >> my;
cout << "\n" << endl;
for (y = 0; y <= 10; y++)
cout << y << " * " << my << " = " << y * my << endl;
break;
case '2': {
for (y = 0; y <= 10; y++)
cout << a << " * " << y << " = " << a * y << endl;
for (y = 0; y <= 10; y++)
cout << b << " * " << y << " = " << b * y << endl;
for (y = 0; y <= 10; y++)
cout << c << " * " << y << " = " << c * y << endl;
for (y = 0; y <= 10; y++)
cout << d << " * " << y << " = " << d * y << endl;
for (y = 0; y <= 10; y++)
cout << e << " * " << y << " = " << e * y << endl;
for (y = 0; y <= 10; y++)
cout << f << " * " << y << " = " << f * y << endl;
for (y = 0; y <= 10; y++)
cout << g << " * " << y << " = " << g * y << endl;
for (y = 0; y <= 10; y++)
cout << h << " * " << y << " = " << h * y << endl;
for (y = 0; y <= 10; y++)
cout << i << " * " << y << " = " << i * y << endl;
for (y = 0; y <= 10; y++)
cout << j << " * " << y << " = " << j * y << endl;
counter++;
if (counter == 10){
cout << "\n";
counter = 0;
}
}
break;
default:
cout << "Вы ввели неверное условие" << endl;
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
Create a body for it:
for (y = 0; y <= 10; y++) {
cout << j << " * " << y << " = " << j * y << endl;
counter++;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question