Thread: Same old beginner question...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Same old beginner question...

    Hello everyone.

    Yes I am asking the same old beginner question and I hope that you guys don't flame me for it. I am currently in my teens and just started to learn C. I have a lot of interest in C but I have not been able to start learning it smoothly. I do have one book, which I am using as my primary source but I don't think that its the best. Sam's Teach Yourself C in 24 Hours By Tony Zhang. I am following things that I see on this site and I am using the Bloodshed Dev-C IDE.

    I want to know how I should start, sources to learn from, how to put the warning on my compiler to a good level so I don't get into bad coding habits, and mainly I want to know about exercises that I can do to learn C in a better way. For example: exercises using a loop when I have just learn't about a loop but in a more day to day situation then what my book gives me with. Finally I want to ask what I should make as an initial program, I have made a 5 question quiz. What do you guys think Is a good beginner program that is both challenging and teaches a lot to a beginner?

    Thanks for your time!
    -Sharmz

  2. #2
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Some ideas for programs:
    - a number guessing game
    - change calculator; how many of what coins are needed to pay $x.xx?

    IDE's are often a bit much for what a beginner needs. I use a text editor (with syntax highlighting) and the gcc compiler from the command line. If you are working on a larger project then an IDE can be invaluable.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Is C your first programming language?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Is C your first programming language?
    Yes

    Here is an easy guessing game that I made. Please comment on it and how I could improve coding. Especially goto...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(void)
    {
        int a; int b;
        printf("Welcome to the number guessing game! Press enter to start:\n");
        getchar();
        a = rand();
        start_of_game:
        printf("Guess a number\n"); 
        scanf("%d", &b);
        
        if(b == a)
        {
             printf("You win! The correct number is %d, enter to exit...\n", a);
             getchar();
             return 0;
        }
        else
        {
            printf("You are incorrect, try agian...\n");
            goto start_of_game; /* I hate using this.... but I cant think of anything else*/
        }      
        return 0;
    }

  5. #5
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Look into using a while loop instead of goto.

    Also, you might tell the user if their guess is too high or too low.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Perhaps make the number in a smaller range so that somebody can actually guess it. If you guess it, the getchar doesn't pause the program because it gets the newline entered from the previous scanf.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Here is a new code without goto! Yes! but it always seems to generate 41 for me. I looked up ways to generate numbers and to make a limit but i can only find very complicated algorithms. I fixed the scanf problem, although not in the best way probably.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(void)
    {
        int a; int b;
        printf("Welcome to the number guessing game! Press enter to start:\n");
        getchar();
        a = rand();
        start_of_game:
        printf("Guess a number \n", a); 
        scanf("%d", &b);
        
        while (b != a)
        {
              printf("You are incorrect, try again...\n");
              scanf("%d", &b);
        }
        if(b == a)
        {
             printf("You win! The correct number is %d, enter to exit...\n", a);
             getchar();
             return 0;
        }
        getchar();   
        return 0;
    }

  8. #8
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    As for the random number. Try:
    Code:
    /* you will need these includes */
    #include <cstdlib>
    #include <time.h>
    
    /* declare a variable to hold the number of seconds */
    time_t seconds;
    
    /* get the number of seconds from your system */
    time(&seconds);
    
    /* seed the random number generator */
    srand((unsigned int) seconds);
    
    /* now when you call rand() it should be different each time */
    But I haven't tried it. Add this to the start of your program. Hope it works!

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Quote Originally Posted by noops View Post
    As for the random number. Try:
    Code:
    /* you will need these includes */
    #include <cstdlib>
    #include <time.h>
    
    /* declare a variable to hold the number of seconds */
    time_t seconds;
    
    /* get the number of seconds from your system */
    time(&seconds);
    
    /* seed the random number generator */
    srand((unsigned int) seconds);
    
    /* now when you call rand() it should be different each time */
    But I haven't tried it. Add this to the start of your program. Hope it works!
    No didn't work, sorry. I might have coded it wrong but I just wrote what you said at the start of my program.

  10. #10
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Quote Originally Posted by Sharmz View Post
    No didn't work, sorry. I might have coded it wrong but I just wrote what you said at the start of my program.
    I didn't mean literally at the start. Put
    #include <cstdlib>
    #include <time.h>

    where you have the other headers (before int main())

    And put:

    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);

    After the int a; int b; line.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Correction: cstdlib is the C++ version of stdlib.h. You should be including stdlib.h.

    In addition, you needn't make the srand() call so difficult. Just this should suffice:

    Code:
    srand(time(NULL));
    Then afterwards calls to rand() will behave in a more random pattern.

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Thanks everyone, I wish someone would answer my initial question please

    I want to know how I should start, sources to learn from, how to put the warning on my compiler to a good level so I don't get into bad coding habits, and mainly I want to know about exercises that I can do to learn C in a better way. For example: exercises using a loop when I have just learn't about a loop but in a more day to day situation then what my book gives me with. Finally I want to ask what I should make as an initial program, I have made a 5 question quiz. What do you guys think Is a good beginner program that is both challenging and teaches a lot to a beginner?
    thanks a lot

  13. #13
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Thanks. I pulled that info from a quick scan of the tutorial on this website titled "Getting Random Values in C and C++ with Rand" which upon closer inspection only covers C++.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    rand () &#37; 10 gives values from 0-9.

    gcc -Wall -Werror -ansi -pedantic code.c

  15. #15
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by Sharmz View Post
    Thanks everyone, I wish someone would answer my initial question please.

    thanks a lot
    Okay, forgive the shameless plug, but please check out my site www.cymonsgames.com. You're already set up so maybe some programming resources would help. (Man, I need to beef that up.)

    As for challenges to get started, when you write some please consider letting me add them to my site. I'm sadly short on beginner programs.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM

Tags for this Thread