random # generator

why does it have the errors in the underlined piece of code.the error is " `cout' undeclared (first use this function) "
and "`endl' undeclared (first use this function) " they are both on the same line


#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
srand(time(0));

for(int x = 1; x<10;x++){
cout << (rand()%6) << endl;
}

system("pause");
}
try this:-

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));

for(int x = 1; x<10;x++){
cout << (rand()%6) << endl;
}

system("pause");
}

cheers
Isn't that what I already had
No, it isn't. You didn't specify where the cout object was. Also, I personally recommend against using statements as was done above because they can lead to troubles if you were to accidentally name a variable something in the namespace. practically defeating the point. Instead use the :: operator to get access to objects in the std namespace. For example the program above could be written more efficiently as:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstdlib>
#include <ctime>

int main(int argc, char *argv[]) {
    srand(time(0));
    for(int x = 1; x < 10; ++x) 
	std::cout << (rand()%6) << std::endl;
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    return 0;
}


As for the system commands... meh
Last edited on
Topic archived. No new replies allowed.