simple output input ....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chutsu@gmail.com

    simple output input ....

    Hi
    Ok...I got a problem in reading a file containning information, and
    trying to send it to a function...
    the file containing information has format:
    Gas 10000 Home Gas Meter
    Electricity 3000 Home Electricity Meter
    Flight 8000 Holiday Abroad
    Flight 1000 Long Weekend
    Car 1250 Own Car (10000 miles)
    Car 500 Holiday Hire Car

    I would like to put the first column to be stored in an array called -
    char fuel [ ]
    second column - double amount
    third column - char description [ ]

    the first and second will be used by a fuction called double co2(char
    fuel [ ], double amount)
    and the third will be stored in the int main ....

    Second Question...Can you return 2 values from a function double
    co2(char fuel [ ], double amount)??

    Thanks
    Chris

  • Malcolm McLean

    #2
    Re: simple output input ....


    <chutsu@gmail.c omwrote in message
    news:1179094788 .034227.184210@ u30g2000hsc.goo glegroups.com.. .
    Hi
    Ok...I got a problem in reading a file containning information, and
    trying to send it to a function...
    the file containing information has format:
    Gas 10000 Home Gas Meter
    Electricity 3000 Home Electricity Meter
    Flight 8000 Holiday Abroad
    Flight 1000 Long Weekend
    Car 1250 Own Car (10000 miles)
    Car 500 Holiday Hire Car
    >
    I would like to put the first column to be stored in an array called -
    char fuel [ ]
    second column - double amount
    third column - char description [ ]
    >
    the first and second will be used by a fuction called double co2(char
    fuel [ ], double amount)
    and the third will be stored in the int main ....
    >
    Second Question...Can you return 2 values from a function double
    co2(char fuel [ ], double amount)??
    >
    Call fgets() to read the input, one line at a time.
    Once you've got the line into a temporary buffer, you need to break it up.
    This is actually quite fiddly, but not conceptually difficult.
    You can get the first word by reading until you hit a space character, the
    number by calling strtol(), and then the end pointer that you pass to strtol
    gives you the remainder of the line. However you should also deal with any
    unexpected inputs.
    Another way is to use fscanf(). However this is very easy to use unsafely.
    --
    Free games and programming goodies.


    Comment

    • Jim Barlow

      #3
      Re: simple output input ....

      chutsu@gmail.co m wrote:
      Hi
      Ok...I got a problem in reading a file containning information, and
      trying to send it to a function...
      the file containing information has format:
      Gas 10000 Home Gas Meter
      Electricity 3000 Home Electricity Meter
      Flight 8000 Holiday Abroad
      Flight 1000 Long Weekend
      Car 1250 Own Car (10000 miles)
      Car 500 Holiday Hire Car
      >
      I would like to put the first column to be stored in an array called -
      char fuel [ ]
      second column - double amount
      third column - char description [ ]
      >
      the first and second will be used by a fuction called double co2(char
      fuel [ ], double amount)
      and the third will be stored in the int main ....
      >
      Second Question...Can you return 2 values from a function double
      co2(char fuel [ ], double amount)??
      >
      Thanks
      Chris
      >
      Response to the Second Question:
      See K&R chapter 1, section 8, page 29

      "When the name of an array is used as an argument, the value
      passed to the function is the location or address of the beginning of
      the array - there is no copying of array elements. By subscripting this
      value, the function can access and alter any argument of the array."

      Your function will alter the values in the original array.

      Comment

      • Keith Thompson

        #4
        Re: simple output input ....

        Jim Barlow <formaldehyde@c harter.netwrite s:
        [...]
        See K&R chapter 1, section 8, page 29
        >
        "When the name of an array is used as an argument, the value
        passed to the function is the location or address of the beginning of
        the array - there is no copying of array elements. By subscripting
        this value, the function can access and alter any argument of the
        array."
        Which is actually (as I keep repeating) a special case of a more
        general rule. Whenever an array name (more generally, an expression
        of array type) is used, its value is implicitly converted to a pointer
        to its first element, *unless* it's the operand of a unary "&" or
        "sizeof" operator, or it's a string literal used to initialize a
        character array.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • guru.jois@gmail.com

          #5
          Re: simple output input ....

          On May 14, 3:19 am, "chu...@gmail.c om" <chu...@gmail.c omwrote:
          Hi
          Ok...I got a problem in reading a file containning information, and
          trying to send it to a function...
          the file containing information has format:
          Gas 10000 Home Gas Meter
          Electricity 3000 Home Electricity Meter
          Flight 8000 Holiday Abroad
          Flight 1000 Long Weekend
          Car 1250 Own Car (10000 miles)
          Car 500 Holiday Hire Car
          >
          I would like to put the first column to be stored in an array called -
          char fuel [ ]
          second column - double amount
          third column - char description [ ]
          >
          the first and second will be used by a fuction called double co2(char
          fuel [ ], double amount)
          and the third will be stored in the int main ....
          >
          Second Question...Can you return 2 values from a function double
          co2(char fuel [ ], double amount)??
          >
          Thanks
          Chris
          If the box is Unix.. use

          cut -f1 -d' ' to access first column,
          In general
          cut -fx -d' ' to access xth column,

          Comment

          • chutsu@gmail.com

            #6
            Re: simple output input ....

            ok so far:

            #include <stdio.h>


            double co2(char fuel[], double amount){



            }



            //Main program starts here
            int main ()
            {
            char fuel[20];
            double amount;
            double Total_Sum=0;
            char description[40];
            char answer;
            char ch;
            int i;
            int y=0;
            FILE * fptr;
            fptr = fopen("info.txt ", "r");
            ch = getc(fptr);

            //Displays the programs function
            printf("\n\n");
            printf("This program is designed to caculate the net CO2 emissions
            \n");
            printf("- Using the information listed in a file\n");
            printf("- Read and analyse the information\n") ;
            printf("- Give total CO2 emission in kg and 'rations'\n");
            printf("- Provide a breakdown of the emissions\n");
            printf("- Make use of the 'srncmp' function\n\n");

            //Continue program?
            printf("Do you what to start the program?(Y/N) ");
            scanf(" %c",&answer);
            printf("\n");

            //If Yes?
            if(answer='Y'){
            while ( ch != EOF){
            fscanf(fptr,"%1 9s %lf %39 [^\n]",fuel , &amount,
            description);
            }
            }


            return 0;
            }

            is my fscanf right?? cause I don't think its quite working....

            Comment

            • Barry Schwarz

              #7
              Re: simple output input ....

              On 14 May 2007 15:00:40 -0700, "chutsu@gmail.c om" <chutsu@gmail.c om>
              wrote:
              >ok so far:
              >
              >#include <stdio.h>
              >
              >
              >double co2(char fuel[], double amount){
              >
              >
              >
              >}
              >
              >
              >
              >//Main program starts here
              >int main ()
              >{
              > char fuel[20];
              > double amount;
              > double Total_Sum=0;
              > char description[40];
              > char answer;
              > char ch;
              > int i;
              > int y=0;
              > FILE * fptr;
              > fptr = fopen("info.txt ", "r");
              > ch = getc(fptr);
              >
              > //Displays the programs function
              > printf("\n\n");
              > printf("This program is designed to caculate the net CO2 emissions
              >\n");
              > printf("- Using the information listed in a file\n");
              > printf("- Read and analyse the information\n") ;
              > printf("- Give total CO2 emission in kg and 'rations'\n");
              > printf("- Provide a breakdown of the emissions\n");
              > printf("- Make use of the 'srncmp' function\n\n");
              >
              > //Continue program?
              > printf("Do you what to start the program?(Y/N) ");
              > scanf(" %c",&answer);
              > printf("\n");
              >
              > //If Yes?
              > if(answer='Y'){
              > while ( ch != EOF){
              > fscanf(fptr,"%1 9s %lf %39 [^\n]",fuel , &amount,
              >description) ;
              > }
              > }
              >
              >
              > return 0;
              >}
              >
              >is my fscanf right?? cause I don't think its quite working....
              I don't think so. The third % needs an 's' somewhere to tell it to
              extract a string.


              Remove del for email

              Comment

              • mark_bluemel@pobox.com

                #8
                Re: simple output input ....

                On 13 May, 23:19, "chu...@gmail.c om" <chu...@gmail.c omwrote:
                Hi
                Ok...I got a problem in reading a file containning information, and
                trying to send it to a function...
                the file containing information has format:
                Gas 10000 Home Gas Meter
                Electricity 3000 Home Electricity Meter
                Flight 8000 Holiday Abroad
                Flight 1000 Long Weekend
                Car 1250 Own Car (10000 miles)
                Car 500 Holiday Hire Car
                Your format is unclear - is it truly columnar data, where each "field"
                occupies a specific range of character positions, or is it blank
                delimited?

                Either way, I'd not use scanf, fscanf or fgets followed by sscanf -
                IMHO the whole family of functions is the spawn of satan...

                I'm not going to post a solution, as this smells like homework.

                On the question of "Can you return 2 values from a function" - the
                answer is simply "no". What are you actually trying to do here?

                Comment

                • mark_bluemel@pobox.com

                  #9
                  Re: simple output input ....

                  On 15 May, 02:10, Barry Schwarz <schwa...@doezl .netwrote:
                  On 14 May 2007 15:00:40 -0700, "chu...@gmail.c om" <chu...@gmail.c om>
                  wrote:
                  ....
                  fscanf(fptr,"%1 9s %lf %39 [^\n]",fuel , &amount, description);
                  ....
                  is my fscanf right?? cause I don't think its quite working....
                  I don't think so. The third % needs an 's' somewhere to tell it to
                  extract a string.
                  Nope, it needs the space removing between the 39 and the '['...

                  Comment

                  • mark_bluemel@pobox.com

                    #10
                    Re: simple output input ....

                    On 14 May, 23:00, "chu...@gmail.c om" <chu...@gmail.c omwrote:
                    ok so far:
                    >
                    #include <stdio.h>
                    >
                    double co2(char fuel[], double amount){
                    >
                    }
                    >
                    //Main program starts here
                    int main ()
                    {
                    char fuel[20];
                    double amount;
                    double Total_Sum=0;
                    char description[40];
                    char answer;
                    char ch;
                    int i;
                    int y=0;
                    FILE * fptr;
                    fptr = fopen("info.txt ", "r");
                    ch = getc(fptr);
                    >
                    //Displays the programs function
                    printf("\n\n");
                    printf("This program is designed to caculate the net CO2 emissions
                    \n");
                    printf("- Using the information listed in a file\n");
                    printf("- Read and analyse the information\n") ;
                    printf("- Give total CO2 emission in kg and 'rations'\n");
                    printf("- Provide a breakdown of the emissions\n");
                    printf("- Make use of the 'srncmp' function\n\n");
                    >
                    //Continue program?
                    printf("Do you what to start the program?(Y/N) ");
                    scanf(" %c",&answer);
                    printf("\n");
                    >
                    //If Yes?
                    if(answer='Y'){
                    while ( ch != EOF){
                    And how would this occur?
                    fscanf(fptr,"%1 9s %lf %39 [^\n]",fuel , &amount,
                    description);
                    }
                    }
                    >
                    return 0;
                    >
                    }
                    >
                    is my fscanf right?? cause I don't think its quite working....
                    remove the space after 39...

                    Comment

                    • Bart van Ingen Schenau

                      #11
                      Re: simple output input ....

                      chutsu@gmail.co m wrote:
                      ok so far:
                      >
                      #include <stdio.h>
                      >
                      >
                      double co2(char fuel[], double amount){
                      >
                      >
                      >
                      }
                      >
                      >
                      >
                      //Main program starts here
                      int main ()
                      {
                      char fuel[20];
                      double amount;
                      double Total_Sum=0;
                      char description[40];
                      char answer;
                      char ch;
                      int i;
                      int y=0;
                      FILE * fptr;
                      fptr = fopen("info.txt ", "r");
                      ch = getc(fptr);
                      >
                      //Displays the programs function
                      printf("\n\n");
                      printf("This program is designed to caculate the net CO2 emissions
                      \n");
                      printf("- Using the information listed in a file\n");
                      printf("- Read and analyse the information\n") ;
                      printf("- Give total CO2 emission in kg and 'rations'\n");
                      printf("- Provide a breakdown of the emissions\n");
                      printf("- Make use of the 'srncmp' function\n\n");
                      >
                      //Continue program?
                      printf("Do you what to start the program?(Y/N) ");
                      scanf(" %c",&answer);
                      printf("\n");
                      >
                      //If Yes?
                      if(answer='Y'){
                      This line contains an error, even though the compiler may not have
                      warned about it.
                      In C and C++, the =-sign *always* means assignment, regardless of the
                      context where it is used.
                      To test for equality, you need
                      if (answer == 'Y') {

                      If you also want to accept a lower-case y, you need to make explicit
                      provisions for it.
                      For example:
                      if (toupper(answer ) == 'Y') {
                      or
                      if ((answer == 'Y') || (answer == 'y')) {
                      while ( ch != EOF){
                      fscanf(fptr,"%1 9s %lf %39 [^\n]",fuel , &amount,
                      As others already noted, you must remove the space between 39 and [.
                      description);
                      }
                      }
                      >
                      >
                      return 0;
                      }
                      >
                      is my fscanf right?? cause I don't think its quite working....
                      The first one is (but the test following it is wrong), but the second
                      isn't.

                      Bart v Ingen Schenau
                      --
                      a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
                      c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
                      c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

                      Comment

                      • CBFalconer

                        #12
                        Re: simple output input ....

                        "chutsu@gmail.c om" wrote:
                        >
                        .... snip ...
                        >
                        Second Question...Can you return 2 values from a function double
                        co2(char fuel [ ], double amount)??
                        Sure, if you collect them into a suitable struct.

                        --
                        <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                        <http://www.securityfoc us.com/columnists/423>
                        <http://www.aaxnet.com/editor/edit043.html>
                        <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                        cbfalconer at maineline dot net



                        --
                        Posted via a free Usenet account from http://www.teranews.com

                        Comment

                        Working...