A
A
andwin2016-06-13 20:27:26
C++ / C#
andwin, 2016-06-13 20:27:26

How to make letters to be entered in c++?

#include <iostream>
using namespace std;

int main()
{
    setlocale(0, "");
    double num;

    cout << "Ïðèâåò: ";
    cin >> num;

    if (num == 0) { // Если введенное число меньше 10.
        cout << "Как тебя зовут?" << endl;
    } else {
        cout << "пока" << endl;
    }
   setlocale(0, "");
    int b;
    cout << "Нажми 1 чтобы я закрылся";
    cin >> b;
    cout << b << endl;
     return 0;
}

How to make it so that in this program if you enter text, then it was as an option for action?
I'm trying to type text instead of 0 - I get an error. What to do?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GavriKos, 2016-06-13
@GavriKos

Read carefully what kind of error, think a lot, read about data types "string" and methods of working with them. Google strcmp for a hint.

D
Daniil Igorevich, 2016-06-13
@Petr_Anisimov

Is this about age? Then why type double? An unsigned short int would do here . And why are there two setlocales in the program?

double num;

    cout << "Ïðèâåò: ";
    cin >> num;

B
Bonce, 2016-06-14
@Bonce

Cin breaks if the input type does not match the declared type. Therefore, you should use

while (!(cin >> num) || num < 0.0)
{
     cin.clear();
     while (cin.get() != '\n')
          continue;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question