Program not working

This is a discussion on Program not working within the C Programming forums, part of the General Programming Boards category; Hi guys, I have this program that doesn't work, can someone plz help me. Write a program using command line ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Program not working

    Hi guys, I have this program that doesn't work, can someone plz help me.

    Write a program using command line arguments to perform arithmetic calculation, e.g. (1) to add: $ a.out 2.35 + 1.47 (2) to subtract: $ a.out 2.35 – 1.47
    (3) to divide: $ a.out 2.35 / 1.47.

    However to do multiplication you have to use
    $ a.out 2.35 “*” 1.47 Notice the “ “ is used for the multiplication symbol otherwise, it will treated as a wildcat that matches everything.

    2.35 and 1.47 are treated as string. To convert a string to floating point numbers, use atof(). You should print the result of the calculation from your program. In your program, (1) use a function to perform the calculation. In this function, use switch..case statement to make decision as to what operation to perform and return the result. (2) use a function to print the result. Make sure you provide checking for “invalid operation” and provide checking for “divide by zero”.
    And here is the code that I wrote

    Code:
    #include<stdio.h>
    
    float calcs(char,float arg[],float *ptr);
    void print_result(int result);
    float add(float argv[],float *ptr);
    float sub(float argv[],float *ptr);
    float mul(float argv[],float *ptr);
    float div(float argv[],float *ptr);
    
    int main(int argc, char *argv[])
     {
      float converted[argc];
      float result;
      float *ptr;
      ptr=result;
      char oper[2];
      char ch;
      
      for(ctr=1;ctr<argc;ctr++)
       {
        converted[ctr]=atof(argv[ctr]);
       }
      strcpy(oper,argv[2]);
      ch=oper[0];
      calcs(ch,converted[argc],*ptr);
      print_result(result);
      return 0;
     }
    
    float calcs(char ch, float argv[],float *ptr)
     {
      switch(ch)
        {
         case'+': add(argv,ptr);
                  break;
         case'-': sub(argv,ptr);
                  break;
         case'*': mul(argv,ptr);
                  break;
         case'/': div(argv,ptr);
                  break;
         default: printf("Invalid Operation\n");
                  break;
        }
      }
    
    void print_result(float result)
      {
       printf("The result is:%f\n",result);
      } 
     
    void add(float argv[],float *ptr)
     {
      ptr=argv[1]+argv[3];
     }
    void sub(float argv[], float *ptr)
     {
      ptr=argv[1]-argv[3];
     }
    void mul(float argv[], float *ptr)
     {
      ptr=argv[1]*argv[3];
     }
    void div(float argv[], flloat *ptr)
     {
      if(argv[3]!=0)
       {
      ptr=argv[1]/argv[3];
       }
      else
       {
        printf("Number should not be zero");
       }
     }
    And here are the errors I am getting

    Code:
    assgn7b.c: In function `main':
    assgn7b.c:16: incompatible types in assignment
    assgn7b.c:20: `ctr' undeclared (first use in this function)
    assgn7b.c:20: (Each undeclared identifier is reported only once
    assgn7b.c:20: for each function it appears in.)
    assgn7b.c:26: incompatible type for argument 2 of `calcs'
    assgn7b.c:26: incompatible type for argument 3 of `calcs'
    assgn7b.c: At top level:
    assgn7b.c:49: conflicting types for `print_result'
    assgn7b.c:5: previous declaration of `print_result'
    assgn7b.c:54: conflicting types for `add'
    assgn7b.c:6: previous declaration of `add'
    assgn7b.c: In function `add':
    assgn7b.c:55: incompatible types in assignment
    assgn7b.c: At top level:
    assgn7b.c:58: conflicting types for `sub'
    assgn7b.c:7: previous declaration of `sub'
    assgn7b.c: In function `sub':
    assgn7b.c:59: incompatible types in assignment
    assgn7b.c: At top level:
    assgn7b.c:62: conflicting types for `mul'
    assgn7b.c:8: previous declaration of `mul'
    assgn7b.c: In function `mul':
    assgn7b.c:63: incompatible types in assignment
    assgn7b.c: At top level:
    assgn7b.c:65: syntax error before "flloat"
    assgn7b.c:66: conflicting types for `div'
    assgn7b.c:9: previous declaration of `div'
    assgn7b.c: In function `div':
    assgn7b.c:67: `argv' undeclared (first use in this function)
    assgn7b.c:69: `ptr' undeclared (first use in this function)
    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Well.. start with the first error..

    assgn7b.c:16: incompatible types in assignment

    Code:
      float result;
      float *ptr;
      ptr=result;
    That should be ptr = &result. Or rather have ptr hold the address of result. ptr wants an address. And in order to get the address of result, we use the address of (&) operator

    There's a freebie, now you can do the rest.. Just go through one at a time. Use the line number the compiler gives you and correct it.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    HI I have fixed some problems, but I can't figure out some of them. Can someone please point me what's wrong. Thanks


    Here are the errors I am getting now.

    Code:
    assgn7b.c: In function `main':
    assgn7b.c:23: array subscript is not an integer
    assgn7b.c:23: array subscript is not an integer
    assgn7b.c:27: incompatible type for argument 2 of `calcs'
    assgn7b.c:27: incompatible type for argument 3 of `calcs'
    assgn7b.c: In function `add':
    assgn7b.c:56: incompatible types in assignment
    assgn7b.c: In function `sub':
    assgn7b.c:60: incompatible types in assignment
    assgn7b.c: In function `mul':
    assgn7b.c:64: incompatible types in assignment
    assgn7b.c: In function `div':
    assgn7b.c:70: incompatible types in assignment

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you even read your compiler's warnings? I absolutely hate teaching people how to think for themselves.

    1) ctr is declared where exactly in your program?
    Code:
    for(ctr=1;ctr<argc;ctr++)
       {
        converted[ctr]=atof(argv[ctr]);
       }
    2) Ah, well let's assume you've "fixed" that by declaring it a float, since you haven't actually posted your latest code:
    assgn7b.c:23: array subscript is not an integer
    Hm... I wonder what that could mean? Could it mean that you're indexing your array using something that isn't an integer? Why, yes, I bet it does! And how do you think one could fix the problem of indexing with a non-integer in a place where it requires an integer?

    Now use your brain, and work through the rest of the incredibly hard to figure out messages.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    quzah u make me laugh ....thanks though



    One last question hehe...

    What does this error means?....
    Code:
    assgn7b.c: In function `add':
    assgn7b.c:56: incompatible types in assignment
    Thanks
    Last edited by jat421; 03-20-2005 at 08:17 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    Originaly posted by jat421

    Code:
    quzah u make me laugh ....thanks though
    
    
    
    One last question hehe...
    
    What does this error means?.... 
    
    Code:
    
    assgn7b.c: In function `add':
    assgn7b.c:56: incompatible types in assignment
    
    
    Thanks
    haeee check out this

    Code:
    void add(float argv[],float *ptr)
     {
      ptr=argv[1]+argv[3];
     }
    how can u do this. u are expected to assign the resulting value of
    Code:
    argv[1]+argv to *ptr rather than just ptr which gives u someother meaning in this case
    try out this
    Code:
    void add(float argv[],float *ptr)
     {
      *ptr=argv[1]+argv[3];
     }
    s.s.harish

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Thanks a lot that worked . Thanks Harish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 11:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM

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