Thread: My introduction to C++. [Beginner's Problem(s)]

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    My introduction to C++. [Beginner's Problem(s)]

    I just started C++ again, after taking a summer school course on it.
    That was a few months back, it's not offered during my normal school day.

    I found some sample code from this website to use as my own, personal version of "Hello World!". Mainly used as a refresher.

    The problems that I run into with this code, is that when I run this "game", the magic number always happens to be "41".

    Code:
    #include <stdlib.h>
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        int number=rand()%100;
        int
        guess=-1;
        int trycount=0;
        while(guess!=number && trycount<10)
    
    
        {
            cout<<"Please Enter a Numerical Guess: ";
            cin>>guess;
    
    
            if(guess<number)
            cout<<"Sorry, That Number is too low! Please Try again!"<<endl;
    
    
            if(guess>number)
            cout<<"Sorry, That Number is too high! Please Try again!"<<endl;
    
    
            trycount++;
        }
        if (guess==number)
        cout<<"Congratulations! You correctly guessed the number!";
        else
        cout<<"Sorry, the correct number was:"<<number;
        return 0;
    }
    41 (for me) is the number that always wins the game. Despite the fact that I used the Random tag. I can't use RAND_MAX because we're not going between 0 and 32767.

    Any ideas, anybody?

    P.S. If you find the problem, lay out what my code /should/ look like. I'm a noob at this, just resumed from a very long break from it today.

    Thanks,
    Adam.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Use srand (only once, at the beginning) before calling rand, for seeding the generator.
    Code:
    srand(time(NULL));
    Here it is seeded with the current time, thus giving a different random series each time.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    so...where would I put that in the code? line 1? like before the #include's?
    I'm a total noob hahah

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At the start of the main function.
    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

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Quote Originally Posted by laserlight View Post
    At the start of the main function.
    Still a tiny bit confused...line 1? so above all of the #include's?

    or before the main()?

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Adam0193 View Post
    Still a tiny bit confused...line 1? so above all of the #include's?

    or before the main()?
    Neither.
    Inside main.
    i.e
    Code:
    int main()
    {
          //Here
          //Everything else
    }

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    It gives me an error that 'time' was not declared in this scope.
    I think that means that it doesn't recognize "time" or that it's not a variable.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Add #include <time.h> just after your other includes.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    1
    Hello, I am new to the forum.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by fengwilson View Post
    Hello, I am new to the forum.
    so what? do you want a cookie? do you think it's ok to hijack someone else's thread to introduce yourself? for that, you should go to the general discussions forum. what you have just done is a rather egregious violation of proper forum etiquette.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elkvis View Post
    so what? do you want a cookie? do you think it's ok to hijack someone else's thread to introduce yourself? for that, you should go to the general discussions forum. what you have just done is a rather egregious violation of proper forum etiquette.
    Haw Wude !!
    My introduction to C++. [Beginner's Problem(s)]-jjportrait-jpg

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    Use srand (only once, at the beginning) before calling rand, for seeding the generator.
    Code:
    srand(time(NULL));
    Here it is seeded with the current time, thus giving a different random series each time.
    mana, you know as well as I do that should be "nullptr".
    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. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    mana, you know as well as I do that should be "nullptr".
    I had some doubts about whether it is alright to use with C functions.
    As NULL is defined in ctime, and time is documented to work with NULL , I wrote that.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, you should really be using
    #include <ctime>
    #include <cstdlib>

    and

    std::srand(std::time(nullptr));

    Anyway, nullptr is implicitly convertible to any pointer type, so yes, it works with C functions.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Introduction?
    By Blade3575 in forum General Discussions
    Replies: 9
    Last Post: 11-30-2011, 10:02 AM
  2. My Introduction
    By MrMatt027 in forum General Discussions
    Replies: 0
    Last Post: 12-03-2010, 01:10 PM
  3. introduction to c++
    By ai_bob in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 07:19 AM
  4. Introduction
    By Surge in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-04-2006, 09:11 PM