Thursday, 15 September 2011

c - "rand()" not working, i keep getting the same number after every execute -


So I'm making an ATM program for my programming class, the long story is short, I have to create about four random numbers I have my problem that my program will not create separate numbers, when I execute my program, then I have similar numbers. My main function was srand (time (null)); And since my variables were global, I tried to move it primarily ahead of my global variable, it left me without any affiliate error. HELPPP !!!!

Includes # lt; Stdio.h & gt; #to & lt include, time.h> #to & lt include, stdlib.h & gt; #to & lt include, string.h & gt; Int account_number = rand ()% 5, pin = rand ()% 3, chk_acc_bal = rand ()% 4, sav_acc_bal = rand ()% 5;

The problem here is that you start your wars with work in the scope of the file Trying to call.

Just try to start your variable within your main function. Something like this (I test and work):

  #include & lt; Stdio.h & gt; #to & lt include, time.h> #to & lt include, stdlib.h & gt; #to & lt include, string.h & gt; Int account_number, pin, chk_acc_bal, sav_acc_bal; Int main (zero) {srand (time (faucet)); Account_number = rand ()% 5; Pin = rand ()% 3; Chk_acc_bal = Rand ()% 4; Sav_acc_bal = rand ()% 5; Printf ("% d \ n", ACCOUNT_NUMBER); Printf ("% d \ n", pin); Printf ("% d \ n", chk_acc_bal); Printf ("% d \ n", sav_acc_bal); Return 0; }  

Hope it helps. the best!


No comments:

Post a Comment