Thread: Another simple program

  1. #16
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Hi,

    @whiteflags: I can see your point. Not so random then is it in that situation.

    However, I'm not entirely sure I understand what rand()%5 actually means, or does. Modulus in my understanding is a number divided into another number, leaving a remainder, so you could check whether a number is even by dividing by 2 (which would leave no remainder).

    What would be the min and max values rand()%5 would return?

    I think maybe your point is affirmed here:

    What is modulus and how do I use it?

    with this quote:

    "Using % for generating random numbers in a given range is good for quick-and-dirty programming, but it is not particularly good if you need 'industrial strength' pseudo random number generation -- you'll often end up with a skewed distribution.

    Edd"

    Sam.
    Last edited by samwillc; 06-08-2012 at 02:54 AM.

  2. #17
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    This seems to work better for me:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main ()
    {
        int numA = 5;
        int numB = 5;
        int answer = numA + numB;
        int guess = 0;
        
        for ( int attempts = 1; attempts<=3 && guess !=answer ; attempts++ )
        {
        cout << "What is " << numA << " + " << numB << "? ";
        cin >> guess;
            
        if (guess == answer)
            {
            cout << "Well done, got it in: " << attempts << " attempts!\n";
            }
            else
            {
                if ( attempts < 3 )
                { 
                cout << "Incorrect. Please try again.\n";
                }
                else
                {
                cout << "Sorry, run out of attempts! Game over...";
                }
            }
        }
    }
    Got rid of the breaks and seems to function ok.

    Still not quite got my head around how to get a random number between 1 and 10 other than rand()%10 which in a lot of forums is not really a good way to do this.

    Also, wanted to add a check whether a number has actually been entered, and also to give the user another go with y/n after they get it right.

    Sam.
    Last edited by samwillc; 06-08-2012 at 03:36 AM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by samwillc
    Still not quite got my head around how to get a random number between 1 and 10 other than rand()%10 which in a lot of forums is not really a good way to do this.
    Suppose you have a ten sided fair die (numbered 0 to 9), and you want to use it to choose between McDonald's, Chinese food and Turkish food. So, you roll the die, divide the result by 3 and take the remainder. If you get 0, you eat at McDonald's, if you get 1, you eat Chinese food and if you get 2 you eat Turkish food. The thing is, the 10 possible die roll outcomes don't map evenly into the 3 choices, so you are more likely to eat at McDonald's. One way to deal with this is to re-roll if you roll a 9.

    Anyway, the latest version of the C++ standard library comes with random number facilities that abstract away this problem.
    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. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your indentation is inconsistent again. Good idea to fix it.
    rand() % 11 gives you a number between 0 ... 10. The whole idea is that you are using modulus, which takes the remainder of the number.
    0 % 11 == 0
    1 % 11 == 1
    ...
    10 % 11 == 10
    11 % 11 == 0
    12 % 11 == 1
    ...
    etc

    For error checking, you do something like

    Code:
    if (!std::cin)
    {
    	std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
    	std::cin.clear();
    }
    The first line checks if the input stream is in an inconsistent state, which happens if the user tries to input something that cannot be converted that whatever you are trying to extract.
    The next line flushes out the invalid data, and the last one tells the stream that we've taken care of the bad input.
    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
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Thanks for the help everyone.

    What would be the best way to indent this code for my maths quiz above? I don't really have much to work with here so although it's helpful pointing out it's inconsistent, I don't know where. Just a single example would probably be good enough for me to work from.

    Sam.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Great, thanks, bookmarked it.

    Sam.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM