ki

b
Last edited on
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
#include <iostream>
#include <ctime>

int GenerateRandomNumber()
{
	int iRandom = rand() % 40;
	iRandom++;

	return iRandom;
}

void main()
{
	int iGuessOne, iGuessTwo, iGuessThree;
	int iLotteryOne, iLotteryTwo, iLotteryThree;

	std::cout << "Please enter your first lottery guess (1-40): ";
	std::cin >> iGuessOne;

	std::cout << "Please enter your second lottery guess (1-40): ";
	std::cin >> iGuessTwo;

	std::cout << "Please enter your third lottery guess (1-40): ";
	std::cin >> iGuessThree;

	if ( iGuessOne == iGuessTwo || iGuessOne == iGuessThree || iGuessTwo == iGuessThree )
	{
		std::cout << "Please try again using unique values for all three numbers" << std::endl;
		return;
	}

	if ( iGuessOne > 40 || iGuessOne < 1 || iGuessTwo > 40 || iGuessTwo < 1 || iGuessThree > 40 || iGuessThree < 1 )
	{
		std::cout << "Please try again using values between 1-40 for all three numbers" << std::endl;
		return;
	}

	srand( time( NULL ) );

	do
	{
		iLotteryOne = GenerateRandomNumber();
		iLotteryTwo = GenerateRandomNumber();
		iLotteryThree = GenerateRandomNumber();
	} while ( iLotteryOne == iLotteryTwo || iLotteryOne == iLotteryThree || iLotteryTwo == iLotteryThree );

	std::cout << "Your numbers: " << iGuessOne << " || " << iGuessTwo << " || " << iGuessThree << std::endl;
	std::cout << "Lottery numbers: " << iLotteryOne << " || " << iLotteryTwo << " || " << iLotteryThree << std::endl;

	if ( iLotteryOne == iGuessOne && iLotteryTwo == iGuessTwo && iLotteryThree == iGuessThree )
		std::cout << "You win!!" << std::endl;
	else
		std::cout << "You lose!!" << std::endl;
}
I ran this in Code::Blocks and got loads of errors. Mainly it complains about how "rand" and "srand" weren't declared and that main is a "void". And 29 and 35 are weird too. Is it the Code::blocks or did you mess up?
Some compilers / compiler configurations require main to return int, and take two arguments - try this for main instead:
 
int main(int argc, char *argv[])

As for rand and srand not being declared, try putting
 
#include <stdlib.h> 

at the top.
You may get better help if we knew which compiler you were using though.
closed account (z05DSL3A)
Vortex47 wrote:
it complains about ... and that main is a "void"
The Palm Tree Magician wrote:
Some compilers / compiler configurations require main to return int

The C++ standard requires that main returns an int, void main() has never been acceptable and should not be used (even if your compiler does allow it).
Last edited on
Like I already mentioned, I'm using Code::Blocks.

I thought that it was fishy to start with that the main wasn't an int, but that aside, it refuses to let the "return" after the if (iGuess etc, etc)


int main(int argc, char *argv[]) and #include <stdlib.h> helped, by the way.

EDIT: So I removed the return and it works now. This is a compiler issue only, right?
Last edited on
Using a void for your main function isn't a big deal at all, especially if it's just for beginner homework. The compiler is going to force it to an int anyway so the only difference is that the code looks cleaner.
Sorry for the double post but I just wanted to point out, with proof, that there's no difference assuming that you're using a good compiler. This testing was done with Visual Studio 2010 Express. As you can see, the dissasembly is the EXACT same. The only change I made was turning void main to int main and having it return 0.

void return assembly
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
push    ebp
mov     ebp, esp
push    0FFFFFFFFh
push    offset sub_402298
mov     eax, large fs:0
push    eax
sub     esp, 20h
mov     eax, ___security_cookie
xor     eax, ebp
mov     [ebp+var_10], eax
push    esi
push    edi
push    eax
lea     eax, [ebp+var_C]
mov     large fs:0, eax
mov     edi, 23h
lea     esi, [ebp+var_2C]
mov     [ebp+var_18], 0Fh
mov     [ebp+var_1C], 0
mov     byte ptr [ebp+var_2C], 0
call    sub_401300
mov     [ebp+var_4], 0
call    loc_401040
mov     ecx, ds:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::basic_ostream<char,std::char_traits<char>> std::cout
mov     eax, esi
push    eax
push    ecx
call    sub_401690
add     esp, 8
cmp     [ebp+var_18], 10h
jb      short loc_4011BA
mov     edx, [ebp+var_2C]
push    edx
call    ds:__imp_??3@YAXPAX@Z ; operator delete(void *)
add     esp, 4

loc_4011BA:
xor     eax, eax
mov     ecx, [ebp+var_C]
mov     large fs:0, ecx
pop     ecx
pop     edi
pop     esi
mov     ecx, [ebp+var_10]
xor     ecx, ebp
call    @__security_check_cookie@4 ; __security_check_cookie(x)
mov     esp, ebp
pop     ebp
retn
endp


void return pseudocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int __cdecl main(int argc, const char **argv, const char **envp)
{
  int v3; // ST04_4@1
  int v5; // [sp+8h] [bp-2Ch]@1
  int v6; // [sp+18h] [bp-1Ch]@1
  unsigned int v7; // [sp+1Ch] [bp-18h]@1
  unsigned int v8; // [sp+24h] [bp-10h]@1
  int v9; // [sp+30h] [bp-4h]@1
  int v10; // [sp+34h] [bp+0h]@1

  v8 = (unsigned int)&v10 ^ __security_cookie;
  v7 = 15;
  v6 = 0;
  LOBYTE(v5) = 0;
  sub_401300((unsigned int)&v10 ^ __security_cookie);
  v9 = 0;
  loc_401040(v3);
  sub_401690(std::cout, &v5);
  if ( v7 >= 0x10 )
    operator delete(v5);
  return 0;
}


int return assembly
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
push    ebp
mov     ebp, esp
push    0FFFFFFFFh
push    offset sub_402298
mov     eax, large fs:0
push    eax
sub     esp, 20h
mov     eax, ___security_cookie
xor     eax, ebp
mov     [ebp+var_10], eax
push    esi
push    edi
push    eax
lea     eax, [ebp+var_C]
mov     large fs:0, eax
mov     edi, 23h
lea     esi, [ebp+var_2C]
mov     [ebp+var_18], 0Fh
mov     [ebp+var_1C], 0
mov     byte ptr [ebp+var_2C], 0
call    sub_401300
mov     [ebp+var_4], 0
call    loc_401040
mov     ecx, ds:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::basic_ostream<char,std::char_traits<char>> std::cout
mov     eax, esi
push    eax
push    ecx
call    sub_401690
add     esp, 8
cmp     [ebp+var_18], 10h
jb      short loc_4011BA
mov     edx, [ebp+var_2C]
push    edx
call    ds:__imp_??3@YAXPAX@Z ; operator delete(void *)
add     esp, 4

loc_4011BA:
xor     eax, eax
mov     ecx, [ebp+var_C]
mov     large fs:0, ecx
pop     ecx
pop     edi
pop     esi
mov     ecx, [ebp+var_10]
xor     ecx, ebp
call    @__security_check_cookie@4 ; __security_check_cookie(x)
mov     esp, ebp
pop     ebp
retn
endp


int return pseudocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int __cdecl main(int argc, const char **argv, const char **envp)
{
  int v3; // ST04_4@1
  int v5; // [sp+8h] [bp-2Ch]@1
  int v6; // [sp+18h] [bp-1Ch]@1
  unsigned int v7; // [sp+1Ch] [bp-18h]@1
  unsigned int v8; // [sp+24h] [bp-10h]@1
  int v9; // [sp+30h] [bp-4h]@1
  int v10; // [sp+34h] [bp+0h]@1

  v8 = (unsigned int)&v10 ^ __security_cookie;
  v7 = 15;
  v6 = 0;
  LOBYTE(v5) = 0;
  sub_401300((unsigned int)&v10 ^ __security_cookie);
  v9 = 0;
  loc_401040(v3);
  sub_401690(std::cout, &v5);
  if ( v7 >= 0x10 )
    operator delete(v5);
  return 0;
}
closed account (z05DSL3A)
codingshark wrote:
Using a void for your main function isn't a big deal at all, especially if it's just for beginner homework. The compiler is going to force it to an int anyway so the only difference is that the code looks cleaner.

Obviously this is NOT the case as Vortex47s spat its dummy at your code.
Why even bother defending the use of void main()??? It's an extra character to type and offers no advantages. It's also an unnecessary potential confusion for beginners that could be avoided. Don't propagate it's use.
Topic archived. No new replies allowed.