I want it to say "You are correct!" when the number they guess matches the random roll but it just keeps saying this when I type in 6 as my guess, how do I fix it?

This is what I wrote in the main:

int num;
	int die;


	cout << "What number do you think you'll roll? " << endl;
	cin >> num;
	
	die=(int)(rand()%6)+1;
	
	if (num<6 && num>0) {
			srand((unsigned)time(0));
			cout << die << endl;
		        }

	else if (num==die) {
		cout << "Congradulations! You predicted correctly. " << endl;
	        }
	
	else if (num > 6) {
		cout << "Number not on die" << endl;
		}



        
        
                           
                            
	system("PAUSE");
	return(0);

Recommended Answers

All 5 Replies

In your example, you're neglecting to test if the die rolls on a 6 in your if statement. Also, rather than using

num < 6 && num > 0

, try:

if ((num >= 1) && (num <= 6)) {
// rolls on a correct die value
}

Also, keep in mind that someone could also enter a negative integer as their guess for the die. To be safe, you could declare an unsigned integer variable.

int num;
    int die;


    cout << "What number do you think you'll roll? " << endl;
    cin >> num;
    
    die=(int)(rand()%6)+1;              // So far, so good
    
    if (num<6 && num>0) {               // So 0 is a proper guess? But 6 is not?
            srand((unsigned)time(0));   // Why are you seeding the random generator
                                        //    after you got your random value?
            cout << die << endl;        // And what good is displaying the die in this case?
    }

    else if (num==die) {                // How can you get here if the above IF is true?
        cout << "Congradulations! You predicted correctly. " << endl;
    }
    
    else if (num > 6) { 
        cout << "Number not on die" << endl;
    }
                            
    system("PAUSE");            // Not a good way to pause the program
    return(0);

srand() needs to be called once at the start of the program.

else if (num==die) {                // How can you get here if the above IF is true

That's what I can't figure out, suggestions would be appreciated.

In your example, you're neglecting to test if the die rolls on a 6 in your if statement. Also, rather than using

num < 6 && num > 0

, try:

if ((num >= 1) && (num <= 6)) {
// rolls on a correct die value
}

Also, keep in mind that someone could also enter a negative integer as their guess for the die. To be safe, you could declare an unsigned integer variable.

O thank you, I totally miseed that :)

That's what I can't figure out, suggestions would be appreciated.

Well, if you start by testing if num == die , you now know if the guess is correct or not right away. Why do you need to test for more?

Any comments on my other points?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.