tring again..

This is a discussion on tring again.. within the C Programming forums, part of the General Programming Boards category; kk before i have posted this code almost the same.. but anywayz im trying to get it to add with ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    Unhappy tring again..

    kk before i have posted this code almost the same.. but anywayz im trying to get it to add with itz not doing what i want it to ... anyone care to tell me what is goin wrong..? thanx
    btw the code is not done yet... it will be a calculator..

    #include <stdio.h>
    #include <math.h>

    int main()

    {
    int x,y;
    char var;



    printf("Type the operator +,-,/,* and your prob in this format 5+5\n",x,y);
    scanf("%d",&x, &y, &var);
    switch(var)
    {
    case '+': printf("Answer = %d\n",x+y);
    break;
    }
    }

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    printf("Type the operator +,-,/,* and your prob in this format 5+5\n",x,y);
    You don't need x and y in here just use
    Code:
    printf("Type the operator +,-,/,* and your prob in this format 5+5\n");
    scanf("%d",&x, &y, &var);
    Your missing the format specifiers for 2 of the variables and also you ask for the format to be 5+5 so the order of your variables should be x var y
    Code:
    scanf(%d%c%d", &x, &var, &y);
    That should get you going you might want to add a default to your switch to allow for an incorrect symbol.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    i wonder why i still cant get it...

    #include <stdio.h>
    #include <math.h>

    int main()

    {
    int x,y;
    char var;

    printf("Type the operator +,-,/,* and your prob in this format 5+5\n");
    scanf("%d",&x,&var,&y);
    switch(var)
    {
    case '+':
    scanf("%d%d%d",&x,&var,&y);
    printf("Answer = %d\n",x+y);
    break;
    default:
    printf(" please try that again\n ");
    break;
    }
    }

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Your almost there you just got an extra scanf you don't need.
    remove the first one
    scanf("%d",&x,&var,&y);
    and move the second scanf to where the first scanf was, also change the second %d to %c, You should end up with
    Code:
    #include <stdio.h> 
    #include <math.h> 
    
    int main() 
    
    { 
       int x,y; 
       char var; 
    
       printf("Type the operator +,-,/,* and your prob in this format 5+5\n"); 
       scanf("%d%c%d",&x,&var,&y); 
       
       switch(var) 
       { 
          case '+': printf("Answer = %d\n",x+y); 
                        break; 
          default : printf(" please try that again\n "); 
                        break; 
       } 
    }
    Now it should definatley work
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    thanx it workd... =) i was creating a calc because i heard that is the best thing to start off ... but after this calc what can i move on to next... that will also help me..

  6. #6
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    i was creating a calc because i heard that is the best thing to start off ... but after this calc what can i move on to next... that will also help me..
    Well the best thing is work from a book, these set you exercises which will give a sense of acheviement when you accomplish them and keep your interest alive.
    so good titles have been suggested here
    Apart from that you need to find a particular area which you enjoy the most and research that
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    what would you recomend for a newbie(not so smart)like me... i love this cboard i really enjoy it this is why i post alot.. but if you know of a place were i can go for linux mostly that i can ask question and enjoy it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error when tring to run program
    By hopeolicious in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2005, 12:06 AM
  2. tring to sort file along with converting it from char to int
    By tony24tone in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2004, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21