Like Tree1Likes
  • 1 Post By manasij7479

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

This is a discussion on My introduction to C++. [Beginner's Problem(s)] within the C++ Programming forums, part of the General Programming Boards category; I just started C++ again, after taking a summer school course on it. That was a few months back, it'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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    21,910
    At the start of the main function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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
    }
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    2,499
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 !!
    Name:  Jjportrait.jpg
Views: 83
Size:  101.3 KB
    Elkvis likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    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, 09:02 AM
  2. My Introduction
    By MrMatt027 in forum General Discussions
    Replies: 0
    Last Post: 12-03-2010, 12: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, 08:11 PM

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