someone please edit my program

i am planning to make a game called spot the not.
the problem is i don't know how to have a timer and high score on it.
i already surf the net but i don't know how to apply it on my program.
many thanks in advance!

# include<iostream.h>
# include<windows.h>
# include<time.h>

void Easy(void);
void Average(void);
void Hard(void);
int rand();
int a, rownum, columnnum, round, row, column;



int main()
{
char level;
do
{
cout<<"Choose difficulty level!\nEasy[1]\nAverage[2]\nHard[3]\nExit[4]\nEnter code:";
cin>>level;
system("cls");

switch(level)
{
case '1': Easy(); break;
case '2': Average(); break;
case '3': Hard(); break;
case '4': break;
default : cout<<"Input Valid Code!"<<endl; break;
}
}
while (level!='4');

return 0;
}

void Easy(void)
{


for(round=1; round<=5; round+=0)
{
a=rand();
cout<<"Round "<<round<<"!"<<endl<<endl;

if (a<=100)
{

for(row=1; row<=10; row++)
{
for(column=1; column<=10;column++)
{
if(a==((row-1)*10)+column)
cout<<"1 ";
else
cout<<"7 ";
}

cout<<endl;
}


cout<<"Enter row number:";
cin>>rownum;
cout<<"Enter column number:";
cin>>columnnum;

if (a== ((rownum-1)*10)+columnnum)
cout<<"Correct!"<<endl;
else
cout<<"Wrong"<<endl;

round+=1;

}

cout<<endl;

}




}

void Average(void)
{

for(round=1; round<=5; round+=0)
{
a=rand();

cout<<"Round "<<round<<"!"<<endl<<endl;

if (a<=400)
{
for(row=1; row<=20; row++)
{
for(column=1; column<=20;column++)
{
if(a==((row-1)*20)+column)
cout<<"n ";
else
cout<<"m ";
}

cout<<endl;
}


cout<<"Enter row number:";
cin>>rownum;
cout<<"Enter column number:";
cin>>columnnum;

if (a== ((rownum-1)*20)+columnnum)
cout<<"Correct!"<<endl;
else
cout<<"Wrong"<<endl;

round+=1;
}

cout<<endl;

}




}

void Hard(void)
{

for(round=1; round<=5; round+=0)
{
a=rand();
cout<<"Round "<<round<<"!"<<endl<<endl;

if (a<=900)
{
for(row=1; row<=30; row++)
{
for(column=1; column<=30;column++)
{
if(a==((row-1)*30)+column)
cout<<"l ";
else
cout<<"1 ";
}

cout<<endl;
}


cout<<"Enter row number:";
cin>>rownum;
cout<<"Enter column number:";
cin>>columnnum;

if (a== ((rownum-1)*30)+columnnum)
cout<<"Correct!"<<endl;
else
cout<<"Wrong"<<endl;

round+=1;
}

cout<<endl;

}




}
What exactly is this game then?
And of course, be sure to get rid of those system calls.
Last edited on
7777777
7777777
7777717
7777777
7777777


you will enter the row and column of the different character

for that example

it is 3
then 6
you really need to use code tags for formatting reasons that code is hard to read and more importantly it took me around 10 minutes to add all the inserted tabs(which is slightly more annoying). But your program works fine, for time you can use ctime, and use the time function as below: For reasons why you shouldn't use system calls can be seen from reading the sticky posts at top of forum.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# include<iostream>
//# include<windows> // i dont have windows headers with DevC++ so can't use this
// removed system call below as well.
//# include<ctime> // this is not used... yet
using namespace std;

void Easy(void);
void Average(void);
void Hard(void);
int rand();
int a, rownum, columnnum, round, row, column;



int main()
{
	char level;
	do
	{
		cout<<"Choose difficulty level!\nEasy[1]\nAverage[2]\nHard[3]\nExit[4]\nEnter code:";
		cin>>level;
		//system("cls");  // not needed.
		 
		switch(level)
		{
			case '1': Easy(); break;
			case '2': Average(); break;
			case '3': Hard(); break;
			case '4': break;
			default : cout<<"Input Valid Code!"<<endl; break;
		}
	}
	while (level!='4');

return 0;
}

void Easy(void)
{

	for(round=1; round<=5; round+=0)
	{
		a=rand();
		cout<<"Round "<<round<<"!"<<endl<<endl;
		
		if (a<=100)
		{
		
			for(row=1; row<=10; row++)
			{
				for(column=1; column<=10;column++)
				{
					if(a==((row-1)*10)+column)
						cout<<"1 ";
					else
						cout<<"7 ";
				}
			
				cout<<endl;
			}
			cout<<"Enter row number:";
			cin>>rownum;
			cout<<"Enter column number:";
			cin>>columnnum;
		
			if (a== ((rownum-1)*10)+columnnum)
				cout<<"Correct!"<<endl;
			else
				cout<<"Wrong"<<endl;
			round+=1;
		}
		cout<<endl;
	}
}

void Average(void)
{

	for(round=1; round<=5; round+=0)
	{
		a=rand();

		cout<<"Round "<<round<<"!"<<endl<<endl;

		if (a<=400)
		{
			for(row=1; row<=20; row++)
			{
				for(column=1; column<=20;column++)
				{
					if(a==((row-1)*20)+column)
						cout<<"n ";
					else
						cout<<"m ";
				}
				cout<<endl;
			}
			cout<<"Enter row number:";
			cin>>rownum;
			cout<<"Enter column number:";
			cin>>columnnum;
			
			if (a== ((rownum-1)*20)+columnnum)
				cout<<"Correct!"<<endl;
			else
				cout<<"Wrong"<<endl;
			round+=1;
		} // endif
		cout<<endl;
	} //endfor
}

void Hard(void)
{

	for(round=1; round<=5; round+=0)
	{
		a=rand();
		cout<<"Round "<<round<<"!"<<endl<<endl;

		if (a<=900)
		{
			for(row=1; row<=30; row++)
			{
				for(column=1; column<=30;column++)
				{
					if(a==((row-1)*30)+column)
						cout<<"l ";
					else
						cout<<"1 ";
				}
				cout<<endl;
			}
			cout<<"Enter row number:";
			cin>>rownum;
			cout<<"Enter column number:";
			cin>>columnnum;
			
			if (a== ((rownum-1)*30)+columnnum)
				cout<<"Correct!"<<endl;
			else
				cout<<"Wrong"<<endl;
			round+=1;
		} // endif
		cout<<endl;
	}
}


TIME 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
/* clock example: countdown */
#include <stdio.h>
#include <time.h>
#include <iostream>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main ()
{
  int n;
  printf ("Starting countdown...\n");
  for (n=10; n>0; n--)
  {
    printf ("%d\n",n);
    wait (1);
  }
  printf ("FIRE!!!\n");
  return 0;
}


For the high score there's a number of ways you can do this. I'm guessing you want the scores to persist, so personally I would use a write the scores to a file, then when the program starts up read the file into an array of high scores and makes adjustments as needed. As high scores typically only contain 10-20 high scores a const int for array size.

Additionally for round2! "round 2!" printed out at least 50+ times because everything scrolled off the console. Only once or a few times is needed for this. it could be a loop error or intentional I'm not usre I didn't look at code closely.
Last edited on
where shall i insert the time code?
where ever you like. is there something you don't understand about it?
i can't understand how to insert the time code. Can you edit??
To put you on the right track heres an example from this site:

http://www.cplusplus.com/reference/clibrary/ctime/difftime/

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
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

int main()
{
	time_t start;
	time_t end;

	double timeTaken;
	int rownum;
	int columnnum;

	time(&start);

	cout<<"Enter row number:";
	cin>>rownum;
	cout<<"Enter column number:";
	cin>>columnnum;

	time(&end);

	timeTaken = difftime(end, start);
	cout << "Time taken = " << timeTaken << endl;

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* clock example: This function can be added to your class, 
 * calling it and passing an integer will make it wait a certain number of seconds
 * as an example: wait(55);  
 * will make the program pause for 55 seconds.
 * wait(1); will cause the program to pause for 1 second
 */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}


you have implemented your own functions this shouldn't be too hard for you to use. simply:
wait (n);
Last edited on
Yes but during that wait time you can't do anything? As it's stuck in the while loop.
ahh woops, I missread. I thought OP wanted a pause.
well you can get current time, and at the time you want to display the timer get the current time then get the difference between the two.

http://cplusplus.com/reference/clibrary/ctime/time/

just format end_time as necessary it will be number of seconds.

edit:
1
2
3
4
5
6
7
8
9
10
time_t start_time;
start_time = time (NULL);


// when you want to display timer:
time_t end_time;
end_time = time (NULL);
end_time -= start_time;

printf ("%ld", end_time);
Last edited on
As I have been looking into this for person reasons found this statement was incorrect. you need to use difftime for differences between time. while - may work. probably better to use this. it is stored as double.

1
2
3
4
5
6
7
8
9
10
11
time_t start,end;
double dif;

// start timer
time (&start);

// end timer
time (&end);

dif = difftime (end,start);
printf ("It took you %.2lf seconds to complete game.\n", dif );


p.s all code is simply ripped from this website documentation on time. I haven't tested.
Last edited on
That's the same as I did above...
i r good reeeder m8

ahaha.... hmmmm


.... where did I put that shotgun....


yea, I completely missed your first post.
Last edited on
.... where did I put that shotgun....

lol :P
Topic archived. No new replies allowed.