Brain explosion

Feb 15, 2011 at 8:51pm
Hey dudes, please help me out with this code.
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
#include <windows.h>
#include <ctime>
#include <conio.h>
#include <iostream>
#include <string>
 
using namespace std;
 
 
 
int main ()
{
cout << "Hello!" << endl;
 
int amount = 0;
int primary = 0;
int turn = 0;
int time = 1;

const int SEC = time;
time = 2;
clock_t delay = SEC *CLOCKS_PER_SEC;
clock_t start = clock();
 
         while(clock() - start < delay){
		if(clock() - start == delay){
			cout << turn << endl;
			cout << "lol" << endl;
           }
 
while (primary == 0){ 
if(amount >= 1) { 
	if (turn == 0) {
		turn = turn + 1;
	}
}
 
if(amount >= 2) {
	if (turn == 1) {
		turn = turn + 1;
      }
}
           
if(amount >= 3) {
	if (turn == 2) {
		turn = turn - 2;
				}
			}
		}
	}
}

For some reason the loop doesn't start and it only prints me "Hello!".

Any help is appreciated.
Feb 15, 2011 at 9:04pm
If you want to wait for a period of time before executing some code, I'd suggest you use the sleep() method. You're code is putting the application into a high-demand loop for that period of time (using 100% of core/cpu).

clock() - start == delay is likely to never be true. Think more about the flow of logic you want to have in your application and design it accordingly.

It'd also be helpful if you told us what your application is suppose to do. It's currently got very poor indentation which makes the flow of logic hard to follow.
Feb 16, 2011 at 12:09pm
Thanks dude! Exactly what I needed.

I'll keep that in mind ;)
Feb 16, 2011 at 12:27pm
Your loop should look like this (this is only regarding formatting):
1
2
3
4
5
6
while (primary==0)
{ 
  if (amount>=1 && turn==0)turn++;
  if (amount>=2 && turn==1)turn++;
  if (amount>=3 && turn==2)turn-=2;
}


There are few things that can make source code more unreadable than braces (both curly and regular ones).
Avoid them whenever possible. The same goes for vast amounts of unnecessary whitespace.
Feb 16, 2011 at 12:49pm
Yea, I know. I'm just too lazy :)
Thanks.
Topic archived. No new replies allowed.