-4

i just want to put more than one character through the (get char) or by any other way, this is a simple calculator program using switch cases condition but cant choose a case with more than one input to the getch function for ex: 10,11....

#include <stdio.h>
#include <conio.h>
void plus_func();
void menus_func();
void mul_func();
void div_func();
void modulus_func();
void shiftleft_func();
void shiftright_func();
void and_func();
void or_func();
void not_func();
void doubleand_func();
void doubleor_func();
void notequal_func();
void xor_func();
void main(void){

int x,y, res;
int m;
int op1;
int op2;
int op3;
int i=0;
int k;
while(i==0){
 printf("Enter any key to start the calculator function program or escape to exit it \n");
 k=getch();
 if(k!=0x1b)
{
 printf("enter the first num or press escape to exit \n"); 
 x=getch();
 if(x==0x1b)
 {
    break;
 }
 else
 {
    op1=x-48;
 }
  printf("enter the second num or press escape to exit \n"); 
  y=getch();

  if(y==0x1b)
  {
    break;
  }
  else
  {
    op2=y-48;
  }
  printf("enter the type of the operation \n",m);
  m=getch();
  if(m==0x1b)
  {
    break;
  }
  else
  {
    op3=m-48;
  }
  switch(op3)
  {

     case 1: plus_func(op1,op2);break;

     case 2: menus_func(op1,op2);break;

     case 3: mul_func(op1,op2);break;

     case 4: div_func(op1,op2);break;

     case 5: modulus_func(op1,op2);break;

     case 6: shiftleft_func(op1,op2);break;

     case 7: shiftright_func(op1,op2);break;

     case 8: and_func(op1,op2);break;

     case 9: or_func(op1,op2);break;

     case 10: not_func(op1,op2);break;

     case 11: doubleand_func(op1,op2);break;

     case 12: doubleor_func(op1,op2);break;

     case 13: notequal_func(op1,op2);break;

     case 14: xor_func(op1,op2);break;

     default: printf("error");
     break;
  }
}
  else
  {
    break;
  }
}
}

  void plus_func(int a, int b)
{
    int c;
    c=a+b;
    printf("the result is %d \n",c);
}

  void menus_func(int a, int b)
{
    int c;
    c=a-b;
    printf("the result is %d \n",c);
}

  void mul_func(int a, int b)
{
    int c;
    c=a*b;
    printf("the result is %d \n",c);
}
  void div_func(int a, int b)
{
    int c;
    c=a/b;
    printf("the result is %d \n",c);
}
  void modulus_func(int a, int b)
{
    int c;
    c=a%b;
    printf("the result is %d \n",c);
}
  void shiftleft_func(int a, int b){
    int c;
    c=a<<b;
    printf("the result is %d \n",c);
}
  void shiftright_func(int a, int b)
{
    int c;
    c=a>>b;
    printf("the result is %d \n",c);
}
  void and_func(int a, int b)
{
    int c;
    c=a&b;
    printf("the result is %d \n",c);
}
  void or_func(int a, int b)
{
    int c;
    c=a|b;
    printf("the result is %d \n",c);
}
  void not_func(int a, int b)
{
    int c;
    c=a+b;
    c=~c;
    printf("the result is %d \n",c);
}
  void doubleand_func(int a, int b)
{
    int c;
    c=a&&b;
    printf("the result is %d \n",c);
}
  void doubleor_func(int a, int b)
{
    int c;
    c=a||b;
    printf("the result is %d \n",c);
}
  void notequal_func(int a, int b)
{
    int c;
    c=a+b;
    c=!c;
    printf("the result is %d \n",c);
}
  void xor_func(int a, int b)
{
    int c;
    c=a^b;
    printf("the result is %d \n",c);
}
  • Are you *required* to use `getch`? Otherwise, a simple `scanf("%d")` will suffice. – Spikatrix Feb 14 '16 at 12:49
  • `void plus_func();` etc. are no valid prototypes and use deprecated syntax. C is not C++! TL;DR. Also format & indent your code properly. (Hint: Your code has strong potential to invoke undefined behaviour). – too honest for this site Feb 14 '16 at 12:53
  • @Olad They aren't “invalid prototypes” but rather declarations without a prototype. Yes, that's deprecated but it's not wrong in any way. – fuz Feb 14 '16 at 13:26
  • unfortunately im resitricted with the getch function is there any other way @GillBates – Dravon Yossri Feb 19 '16 at 09:08

3 Answers3

0
scanf("%d",&k);

is what you need.

More safe:

scanf_s("%d",&k);
Van Tr
  • 5,889
  • 2
  • 20
  • 44
  • Could you explain how `scanf_s("%d")` is more safer than `scanf("%d")`? – Spikatrix Feb 14 '16 at 15:56
  • @CoolGuy http://stackoverflow.com/questions/21434735/difference-between-scanf-and-scanf-s – Van Tr Feb 14 '16 at 16:09
  • i wrote this code to be exit if the escape button is pressed ,escape represents(0x1b) in binary, and im checking on any input and before the program starts to exit from the program any time you want that is one of the purpose of the program. – Dravon Yossri Feb 16 '16 at 13:36
  • i had to make all my inputs by getch(), and then gets its ascii value and returns it back to decimal by (-48) to check the decimal value in the switch case, the problem is in the getch i cant put more than one value only from (0------9). – Dravon Yossri Feb 16 '16 at 13:36
0

Well, getch () cannot take more than two characters. You can use getline (), but I don't think that is what you need. You are reading numbers, not characters. So do

scanf ("%d", &k);

or use scanf_s as mentioned in @DivinCodino's answer. Also, still if you want to read characters, use getline as mentioned above or use scanf:

scanf (" %c", &k);

and declare k as

char k;

Also, your current program will not work properly as it is trying to read integers using getch, which is designed only for reading characters.

After making the changes above, the code is:

#include <stdio.h>
#include <conio.h>
void plus_func();
void menus_func();
void mul_func();
void div_func();
void modulus_func();
void shiftleft_func();
void shiftright_func();
void and_func();
void or_func();
void not_func();
void doubleand_func();
void doubleor_func();
void notequal_func();
void xor_func();
void main(void){

int x,y, res;
int m;
int op1;
int op2;
int op3;
int i=0;
int k;
while(i==0){
 printf("Enter any key to start the calculator function program or escape to exit it \n");
 k=getch();
 if(k!=0x1b)
{
 printf("enter the first num or press escape to exit \n"); 
 scanf ("%d", &x);
 if(x==0x1b)
 {
    break;
 }
 else
 {
    op1=x-48;
 }
  printf("enter the second num or press escape to exit \n"); 
  scanf ("%d", &y);

  if(y==0x1b)
  {
    break;
  }
  else
  {
    op2=y-48;
  }
  printf("enter the type of the operation \n",m);
  scanf ("%d", &m);
  if(m==0x1b)
  {
    break;
  }
  else
  {
    op3=m-48;
  }
  switch(op3)
  {

     case 1: plus_func(op1,op2);break;

     case 2: menus_func(op1,op2);break;

     case 3: mul_func(op1,op2);break;

     case 4: div_func(op1,op2);break;

     case 5: modulus_func(op1,op2);break;

     case 6: shiftleft_func(op1,op2);break;

     case 7: shiftright_func(op1,op2);break;

     case 8: and_func(op1,op2);break;

     case 9: or_func(op1,op2);break;

     case 10: not_func(op1,op2);break;

     case 11: doubleand_func(op1,op2);break;

     case 12: doubleor_func(op1,op2);break;

     case 13: notequal_func(op1,op2);break;

     case 14: xor_func(op1,op2);break;

     default: printf("error");
     break;
  }
}
  else
  {
    break;
  }
}
}

  void plus_func(int a, int b)
{
    int c;
    c=a+b;
    printf("the result is %d \n",c);
}

  void menus_func(int a, int b)
{
    int c;
    c=a-b;
    printf("the result is %d \n",c);
}

  void mul_func(int a, int b)
{
    int c;
    c=a*b;
    printf("the result is %d \n",c);
}
  void div_func(int a, int b)
{
    int c;
    c=a/b;
    printf("the result is %d \n",c);
}
  void modulus_func(int a, int b)
{
    int c;
    c=a%b;
    printf("the result is %d \n",c);
}
  void shiftleft_func(int a, int b){
    int c;
    c=a<<b;
    printf("the result is %d \n",c);
}
  void shiftright_func(int a, int b)
{
    int c;
    c=a>>b;
    printf("the result is %d \n",c);
}
  void and_func(int a, int b)
{
    int c;
    c=a&b;
    printf("the result is %d \n",c);
}
  void or_func(int a, int b)
{
    int c;
    c=a|b;
    printf("the result is %d \n",c);
}
  void not_func(int a, int b)
{
    int c;
    c=a+b;
    c=~c;
    printf("the result is %d \n",c);
}
  void doubleand_func(int a, int b)
{
    int c;
    c=a&&b;
    printf("the result is %d \n",c);
}
  void doubleor_func(int a, int b)
{
    int c;
    c=a||b;
    printf("the result is %d \n",c);
}
  void notequal_func(int a, int b)
{
    int c;
    c=a+b;
    c=!c;
    printf("the result is %d \n",c);
}
  void xor_func(int a, int b)
{
    int c;
    c=a^b;
    printf("the result is %d \n",c);
}

This code still does not work, but I haven't done any changes except changing getch to scanf.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
  • 1-yup cuz i wrote this code to be exit if the escape button is pressed ,escape represents(0x1b) in binary, and im checking on any input and before the program starts to exit from the program any time you want that is one of the purpose of the program. – Dravon Yossri Feb 16 '16 at 13:20
  • 2- i had to make all my inputs by getch(), and then gets its ascii value and returns it back to decimal by (-48) to check the decimal value in the switch case, the problem is in the getch i cant put more than one value only from (0------9). – Dravon Yossri Feb 16 '16 at 13:28
0

This function will capture a number of digits and concatenate them into a value. To conserve space only the plus_func is include in the code and switch. The function accepts the number of digits, a sign flag and a pointer to the value. It returns an integer to indicate that ESC was pressed.

With this, each input is a fixed width. To input 3, 03 must be typed.

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

int getint ( unsigned int digits, int sign, int *value) {
    int ch = 0;
    unsigned int first = digits;

    *value = 0;//set to zero

    while ( digits) {
        while ( ( ch = getch()) < '0' || ch > '9') {
            if ( ch == '-' && sign == 1 && first == digits) {// allow for negative numbers
                sign = -1;
                putchar ( '-');
            }
            if ( ch == 0x1b) {
                return 1;
            }
        }
        putchar ( ch);
        *value *= 10;
        *value += ch - '0';//concatenate the digits
        digits--;
    }
    if ( sign) {
        *value *= sign;
    }
    return 0;
}

void plus_func(int a, int b)
{
    int c = 0;
    c = a + b;
    printf ( "\nthe result is %d\n", c);
}

int main()
{
    int i = 0;
    int op1 = 0;
    int op2 = 0;
    int op3 = 0;
    unsigned int digits = 2;

    do {
        printf("\nenter the first %u digit num or press escape to exit \n", digits);
        if ( getint ( digits, 1, &op1)) {
            break;
        }

        printf("\nenter the second %u digit num or press escape to exit \n", digits);
        if ( getint ( digits, 1, &op2)) {
            break;
        }

        printf("\nenter the type of the operation from 01 to 14\n");
        if ( getint ( digits, 0, &op3)) {
            break;
        }

        switch ( op3) {
            case 1:
                plus_func ( op1, op2);
                break;

            default:
                printf ( "error\n");
                break;
        }

    } while ( i == 0);
    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16