Hello all! First time poster, and VERY new C++ user. I'm just learning the ropes here, so be gentle. =)
Anyway, here's what I've set out to do: I'd like to have "randomized" binary numbers appear in a manner such as a computer screen (format wise, so that shouldn't be an issue...). I did a quick search and found http://www.cplusplus.com/forum/windows/13326/ , but it didn't quite solve my problem, and it didn't even run for me, so I've done something wrong, methinks. I'm using Code::Blocks, if it matters (don't think it does, but I'm just putting it out there).
#include <iostream> //Input/output functions.
#include <ctime> //Contains the time functions we need.
#include <cstdlib> //Contains the random functions we need.
usingnamespace std;
int main()
{
bool nums[100]; //Declare an array of bool values.
shortint i = 0; //We will use this to iterate around our loop.
srand(time(NULL)); //Set the seed for the random numbers to the current time (so we get a different seed each time we run the program).
while(i < 100) //Iterate through the array.
{
nums[i] = rand() % 2; //Make our bool value a random number from 0-1.
cout << nums[i]; //Output number.
++i; //Carry on through the loop.
}
return 0;
}
Everything you've listed makes sense, Mats, but I'm getting 11 errors in Code::Blocks when I attempted to run this. I've copy pasted this exactly, so this isn't an issue.
Line 1, error: iostream: No such file or directory
Line 2, error: ctime: No such file or directory
Line 3, error: cstdlib: No such file or directory
Line 5, error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'
In function 'main':
Line 9, error: 'bool' undeclared (first use in this function)
Line 9, error: (Each undeclared identifier is reported only once
Line 9, error: for each function it appears in.)
Line 9, error: expected ';' before 'nums'
Line 11, error: 'NULL' undeclared (first use in this function)
Line 14, error: 'nums' undeclared (first use in this function)
Line 15, error: 'cout' undeclared (first use in this function)
These are the errors I'm getting, so I'm assuming I've done something wrong with Code::Blocks itself, considering it's not recognizing the libraries. When opening Code::Blocks, I went New --> Empty File. Then I input the data on that file, and compiled. What am I doing wrong?
Are you sure you compile it as C++ and not C? If your file name ends with .c I think Code::Blocks try to compile the code as C. Make sure your file name ends with .cpp or some other common C++ file extension.
Peter87, you're right. It was saving as a C file. After saving the file as a .cpp, it compiles with no errors, thanks!
Mats code works just as I had hoped, with one exception: it ends far shorter than I had hoped. Ideally, I would like it to fill the command prompt with randomized binary numbers, but it ends after filling the first one and a third lines. Is that due to "while(i < 100)" (line 12)'s code, telling it to only do 100 lines of code?