Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Дан файл, содержащий натуральные числа. Определить количество чисел
- этого файла, оканчивающихся заданной цифрой.*/
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int main() {
- setlocale(LC_ALL, "rus");
- string path;
- char digit;
- string line;
- int counter = 0;
- cout << "Введите путь к файлу: ";
- cin >> path;
- cout << "Введите цифру для поиска: ";
- cin >> digit;
- ifstream file(path);
- if (!file.is_open()) {
- cout << "Ошибка открытия файла" << endl;
- system("pause");
- exit(0);
- }
- cout << endl;
- for (int i = 0; !file.eof(); i++) {
- getline(file, line);
- cout << i + 1 << ")\t" << line;
- if (line[line.length() - 1] == digit) {
- cout << "*";
- counter++;
- }
- cout << endl;
- }
- cout << endl << "Чисел, заканчивающихся на цифру " << digit << ": " << counter << " шт." << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment