Like Tree3Likes
  • 1 Post By stahta01
  • 2 Post By anduril462

What's wrong with the code(Calculator program)

This is a discussion on What's wrong with the code(Calculator program) within the C Programming forums, part of the General Programming Boards category; I have made this program but it is not giving the output. please help me. I use turbo c++ compiler: ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    What's wrong with the code(Calculator program)

    I have made this program but it is not giving the output. please help me. I use turbo c++ compiler:
    Code:
    #include<conio.h>
    #include<stdio.h>
    void main(void)
    {
    int num[2];
    char ch[5];
    printf("Enter operator \n + for addition,\n - for subtratcion,\n * for multiplication,\n / for division");
    scanf("%c",&ch[5]);
    printf("Enter number1,\n Enter number2");
    scanf("%d",&num[2]);
    switch(ch[5])
    {
    case '+':
    printf("%d+%d=%d",num[0]+num[1]);
    break;
    case '-':
    printf("%d-%d=%d",num[0]-num[1]);
    break;
    case '*':
    printf("%d * %d=%d",num[0]*num[1]);
    break;
    case'/':
    printf("%d / %d=%d",num[0]/num[1]);
    }
    getch();
    }
    Last edited by rizwan; 10-19-2011 at 10:52 AM.

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Why are you using arrays for everything, just use 2 ints and a char.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You dont know array bounds, you dont know scanf formats.
    Therefore your program not works.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    I want to use array in this program
    Could you please correct the mistake

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,703
    Quote Originally Posted by rizwan View Post
    I want to use array in this program
    Could you please correct the mistake
    OK, You no longer want to use an array in this program.

    Tim S.
    CommonTater likes this.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    code corrected , but wrong output
    Code:
    #include<conio.h> 
    #include<stdio.h> 
    void main(void) 
    { 
    int num[2]; 
    char ch[5]; 
    printf("Enter operator \n + for addition,\n - for subtratcion,\n * for multiplication,\n / for division"); 
    scanf("%c",&ch[5]); 
    printf("Enter number1"); 
    scanf("%d",&num[0]);
    printf("Enter number2");
    scanf("%d",num[1]); 
    switch(ch[5]) 
    { 
    case '+': 
    printf("%d+%d=%d",num[0]+num[1]); 
    break; 
    case '-': 
    printf("%d-%d",num[0]-num[1]); 
    break; 
    case '*': 
    printf("%d * %d",num[0]*num[1]); 
    break; 
    case'/': 
    printf("%d / %d",num[0]/num[1]); 
    } 
    getch(); 
    }

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,579
    ch is an array of 5 chars. That means valid indexes are 0-4, so ch[5] does not exist. Why do you even have an array of chars when you only read in one for an operator?

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Code:
    #include<conio.h>  
    #include<stdio.h>  
    void main(void)  
    {  
    int num[2];  
    char op;  
    printf("Enter operator \n + for addition,\n - for subtratcion,\n * for multiplication,\n / for division");  
    scanf("%c",&op);  
    printf("Enter number1");  
    scanf("%d",&num[0]); 
    printf("Enter number2"); 
    scanf("%d",num[1]);  
    switch(op)  
    {  
    case '+':  
    printf("%d+%d=%d",num[0]+num[1]);  
    break;  
    case '-':  
    printf("%d-%d",num[0]-num[1]);  
    break;  
    case '*':  
    printf("%d * %d",num[0]*num[1]);  
    break;  
    case'/':  
    printf("%d / %d",num[0]/num[1]);  
    }  
    getch();  
    }
    I changed the operator. Still no results

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,579
    It's int main(void) and you need to return an int at the end, usually zero. Read this: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]).

    You should avoid conio.h and getch() where possible since they're not portable.

    Fixing those few things, here's what happens when I compile:
    Code:
    $ gcc -Wall calc.c
    calc.c: In function ‘main’:
    calc.c:12: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    calc.c:16: warning: too few arguments for format
    calc.c:19: warning: too few arguments for format
    calc.c:22: warning: too few arguments for format
    calc.c:25: warning: too few arguments for format
    If you don't see the same warnings, turn up the warning levels on your compiler. If you do, and ignore them, then shame on you. The compiler is smarter than you, and it's telling you you're doing it wrong. You forgot an & in
    Code:
    scanf("%d", &num[1]);
    The rest are because you're not using printf correctly. Each %d needs to have it's own integer expression in the parameter list:
    Code:
    printf("%d + %d = %d", num[0], num[1], num[0]+num[1]);
    Fix the others accordingly.
    stahta01 and Salem like this.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You dont know, how scanf works.
    You dont know the difference between pointer, array and arrayelements.
    You should read a book about C.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rizwan View Post
    I use turbo c++ compiler:
    And that's your biggest mistake...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's wrong with this selection sort program code
    By suryak in forum C Programming
    Replies: 14
    Last Post: 05-20-2011, 10:23 AM
  2. Replies: 13
    Last Post: 04-26-2011, 03:02 AM
  3. What's wrong with my calculator?
    By andrewmin in forum C Programming
    Replies: 3
    Last Post: 09-08-2010, 02:30 PM
  4. Replies: 3
    Last Post: 03-16-2010, 02:29 PM
  5. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM

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