main (int argc,char *argv[])

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    #1

    main (int argc,char *argv[])

    I have been having a little trouble with this page.


    I wanted to create a command called div that takes two parameters a
    numerator and denomenator that works as such:

    div 3 15

    and the return should be 5. The coding on this page is pretty poor IMO.
    Atleast I'm having trouble reading it so that's nothing really new. Does a
    for loop always have to be used in the command line situation?

    Bill


  • Bill Cunningham

    #2
    Re: main (int argc,char *argv[])


    [snipe]

    For example:
    >
    #include <stdlib.h>
    #include <stdio.h>
    >
    int main(int argc, char **argv)
    {
    if (argc < 3)
    {
    printf("Usage: div numerator denominator\n") ;
    }
    else
    {
    double numerator = strtod(argv[1], (char **) NULL);
    double denominator = strtod(argv[2], (char **) NULL);
    >
    OK John right here. Instead of the char** cast could I just use as
    main's parameter,

    int argc, double* argv[]) ? Does it have to be a pointer to pointer to char
    ?

    Bill


    Comment

    • Bill Cunningham

      #3
      Re: main (int argc,char *argv[])

      Hi Bill-
      Consider that stdlib declares..
      >
      typedef struct {
      int quot;
      int rem;
      } div_t;
      >
      #include <stdio.h>
      #include <stdlib.h>
      >
      int main(void) {
      int x = 15, y = 3;
      div_t dv = div(x, y);
      printf("%d %d\n", dv.quot, dv.rem);
      return 0;
      }
      >
      Oh Thanks Joe. Here's what I was doing and it was obviously wrong.

      div_t dv;
      dv=div(15,3);
      printf("%d%d",d v(15,3),dv.quot ,dv.rem);

      I see now.

      Bill


      Comment

      • Joe Wright

        #4
        Re: main (int argc,char *argv[])

        Bill Cunningham wrote:
        >Hi Bill-
        >Consider that stdlib declares..
        >>
        > typedef struct {
        > int quot;
        > int rem;
        > } div_t;
        >>
        >#include <stdio.h>
        >#include <stdlib.h>
        >>
        >int main(void) {
        > int x = 15, y = 3;
        > div_t dv = div(x, y);
        > printf("%d %d\n", dv.quot, dv.rem);
        > return 0;
        >}
        >>
        Oh Thanks Joe. Here's what I was doing and it was obviously wrong.
        >
        div_t dv;
        dv=div(15,3);
        printf("%d%d",d v(15,3),dv.quot ,dv.rem);
        >
        I see now.
        >
        Bill
        >
        >
        Glad to be of help. I know you have a Space bar because you used it once
        in the above snippet. If you use it more often your code might be as
        pretty as mine. :-)

        --
        Joe Wright
        "Everything should be made as simple as possible, but not simpler."
        --- Albert Einstein ---

        Comment

        Working...