!isdigit isnt working (C programming) [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to create a random number guessing game that generates a random number between 1-10 and have the user guess it. It then tells the user if they're right or not. That part works fine, but the problem also asked for me to utilize the isdigit() function. I cannot get this to work for some reason. It says everything is a digit.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
int main(){
srand(time(NULL));
int x;
int r;
r=(rand() %10)+1;
r=6;
printf(" Select a number between 1 and 10 \n");
scanf(" %d", &x);
if(!isdigit(x)){
if(x==r)
printf("Congratulations, you guessed correctly! \n");
else{
printf("Uh oh! You guessed the wrong number! \n");
printf("The correct guess was %d \n",r);
}
}
else{
printf("That is not a number! \n");
}
}

Is digit expects an ascii character - with '0','1','2','3' ... being values that will return non zero.
Your code is getting the actual value input (as an example 104 which would have been ascii values '1' '0' '4') and setting x to the converted integer value and then seeing if that is a ascii digit value.
You probably want to use the return code of scanf to determine whether it read a numeric value.
If, you were going to use isdigit, you would need to read the value as a string, then use isdigit on each character in the string to verify the are numbers. And then convert the string to a number using sscanf, or atoi.

Related

What should I pass to isdigit() in order for it to work properly? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm really struggling with isdigit();
I need to test for simple numbers, the relevant part of my code looks like this:
printf("Enter a number\n");
scanf("%f", &a);
if (isdigit(a))
printf("That's a number");
doesn't matter what I input, it won't acknowledge it as a number. I saw somewhere an answer to a similar problem, where they said you can only pass the ASCII code to isdigit if you want accurate evaluation (so only 48-57 will work). Instead '2' for example will be interpreted as the 'start of text' I couldn't find any answer on how to get around that though. I don't remember having that problem in cpp.
Any suggestions?
The isdigit function expects a character code and returns true if the character is one of '0' - '9'.
What you are doing by using the %f format specifier is reading in some text which gets interpreted as a floating point and is then parsed to result in the actual value encoded in the system floating point format (probably IEEE754).
You don't want scanf to interpret the values for you. You want to read the text directly. So declare a char array big enough to read your input and use the %s format specifier. This will give you a string. Then inspect each character in the string with isdigit.
isdigit() checks if a single character is a digit or not. You're passing something that's not a single character.
It would be helpful to have a complete code snippet so we could e.g. know the data type of a. Since you've passed %f to scanf(), presumably it's a float?
If so, the scanf() will return 1 if it is able to parse a float out of whatever input it is given. You can use the return value to determine if the input was valid and not have to even use isdigit().
If you want to use isdigit() for some reason, then you should read the input as a string (scanf("%s", ...something that is a char *...)) and then iterate over each character, passing it to isdigit()
The definition for isdigit() is
int isdigit( int ch );
Checks if the given character is one of the 10 decimal digits: 0123456789.
The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF.
Returns:
Non-zero value if the character is a numeric character, zero otherwise.
Since you are passing the input to a float variable it will not work.
#include <stdio.h>
#include <ctype.h>
int main() {
unsigned char c;
printf(">");
scanf(" %c", &c);
if(isdigit(c)){
printf("Is a digit\n");
}
}

Why is scanf always giving me zero for my first input? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm a Java programmer trying to learn C for a class and man, I can't wrap my head around this. There's no reason why this shouldn't work and yet it doesn't. I'm trying to write a simple calculator app, and no matter how I write it, the first number I input (variable a) ends up being 0, but the 2nd one is fine. With 5 + 6 as input, the output is 6. What am I missing?
#include <stdio.h>
int main()
{
long int a, b, c;
char op;
c = 0;
printf("Enter the expression: ");
scanf("%ld %s %ld", &a, &op, &b);
switch(op){
case('+'): c = a+b; break;
case('-'): c = a-b; break;
case('*'): c = a*b; break;
case('/'): c = a/b; break;
default: break;
}
printf("\n%ld", c);
return 0;
}
The scanf function returns the value of the macro EOF if an input failure occurs
before the first conversion (if any) has completed. Otherwise, the function returns the
number of input items assigned, which can be fewer than provided for, or even zero, in
the event of an early matching failure.
So always check if scanf is successful and if the number of items are correctly assigned.
And it is good to know about the conversion specifiers (what follows after % character) and the length modifiers and their meanings in C.
Check out C Committee Draft (N1570) sections 7.21.6.2 The fscanf function and 7.21.6.4 The scanf function and you will get a good idea of how to use scanf.
I am not sure if you are giving 5<space>+<space>6 or 5+6(without spaces) as input. If you are doing the first, then try doing the second one, i.e, number1+number2 without spaces between the characters.
And remove the spaces in the scanf("%ld %s %ld") too.
Hope this helps.

c - how to break scanf with no enter and no string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a project for school and in it, I need to get a four digit number and then continue immediately with no enter. For example: "Enter a number: 1424" and then it just continues, and you can't enter anymore numbers, aw well as Enter key pressing should not be needed.
I tried scanf("%4d",&num); but it waits for Enter key.
And one more restriction is... I can't use strings in this project, so all the solutions must be without strings.
The only way to organize input without Enter key press are functions getch and getche from conio.h header, that I suppose is not in C/C++ standard. So POSIX standard names are _getch and _getche.
With that functions you will read character - which are not strings if you process each char separately.
UPDATE:
My solution is:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int main(void)
{
char ch; // to store an input - single char
int number = 0; // to make number from inputs
printf("Enter a number: ");
int digits_cnt = 0;
while (digits_cnt < 4)
{
ch = _getche();
if (isdigit(ch))
{
number *= 10; // add an order to number
number += ch - '0'; // add a decimal digit to number
digits_cnt++; // count this digit to stop loop
}
}
// just to check result
printf("\nThe number %d was entered.\n", number);
return 0;
}
I assume that all 4 digits should become a number, but, perhaps, you need to do something else with them.
For reading 4 digits you probably want use char * fgets ( char * str, int num, FILE * stream ); that will do exactly what you are asking for. You should not use scanf for interactive input you can find why in this article
To the string part since you are typing input in ascii you are already working with strings(arrays of chars).

Why b.r is not visible while printing? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have this structure after scanning and printing p and q are printed while r is not printed can you please let me know why?
struct book
{
int p;
float q;
char r;
};
int main()
{
struct book b;
scanf("%d%f",&b.p,&b.q);
scanf("%c",&b.r);
printf("%d......%f.....%c",b.p,b.q,b.r);
return 0;
}
Problem :
that's because b.r takes in the \n character entered at the end of the previous scanf() statement
scanf("%d%f",&b.p,&b.q);
Solution :
Avoid it by giving a space before %c in the scanf()
scanf(" %c",&b.r);
Why give a space ?
This would consume if there are any whitespaces (' ' or '\n' or '\0') present in the input stream
Suggestion :
next time when you don't get any output when you print, try printing it's ascii value by casting it to int, that way you'd know what value the variable is taking and see it's corresponding character in ascii table.
printf("%d",(int) b.r);
for example, without making any changes to your code except this to your printf statement :
printf("%d......%f.....%d",b.p,b.q,(int)b.r);
you'd get
input :
2
2
output :
2.....2.....10
why the 10?
because it's the ascii value of \n or the newline character

Write a program in C that inputs one six-digit number and prints the number again with spaces [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Fun puzzle: Write a program in C that inputs one six-digit number from the keyboard and prints the number again and on a new line prints the digits separated from one another by four spaces each. Use combinations of integer division and the remainder operation (% operator)
My original idea was to do a work around with this but it's not the correct way of doing it.
#include <stdio.h>
int main(void)
{
char a,b,c,d,e,f;
printf("Please enter a 6 digit number:");
scanf("%c%c%c%c%c%c",&a,&b,&c,&d,&e,&f);
printf("The number you inputted is:\n");
printf("%c%c%c%c%c%c\n",a,b,c,d,e,f);
printf("The number you entered, with spaces, is:\n");
printf("%c %c %c %c %c %c\n",a,b,c,d,e,f);
return 0;
}
You need to change the logic entirely to conform to the requirement. I'll not provide you any code, but will be happy to assist you with the logic.
Read the integer value as int (or long int, because the safe bet to store 6-digit number need to have at least 32-bit width variable)
Perform modulo-10 operation to get the Least significant digit. print or store.
Divide by 10.
Go back to step 2 until you have looped 5 more times i.e, the division yields 0.

Resources