srand troubles

1
2
3
4
5
6
7
8
9
10
#include <iostream.h>
#include <windows.h>
#include <time.h>



int x, y, z, a, b, c;
int numbers[200];

srand(time(NULL));


I'm being told:

g:\c++\2004_two\2004_two.cpp(11) : error C2501: 'srand' : missing storage-class or type specifiers
g:\c++\2004_two\2004_two.cpp(11) : error C2373: 'srand' : redefinition; different type modifiers
c:\program files\microsoft visual studio\vc98\include\stdlib.h(308) : see declaration of 'srand'

Anybody know why?
1) you must #include <cstdlib> (or <stdlib.h> if this is C)

2) You can't have executable code outside of a function (usually). Put the srand call inside your main() -- don't have it global
Thanks!
#include <iostream.h>

I'd guess from this horrifically ancient header that the OP is using a decade-old compiler, in which case #include <cstdlib> might have to be #include <cstdlib.h> .

Please, get a compiler that conforms to at least one of the C++ standards.

Edit: I see from other threads that you do indeed have some kind of FrankenCompiler that accepts code that isn't C++. You're doing yourself no favours by learning ... whatever it is that you're learning. :)
Last edited on
@Moschops, lol, it's Visual c++ 6.0 enterprise, it sucks. I have express 2012 at home, but at school they make me use 6.0
And indeed, why not. Why should the education system teach you modern (correct) C++ using free tools far better than VS6, when they could cripple your abilities instead? Marvellous.
@Moschops agreed. So, apologies for what appears to be frankencode. it is.

I was just told that my void{ } had a syntax error "}" I hate this compiler.
Last edited on
Sorry to resurrect this post. Same program, new dumb problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//MAIN

void main()
{

    srand(time(NULL));

	x=0;

	do
	{
		numbers[x]=rand() % 1000+1;

		y=0;

		do
		{

			if(numbers[x]==numbers[y])
			{x--; break;}

			y++;

		}while(y<x);

		x++;
	}while(x<200);


	//Least to greatest

	LTG_bubble_sort(x);

	//Cout

	y=0;

	do
	{
		y=0;

		do
		{
				cout << numbers[y] << "   ";
				y++;
		}while(y<10);

		cout << '\t';
	}




}


G:\C++\2004_two\2004_two.cpp(100) : error C2059: syntax error : '}'

I can't figure out why.
void main()
Not C++. C++ demands int main()

Your final do is missing its while.

Why does c++ demand int main()? I'll change it, I'm just wondering. Thanks on the do-while.
The simple answer is because the actual definition of C++, ISO IEC 14882-2011 (and earlier versions), section 3.6.1.2 states
It shall have a return type of type int
Last edited on
Is that written on stone tablets? :P okay haha
programs output a value to the OS on exit. This can be useful in batch files to know whether a program completed its job successfully, which can abort the script (ie, a compiler returning a failure code will let the build script know that it shouldn't try to run the linker)

main has to return an int to give the OS that code.
So are void functions okay for the others? it's only main that returns a value to the OS right?
Topic archived. No new replies allowed.