a really stupid question

This is a discussion on a really stupid question within the C Programming forums, part of the General Programming Boards category; I just learned C this morning and I have a simple question. I have a program called donut.c and the ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    a really stupid question

    I just learned C this morning and I have a simple question. I have a program called donut.c and the code is like this:
    Code:
    #include <stdio.h>
    
    int main()
    {
        float dozen_donut_cost;
        int donuts_eaten;
        int i = 0;
        float donuts_cost;
    
        return 0;
    }
    then when I want to execute donut, the instruction is:
    ./donut < t0

    where the file t0 contains:
    3.00 12 .5 .5 .5 .5 .5 .75 .75 .75 .75 1.0 1.0 1.0

    how do I assign the variable dozen_donut_cost so that it equals 3.00? Which is the first value in file?? Thanks

  2. #2
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by -EquinoX- View Post
    I just learned C this morning...
    Don't you mean you started to learn about C this morning?
    Quote Originally Posted by -EquinoX- View Post
    how do I assign the variable dozen_donut_cost so that it equals 3.00? Which is the first value in file?? Thanks
    Before you can do that, you'll need to keep learning for a while longer.
    You need to learn about accepting arguments from the command line into the main function.
    You need to learn about pointers and FILE pointers
    You need to learn about opening files in different diverse modes
    You need to learn how to read from files.
    You need to learn how to close files.

    That's only a rough road map. Keep it up.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by Aia View Post
    Don't you mean you started to learn about C this morning?

    Before you can do that, you'll need to keep learning for a while longer.
    You need to learn about accepting arguments from the command line into the main function.
    You need to learn about pointers and FILE pointers
    You need to learn about opening files in different diverse modes
    You need to learn how to read from files.
    You need to learn how to close files.

    That's only a rough road map. Keep it up.
    Say that I want to do this instead of the one that I posted up:
    ./donut 3.00 12 .5 .5 .5 .5 .5 .5 .75 .75 .75.75

    how do I read the 3.0??

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Learn C better first before you start messing with things above your head.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Say that I want to do this instead of the one that I posted up:
    ./donut 3.00 12 .5 .5 .5 .5 .5 .5 .75 .75 .75.75

    how do I read the 3.0??
    Well something like this should work:
    Code:
    int main(int argc, char* argv[])
    {
        int i;
        for(i=0; i<argc; i++)
            printf("&#37;s\n", argv[i]);
        getchar();
    }
    basically argc is the number of things you add in the command line, and argv is an array of arguments passed in as a string.

    Then you would have to convert the strings to doubles. So look up strtod.

    Good luck

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    But start i at 1 (as mike_g knows of course!).
    Element 0 is the program name.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    but I can't convert the char to an int right??

  9. #9
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by -EquinoX- View Post
    but I can't convert the char to an int right??
    Oh, oh, here starts to be noticeable the gap in understanding.
    Is is not a char. It is a string made up of an array of chars terminated with the nul terminator '\0'.
    And yes, you can convert char to int.
    Nevertheless, you need to convert the string or array of chars to a float.
    Last edited by Aia; 01-31-2008 at 07:03 PM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I tried to put in this code:

    Code:
    int main(int argc, char* argv[])
    {
        int i;
        for(i=0; i<argc; i++)
            printf("&#37;s\n", argv[i]);
        getchar();
    }
    however when I enter the command :
    gcc -ansi -Wall donut.c -o donut
    ./donut < t0

    it stays and do nothing, it should print the value of 3.00 12 .5 .5 .5 .5 .5 .75 .75 .75 .75
    right? however when I type in
    ./donut 3.00 12 .5 .5 .5 .5 .5 ..... it works
    Last edited by -EquinoX-; 01-31-2008 at 09:12 PM.

  12. #12
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by -EquinoX- View Post
    t should print the value of 3.00 12 .5 .5 .5 .5 .5 .75 .75 .75 .75
    right?
    Riiiight!. Your compiler is broken. Make sure you return it for a refund

    Just joking.

    If anything your program would display:
    donut
    <
    t0
    because that would be argv[0], argv[1] and argv[2].
    However that's not the case, since the < is a selected operator by the shell. So any thing after that doesn't count as argument for main.
    The point is that it would never display the content of the file t0 that you are passing.

    Try:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
    	char buf[BUFSIZ];
    
    	while( fgets( buf, sizeof buf, stdin ) )
    	{
    		printf( "%s", buf );
    	}
    
    	return 0;
    }
    and see what happens.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by Aia View Post
    Riiiight!. Your compiler is broken. Make sure you return it for a refund

    Just joking.

    If anything your program would display:

    because that would be argv[0], argv[1] and argv[2].
    However that's not the case, since the < is a selected operator by the shell. So any thing after that doesn't count as argument for main.
    The point is that it would never display the content of the file t0 that you are passing.

    Try:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
    	char buf[BUFSIZ];
    
    	while( fgets( buf, sizeof buf, stdin ) )
    	{
    		printf( "&#37;s", buf );
    	}
    
    	return 0;
    }
    and see what happens.
    This is what I want, but I don't want it to be printed I want to store each element in the file into an argument. How do I do this?
    Last edited by -EquinoX-; 02-01-2008 at 02:53 PM.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    anyone know how to convert a char into an int of it's value? Say I have a char 15 and I want to get an int which value is 15?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char ch = 15;
    int x = ch;
    Although I have a feeling you are after something more than that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 3 123 LastLast
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-03-2005, 11:17 PM
  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, 07: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

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