Thread: a really stupid question

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    I believe you mean you have the string 15, as in "15"? And you want to convert that into an int, right? Then you can use strtod.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    #include <stdio.h>
    
    int main( void )
    {
        char buf[BUFSIZ];
        float var[255];  /* big enough to hold 255 entries */
        int i = 0;
        
        /*
         * read from standard input stream line by line
         * and format any float 
         */
        while( fgets( buf, sizeof buf, stdin ) )
        {
            if( sscanf( buf, "&#37;f", &var[i] ) > 0 )
            {
                /* display */
                printf( "%.2f\n", var[i] );
                ++i;
                
            }
        }
        return 0;  
    }
    Make sure you direct some file to it in the command line. If not it will enter in a cat mouse chase
    Last edited by Aia; 02-01-2008 at 05:19 PM.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    this code is actually right:

    Code:
    int main(int argc, char* argv[])
    {
        int i;
        for(i=0; i<argc; i++)
            printf("&#37;s\n", argv[i]);
        getchar();
    }
    however instead of printing, I would like to store argv[i] into a char, say argv[i] is '15.0' and I want a double value of 15.0
    Last edited by -EquinoX-; 02-01-2008 at 05:48 PM.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    OK, here's how it's done:
    If you want a string, for example "15.0", to be converted into an integral type, then you use strtod for string to double.
    If you just want the representational number that represents a character, then you use a cast.
    For example, the value which represents the character '1' in multi-byte on Windows, which is 49, then do a cast.
    Last edited by Elysia; 02-01-2008 at 06:06 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay hold on, before preceeding to my next confusion, can I do this:

    double temp = (double)argv[1][1];

    so when I type in ./donut 3.0

    temp will have a value of 3.0 as a double not as a char

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    No. First it should be argv[1], because it's a pointer to strings. And a string is an array of char.
    What you get, is 52. What the heck, you say? Why 52? Because it's the value used to represent the character 3 on multi-byte on Windows.
    Use strtod to properly parse the string and return a double representation of that string. Or in other words, to return 3.0.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    elysia, now I am able to get what you said 2 posts ago.
    when I typed in 1 the out put is 49. You mentioned to do a cast. So I cast it to a double right?? by just adding the (double) in front?
    here's my code:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        double dozen_donut_cost;
        int donuts_eaten;
        int i;
        double donuts_cost;
        double temp = (double) argv[1][0];
    
          printf("&#37;g\n", temp);
    
        return 0;
    }
    when I typed in ./donut 1.0

    the result is

    49

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Then you'll get 49, the value representing the character '1'.
    You need to call strtod to translate your string into a double.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by Elysia View Post
    Then you'll get 49, the value representing the character '1'.
    You need to call strtod to translate your string into a double.
    how would I use the strtod in this case?

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    double d = strtod(argv[1], NULL);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    it gives me this error implicit declaration of function strtod and it doesn't give me 1.0 when I execute the program using ./donut 1.0

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        double dozen_donut_cost;
        int donuts_eaten;
        int i;
        double donuts_cost;
        double temp = strtod(argv[1], NULL);
    
          printf("&#37;g\n", temp);
    
        return 0;
    }
    Last edited by -EquinoX-; 02-01-2008 at 06:55 PM.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Implicit declaration of function means you haven't created a prototype for your own functions or in the case of library functions, you haven't included the correct header. So typically you would look into some documentations to find the correct header. For strtod, it's stdlib.h.

    And no, it should be argv[1]. Remember that argv is a array of strings. Strings themselves are an array of char. So it becomes string* or string[], but since string is char[], it becomes char[][].
    You must pass a string, not a character.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    thanks Elysia it works, I had one question though an important one:

    say that I want to do ./donut< t0,

    where t0 is file that contains 3.0 1.0 2.0.... (all in one line)

    how do I get the 3.0 value 1.0 value, etc??

    my instructor says that I should use scanf to get the t0? but how do I do this?

    what if I do this at the beginning of the program:

    Code:
    #include <stdio.h>
    
    int main ()
    {
      char temp;
      scanf("&#37;s", &temp);
    
      printf("%s\n", temp);
    
      return 0;
    }
    will it work if I do ./donut < t0?
    Last edited by -EquinoX-; 02-01-2008 at 08:06 PM.

  14. #29
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by -EquinoX- View Post
    what if I do this at the beginning of the program:

    Code:
    #include <stdio.h>
    
    int main ()
    {
      char temp;
      scanf("&#37;s", &temp);
    
      printf("%s\n", temp);
    
      return 0;
    }
    will it work if I do ./donut < t0?
    Q: What's temp?
    A: A tiny, tiny, one byte of holding storage in most systems.

    Q: What are you telling scanf to do with %s?
    A: Hey, get this string ( an array of chars with '\0' that is invisible behind every string ) and
    push it into this tiny one byte variable named temp.

    Q: Do you think that is going to work?
    A: Nope, but ...I though...I am just learning...ah,...maybe...it will work for me just this one time?

    Code:
    #include <stdio.h>
    
    int main( void )
    {
        char buf[BUFSIZ];
        float var[255];  /* big enough to hold most entry? */
        int i = 0;
        int x = 0;
        
        /* read the standard input stream */
        while( scanf( "%f", &var[i] ) > 0  )
            ++i;
        /* testing outside the while loop */
        for( x = 0; x < i; x++ )
        {
            printf( "var[%d] = %.2f\n", x, var[x] );
        }
        return 0;  
    }

  15. #30
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    how do I resolve this problem??

    Code:
    warning: format &#226;&#37;lf&#226; expects type &#226;double *&#226;, but argument 2 has type &#226;double&#226;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM