#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Random number generator problem(noob)


    Code:
    /*
    Sean M. Dane
    November 14,2013
    COP 2000 assignment #4
    */
    
    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    
    
    using namespace std;
    void intRand (int *);
    void showMenu();
    
    
    
    
    int main ()
    {
    
    	float guess, ANS;
    	int r[2];
    	int choice = 0;
        int ADD_CHOICE = 1,
            QUIT_CHOICE = 4;
                 
    	srand((unsigned int)time((time_t *)NULL));//need for rand()
    
    	showMenu();
    	
    	while (choice < ADD_CHOICE || choice > QUIT_CHOICE)
        {
            cout << "Please enter a valid menu choice: ";
            cin >> choice;
        } 
        
        if (choice == QUIT_CHOICE)
    		return 0;
    	cout << "What is your answer for ";
        
        intRand(r);
        
    	switch (choice)
    	{
    		case 1:
    			ANS = (r[0] + r[1]);
    			cout << r[0] + r[1] << "= ? ";
    			cin>> guess;
    			if (guess == ANS)
    				cout << "Great job!";
    			else
    				cout << ANS << '\n';
    			break;
    
    		case 2:
    			ANS = (r[0] - r[1]);
    			cout << (r[0] - r[1]) << "= ? ";
    			cin>> guess;
    			if (ANS == guess)
    				cout << "Great job!";
    			else
    				cout << ANS << '\n';
    			break;
    
    		case 3:
    			ANS = (r[0] * r[1]);
    			cout << r[0] * r[1] << "= ? ";
    			cin>> guess;
    			if (ANS == guess)
    				cout << "Great job!";
    			else
    				cout << ANS << '\n';
    			break;
    	}
    	
    	return 0;
    }
    
    
    void showMenu()
    {
    	cout << "\n\t\tMENU OF OPERATIONS \n\n"
    	<< " 1. Addition.\n"
    	<< "\n 2. Subtraction.\n"
    	<< "\n 3. Multiplication.\n"
    	<< "\n 4. Quit the Program.\n\n";
    }
       
    void intRand(int *r)   
    {
    	r[0] = rand() % 11 + 2;
    	r[1] = rand() % 11 + 2;
    }
    it seems to be generating 1 number and not 2. when I click 1 for addition it says

    what is your answer for 9= ? =
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,706
    Rep Power
    1959
    You could try to output r[0] and r[1] in the intRand function.

    But my guess is that it is calculated when you try to print/cout it out.
    Take a look at how you get the ANS value, and then look how you want to print it out. (same syntax used)

    To print each out, try something like this (untested):
    Code:
    cout << r[0] <<  " + " << r[1] << "= ? ";
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,248
    Rep Power
    2222
    (guess == ANS)

    This is binary floating-point that we're talking about, so the representation of decimal values is only approximate. It may look like they'd have to be equal, but they could instead differ by less than a millionth -- remember, they have to be exactly the same to count as being equal.

    Why use float? The numbers you are getting in intRand are integers. Why not work with integers instead? Integers won't give the approximation problems that floating-point will.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,248
    Rep Power
    2222
    BTW, compiling your code as-is and running, I got this:
    Code:
    C:TEST>a
    
                    MENU OF OPERATIONS
    
     1. Addition.
    
     2. Subtraction.
    
     3. Multiplication.
    
     4. Quit the Program.
    
    Please enter a valid menu choice: 1
    What is your answer for 15= ? 15
    Great job!
    C:TEST>
    So what problem do you think exists? What makes you think that one exists?
    Last edited by dwise1_aol; November 21st, 2013 at 06:21 PM.

IMN logo majestic logo threadwatch logo seochat tools logo