|
-
January 1st, 2012, 12:27 PM
#2
Re: Srand function problem and a different problem
 Originally Posted by revve
Code:
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
#include <ctime>
#include <iomanip> //In this line, compiler gives warning C4067: unexpected tokens following //preprocessor directive - expected a newline but program still executes instead of this. //What is the reason of this error? How can I fix it?
using std::setw;
using namespace std;
Have you tried moving the using directives to after the includes?
 Originally Posted by revve
Another question,
I have the following code in my constructor:
Code:
srand(int(time(NULL)));
for(int i=0; i<(99-1); i++){ //shuffling card;
int r = i + (rand() % (99-i)); // Random remaining position.
int temp = kartSayilar;
kartSayilar = kartSayilar[r];
kartSayilar[r] = temp;
}
The aim of this is to shuffle numbers in the kartSayilar array randomly. I creates two objects of this in a different .cpp file but when program executes, both objects have the same random numbers. I think my srand function doesn't work. How can i fix it?
Where is the index to kartSayilar on the third line in the for loop? I think you want:
Code:
srand(int(time(NULL)));
for(int i=0; i<(99-1); i++){ //shuffling card;
int r = i + (rand() % (99-i)); // Random remaining position.
int temp = kartSayilar[i];
kartSayilar[i] = kartSayilar[r];
kartSayilar[r] = temp;
}
-Erik
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|