Применение функции getche() в C++

Пример использования функции getche() в C++

#include <iostream>
#include <conio.h>
using namespace std;


int main()
{
    setlocale(LC_ALL, "Rus");

    int counter_space=0;
    int counter_char=0;
    char ch;
    cout << "Введите слово и нажмите enter" << endl;
    while ((ch=_getche())!='\r')
    {
        if (ch == ' ')
        {
            counter_space++;
        }
        else
        {
            counter_char++;
        }
        
    }

    cout << "Кол-во пробелов  " << counter_space <<endl;
    cout << "Кол-во символов  " << counter_char << endl;

    system("pause");
}

Input:123 45 6789

Вывод программы
getche пример

Если появится ошибка при компиляции

Ошибка C4996 ‘getche’: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _getche. See online help for details.

то необходимо поменять устаревшую функцию getche() на новую функцию _getche()

 

448

Leave a Reply

Ваш адрес email не будет опубликован.