Shit code aint working QQ

Discussion in 'Homework' started by DarkMessiah7, Jan 21, 2016.

  1. DarkMessiah7

    DarkMessiah7 Well-Known Member

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int computeraki(int,int,char);
    main()
    {
          int a,b;
          char ch;
          
          printf("dwse tous akereous a,b : ");
          printf("\n");
          scanf("%d%d",&a,&b);
          printf("epelekse praksh ");
          scanf("%c",&ch);
          computeraki(a, b,ch);   
          
    system("pause");
    }
    int computeraki(int a, int b,char ch)
    {
        switch(ch)
        {
                 case '1'; : if (ch='+'):
                      
                          printf("%d",a+b);
                          break;
                 case '2'; : if (ch='-'):
                      
                          printf("%d",a-b);
                          break;
                 case '3'; : if (ch='*'):
                      
                          printf("%d",a*b);
                          break;
                 case '4'; : if (ch='/'):
                      
                          printf("%d",a/b):
                          break;
                 default : printf("lathos epilogh");
        }
    }
    
    Any ideas ? Im stuck and i cant think anymore today.
     
  2. HHHNNNGGG

    HHHNNNGGG Well-Known Member

    Your switch-case is weird imo. You already use variable ch as switch, but then you use if with ch in each case. Furthermore there are many syntax errors.

    I'm using phone atm, and I don't know what you are trying to achieve, so I can't fix it. But for example:

    Switch (a){
    case 1:
    if(b==6) c=a+b;
    break;​
    case 2:
    if(b==2) c=a-b;
    break;​
    }
     
  3. Ety

    Ety Well-Known Member

    That code is indeed very weird. You don't seem to understand how switch statement works.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int computeraki(int,int,char);
    
    int main()
    {
    	int a,b;
    	char ch;
    	  
    	printf("dwse tous akereous a,b :\n");
    	scanf("%d %d",&a,&b);
    	getchar();
    	printf("epelekse praksh ");
    	scanf("%c",&ch);
    	computeraki(a, b,ch);   
    	printf("\n");
          
    	system("pause");
    	return 0;
    }
    
    int computeraki(int a, int b,char ch)
    {
        switch(ch)
        {
    		 case '+':
    			  printf("%d",a+b);
    			  break;
    		 case '-':
    			  printf("%d",a-b);
    			  break;
    		 case '*':
    			  printf("%d",a*b);
    			  break;
    		 case '/':
    			  printf("%d",a/b);
    			  break;
    		 default : printf("lathos epilogh");
        }
    }
    Notice the getchar(). Without it, it won't work, because scanf("%c") will interpret ENTER key as the newline character, storing it into ch. You could just input all three variables in a single line, like this:

    Code:
    scanf("%d%c%d",&a,&ch,&b);
    Then you can write something like: 8*4, but not 8 * 4.
     
  4. DarkMessiah7

    DarkMessiah7 Well-Known Member

    ty i'll probably fix it tonight and study some more. :D
     
  5. Blarrg

    Blarrg Well-Known Member

    characters and character arrays can be a pain in the butt in C.

    Also I recommend using the <iostream> library, so you can use >> to get input from the keyboard instead, and also use << to output information


    Example use:

    [nqb]#include <iostream>


    int x;
    char y;

    cin >> x; // this will take all input from the keyboard until the user presses enter and store it in x

    cin >> y; // same as above, but will store as a character

    //The following line outputs as follows:
    //Integer: <x>
    //Character: <y>
    cout << "Integer: " << x << endl << "Character: " << y << endl;[/nqb]


    The functions are overloaded to work for all the data types so they are easy to work with.