Hi if i want to say if (int <= 0 || int is not a number)
how could i do this i want to make sure that a positive number is input not a negative number or word or symbol hope this makes sense
Hi if i want to say if (int <= 0 || int is not a number)
how could i do this i want to make sure that a positive number is input not a negative number or word or symbol hope this makes sense
Use the scanf function with a conversion specifier.
scanf("conversion specifier", variable);
%d means integer
%f means floating point munber
%c means any character input
Example : scanf("%d",i);
The variable i will contain only digits.
hi
Use the scanf function with a conversion specifier.
scanf("conversion specifier", variable);
%d means integer
%f means floating point munber
%c means any character input
Example : scanf("%d",i);
The variable i will contain only digits.
Correct solution but for the C language.... Works for C++ too though.
I would propose:
cin.ignore(numeric_limits<int>::max(), '\n');
the C++ way of saying 'the largest possible integer number'
I know it looks ugly.... :D
Alas, ddanby's "solution" is absolutely incorrect. Must be
if (scanf("%d",&i) == 1 && i > 0) { // OK
...
}
else { // error or eof or <= 0
If you have a C-string str with text, test if it's an integer:
if (sscanf(str,"%d",&i) == 1 && i > 0) { // OK
As usually ;) sidatra79's "solution" does not bear a relation to the problem...
Every solution proposed here has major problems -- that of trying to read an integer when a 'not a number' can be entered which screws up the entire read process..
You need to read an entire line as a string, test all characters entered up to the '\n' to make sure they are all digits. If they are, convert the string to an integer, if not, output an error.
thank you for all your help i will try these solutions
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.