Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2011, 11:09 PM   PM User | #1
HopefulCoder
New to the CF scene

 
Join Date: Jul 2011
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
HopefulCoder is an unknown quantity at this point
Why is it always zero?

This is going to be a fairly simple question, but why does this always spit out 0?
Code:
#include <iostream>
#include <time.h>
#include <cstdio>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

int roll;

int dice(){
srand(time(NULL));
int roll=rand()%2+1;
return roll;
}

int main(void){
    char answer;
cout<<"Flip coin? (y or n) ";
cin>> answer;

if(answer=='y'||answer=='Y'){
dice();
cout<<roll;
}

else{
cout<<"You suck!";
}


}

Last edited by HopefulCoder; 11-14-2011 at 02:19 AM..
HopefulCoder is offline   Reply With Quote
Old 11-14-2011, 01:00 AM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,233
Thanks: 4
Thanked 81 Times in 80 Posts
Spookster will become famous soon enough
Did you run it through a debugger?
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 11-14-2011, 02:07 AM   PM User | #3
HopefulCoder
New to the CF scene

 
Join Date: Jul 2011
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
HopefulCoder is an unknown quantity at this point
Yes, no problems there.

The program runs, but always spits out zero if y or Y is picked.

Which I'm guessing means there is something wrong with the random number generator.
HopefulCoder is offline   Reply With Quote
Old 11-14-2011, 03:39 AM   PM User | #4
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,042
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
You don't need to seed the RNG every time you call your dice function. You only need to do it once in your main function.

The reason it is always zero though is because you are printing the value of the global variable 'roll' which you are never initializing. (The debugger is just being nice enough to zero it for you.) And you never assign the return value of your dice function to said roll variable. Or you could just cout the result of the function if you wanted.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Users who have thanked oracleguy for this post:
HopefulCoder (11-14-2011)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:53 AM.


Advertisement
Log in to turn off these ads.