C/C++

Помощь в языке программирования

Я хочу чтобы если пользователь при вводе с клавиатуры вводит отрицательное число или просто букву, символ, то пишет что введите положительное или отрицательное число. С отрицательными разобрался, как мне это сделать с символами или с буквами?


#include <stdio.h>
#include <stdbool.h>
#include <iostream>

int main(){
int n;
int count = 0;
bool is = true;
printf("Enter N: ");
scanf("%d", &n);
if (n < 0 && %s) {
printf("Number cannot be negative");
return 0;
}
printf("Numbers that are mutually prime with N: ");
for (int i = 1; i < n; i++) {
for (int j = 2; j <= i; j++) {
if (n % j == 0 && i % j == 0) {
is = false;
}
}
if (is) {
printf("%d ", i);
count++;
}
is = true;
}
printf("\nThe number of such numbers: %d", count);
return 0;
}
C
 #include  
#include
#include
#include
#include
void flush(void) {
while (getchar() != '\n');
}
char space_ignore(void) {
char ch = getchar();
if (ch == '\n') return '\n';
while (isblank(ch)) ch = getchar();
return ch != '\n' ? ch : '\n';
}
int main(void) {
const char* msg = "Input a positive integer: ";
unsigned number;
printf("%s", msg);
while (true) {
long long value;
while (!scanf("%lli", &value)) {
puts("ERROR: invalid argument!");
printf("%s", msg);
flush();
}
if (value < 0 || space_ignore() != '\n') {
puts("ERROR: invalid type!");
printf("%s", msg);
flush();
continue;
}
if (value > UINT_MAX) {
puts("ERROR: out of range!");
printf("%s", msg);
flush();
continue;
}
number = (unsigned)value;
break;
}
printf("Positive integer: %u\n", number);
return 0;
}
C++
 #include  
#include
#include
#include
using namespace std;
bool digits_only(const string& line) {
for (auto ch : line) if (!isdigit(ch)) return false;
return true;
}
unsigned input(const char* msg) {
string value;
while (true) {
cout > value;
try {
if (!digits_only(value)) throw exception("");
auto number = stoul(value);
return number;
} catch (invalid_argument& e) {
cin.clear();
puts("Exception: invalid argument!");
} catch (out_of_range& e) {
cin.clear();
puts("Exception: out of range!");
} catch (exception& e) {
puts("Exception: invalid type!");
}
cin.ignore(0x1000, '\n');
}
}
int main() {
auto a = input("Input a positive integer: ");
cout
Станислав Паршаков
Станислав Паршаков
88 624
Лучший ответ
ну если сразу хотите отфильтровать
нечисловое введенное значение,
то используйте scanf для ввода обычной
строки, которую преобразуйте
в число, например функцией atoi
http://all-ht.ru/inf/prog/c/func/atoi.html