I was starting off with making the game " Cows and Bulls".
And to start off i am having a problem in inputting the 4 digit number in a mannar that it appears in form of "*" on the screen. similar to a password.

And each digit should be separately inputted. Similar to the following...

int num[4],i=0;
printf("\nEnter the four digit no. : ");
for(i=0;i<4;i++)
{
 scanf("%d",&num[i];
}

the problem with code is that :
1. The 4 digit no. should be inputted without a pause.
2. The digit should not be displayed on the screen.

So i tried this...

char a;
int num[4];
printf("\nEnter the four digit no. : ");
for(i=0;i<4;i++)
{
 a=getch();
 printf("*");
 sscanf(a,"%d",num[i]);
}

Whts the problem with this code? How do i convert the string character into a number?

>>How do i convert the string character into a number?
you don't need sscanf() -- just subtract '0' from the ascii digit. Make sure the ascii char is a numeric digit before conversion.

char a = '1';
int num;
if( isdigit(a) )
   num = a - '0';
else
   num = 0;

>>for(i=0;i<4;i++)
what happens if I type 5 digits, or 100 digits? or a a bunch of random keys? Don't expect peope to do what you tell them to in the printf() statement.

Whts the problem with this code? How do i convert the string character into a number?

That's not necessary:
When you use getch() you get a one char. You want to store this char as an int in an intarray. So just change scanf("%d",&num[i]; to num[i] = a - 48; and it should work! Just make sure 'a' is a valid input.

Edited 6 Years Ago by Nick Evan: n/a

what happens if I type 5 digits, or 100 digits? or a a bunch of random keys? Don't expect peope to do what you tell them to in the printf() statement.

I was going to code to fix that as soon as my minor problem was solved :), which now is, Thnx

Also, if you want the user to enter one 4-digit number you don't have to put each digit in an array, unless you need each digit for something else. If you just want one integer, this is a quick way of doing it (without error checking)

int num = 0;
for(i=0;i<4;i++)
{
 a=getch();
 printf("*");
  num = (num * 10) + a - '0';
}

T num[i] = a - 48;

I know it doesn't really matter to the compiler whether you subtract 48 or subtract '0', but I think '0' is more intuitive

Also, if you want the user to enter one 4-digit number you don't have to put each digit in an array, unless you need each digit for something else. If you just want one integer, this is a quick way of doing it (without error checking)

I do need a separate digit input :)

I know it doesn't really matter to the compiler whether you subtract 48 or subtract '0', but I think '0' is more intuitive

Me too, its easier to check for an error

@ Ancient Dragon : Congratulations from my side too and here's what I coded for solving the problem u mentioned earlier :

what happens if I type 5 digits, or 100 digits? or a a bunch of random keys? Don't expect peope to do what you tell them to in the printf() statement.

int flag=0;
 while(flag==0)
 {
  flag=1; 
  printf("\n\nPlayer1 pls enter the 4 digit no. ( digits should not be repeated and each digit must be 0-9)...\n");
  for(i=0;i<4;i++)
  { 
   a=getch();
   printf("*");
   if( isdigit(a) )
    num[i] = a - '0';
   else
    flag=0;
   for(m=0;m<i;m++)
   {
    if(num[m]==num[i])
    { flag=0;}
   }
  }
  if(flag==0)
  {
   printf("\n\nPlease enter valid number!");
  }
  getch();
 }

Consider putting a continue statement after the user enters a non digit character since there is no point in peforming undue looping when you know you haven't stored the character anyways...

And btw, consider improving on your code indentation, would really make the code more readable. Try not putting the opening and closing braces on the same line.

Don't do something like: if( condition ) { break ; } Something like this would be more like it:

if( condition )
{
     break ;
}

Consider putting a continue statement after the user enters a non digit character since there is no point in peforming undue looping when you know you haven't stored the character anyways...

right...

And btw, consider improving on your code indentation, would really make the code more readable. Try not putting the opening and closing braces on the same line.
Don't do something like: if( condition ) { break ; } Something like this would be more like it:

if( condition )
{
     break ;
}

I have trouble reading the code myself...lol. I sometimes overlook indenting while writing code since i am so eager to go on continuing with the next thing...

I sometimes overlook indenting

What IDE are you using? If you're using VS2005 express (free) just do:
CRTL-A CTRL-K CTRL-F
and your code is formatted

Regards Niek

This article has been dead for over six months. Start a new discussion instead.