compare fields in an array

Hi, I have started a programming course and its being going well until now. I have to create a 3 number lotto program, the possible outcomes are 3 numbers match in sequence i.e. 1,2,3 and 1,2,3. 3 numbers match i.e 1,2,3 and 2,1,3 then two numbers, one number and no numbers.

I may be going about this all wrong but here is what I have done so far. I want it to compare the first number in the array [0][0] with [1][0] if these match then goto sequencematch1, this will then check if [0][1] matches [1][1] etc or else goto not in sequence and check if any numbers match [0][0.

It is checking if the number matchs but and adds the relevant number. But it then goes on and goes through everything else instead of skipping it. Some things are commented out for testing purposes. Any ideas?

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
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <stdio.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int lottonum1, lottonum2, lottonum3, lotto[3][3],  amount;//random1, random2, random3,
	char repeat;
	
	
	do
	{
		srand(time(NULL));
		amount = 0;

		cout<<"\n\n\tEnter Lotto Number 1 : ";
		cin>>lottonum1, lotto[0][0] = lottonum1;
		//cout<<"\n\n\tEnter Lotto Number 2 : ";
		//cin>>lottonum2, lotto[0][1] = lottonum2;
		//cout<<"\n\n\tEnter Lotto Number 3 : ";
		//cin>>lottonum3, lotto[0][2] = lottonum3;
	
		lotto[1][0] = 1;//rand() % 10;
		lotto[1][1] = 2;//rand() % 10;
		lotto[1][2] = 3;//rand() % 10;

		cout<<"\n\n\tFirst Number Drawn : "<<lotto[1][0];
		cout<<"\n\tSecond Number Drawn : "<<lotto[1][1];
		cout<<"\n\tThird Number Drawn : "<<lotto[1][2];
		
		amount = 0;
		cout<<"\n\tAmount before : "<<amount;
		if (lotto[0][0] == lotto[1][0]){
			 amount =+ 100, cout<<"\n\tAmount :"<<amount;
				 goto sequencematch1;}
		else if (lotto [0][0] != lotto[1][0]){
			goto notinsequence;}
notinsequence:
		if (lotto[0][0] == lotto[1][1]){
			amount =+ 30, cout<<"\n\tAmount :"<<amount;
				goto match1;}
		else if (lotto[0][0] != lotto[1][1]){
			goto match1notinsequence;}

match1notinsequence:
		if (lotto[0][0] == lotto[1][2]){
			amount =+ 30, cout<<"\n\tAmount :"<<amount;
				goto match1;}
		else if (lotto[0][0] != lotto[2][0]) {
			goto nomatch1;}
		
sequencematch1:
cout<<"\n\t1st Number in sequence match";

match1:
cout<<"\n\t1 Match not in sequence";

nomatch1:
cout<<"\n\tNo match with first number";


		cout<<"\n\n\tRepeat Program? (Y/N)";
		cin>>repeat;

	}while(repeat == 'y' || repeat == 'Y');
	return 0;
}


Last edited on
Just found the problem, the goto destinations were not in the right place, all down to layout
closed account (o3hC5Di1)
Hi there,

Please note that using goto is considered to be a bad practise in a lot of cases. Mostly because it causes program control to jump from one place to another. In your case you could just include the code of the goto-s in the if/else-if statements, or you could create a separate function for each goto statement.

Please do let us know if you have any further questions.

All the best,
NwN


Topic archived. No new replies allowed.