-1

I'm planning a function that takes the user's input (4 integers) and checks if each integer is bigger than 1 and smaller than 6, I wanted something simple, and thought that if the function 'getche()' (I want to use this particular function because I don't want the user to type the 'enter' key after he gives the input) could get four integers in one code.

I want to avoid this(if possible):

int num1 = 0, num2 = 0, num3 = 0, num4 = 0;
num1 = getche();
num2 = getche(); ...

And I was thinking if something like this is possible:

int num = 0;
num = getche(4)

Thanks.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
Ma250
  • 355
  • 1
  • 4
  • 11
  • Just try it. Come back when you have tried something and have a specific problem. And if in doubt, read the man page/reference for that function. It will tell you exactly what parameters it accepts and what the parameters mean (hint: `getche` does not take any parameters). – kaylum Jan 03 '17 at 21:14
  • You need to include an [mcve] showing what you've already tried ...but that's not how `getche()` works anyway. – Rob K Jan 03 '17 at 21:22
  • Can't you just use [scanf](https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm)? [Example here](http://stackoverflow.com/a/1412524/5601284) – byxor Jan 03 '17 at 21:51
  • @BrandonIbbotson no: "I don't want the user to type the 'enter' key". – Weather Vane Jan 03 '17 at 23:42
  • How are you getting your input then? Maybe this will help: [Scanf on non-stdin input](http://stackoverflow.com/questions/6922253/scanf-on-non-stdin-input). – byxor Jan 04 '17 at 00:06

2 Answers2

1

Couldn't you just use a loop with appropriate conditions and getch() function? It might turn out to be easier.

 int l = 0;
 while (l < 4)
 {
   x = getch();
  // your conditions
 }
Barmar
  • 741,623
  • 53
  • 500
  • 612
Majki
  • 46
  • 4
  • I tried, but it got messier when I tried to check if the parameters are meeting the conditions. – Ma250 Jan 03 '17 at 21:17
  • 1
    Then make a 4-elements bool array. After every getch() check if the read parameter is okay. If it's alright then fill appropriate id of your array with 'true', otherwise with 'false'. – Majki Jan 03 '17 at 21:23
  • I'm not allowed to make any arrays though – Ma250 Jan 04 '17 at 15:20
0

use a do/while loop to get the digits accepting only 1 through 6. A while loop to concatenate the four digits into an integer.

#include <stdio.h>
#include <stdlib.h>
//#include <conio.h>

int main(void)
{
    int num = 0;
    int digit = 0;
    int each = 0;

    printf("Type a four digit number using only 1 through 6\n");
    while ( each < 4) {
        do {
            digit = getchar ( );
            //digit = getch ( );//getch does not echo the typed character
            if ( digit == EOF) {
                fprintf ( stderr, "EOF\n");
                return 1;
            }
        } while ( digit < '1' || digit > '6');//loop if character is NOT 1 to 6
        //putch ( digit);//echo the character
        each++;
        num *= 10;
        num += digit - '0';
    }
    printf("the number is %d\n", num);

    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16