Nested loops don't work well

hey there,
im coding a little game and I want to create 9 xy-coordinates which cantain a character (A-I) and a Number (0-8). The coordinates should not be twice in the array, so I check them. Just for seeing if it works, I want to print out my coordinates, but I get nothing and my program stops working.
What's wrong with my code?

btw: this isn't the whole code, just the part which don't work, if u need the full code, just say it :)
Thanks for help :)

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

#include "stdafx.h"

#include <iostream>

#include <time.h>

#include <Windows.h>

#include <stdlib.h>

#include <string>
#include <sstream>

using namespace std; 

int yWertKi[9];
int xWertKiZahl[9];
int a;

string KiXVergleich[9] = { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
string KiXWert[9];

string KiKoords[9];

int main(){
 	for (int i = 0; i < 9; i++)
	{
		startGen:
		KiXWert[i] = KiXVergleich[xWertKiZahl[i] = rand() % 8];
		yWertKi[i] = rand() % 8;
		KiKoords[i] = KiXWert[i] + to_string(yWertKi[i]);
		for (int j = 0; j < 9; j++)
		{
			if (KiKoords[i] == KiKoords[j])
			{
				goto startGen;
			}
		}

	}
      for (int i = 0; i < 9; i++)
	{
		cout << KiKoords[i] << "\n";
	}
         cin.sync();
	cin.get();
    return 0;
Last edited on
Line 35: What happens when i == j? KiKoords[i] == KiKoords[j] will always be true and you start over again. You will never get past the first iteration (i=0, j=0).

Line 37: goto should be avoided.


The rand() is deprecated. One should use the tools that are in <random>. That said, what is the largest number that N % 8 can be? It is not 8. In other words, you can never pick the "I" or 8. Can your loop end in those conditions?

The goto should be avoided too. One is better to be a genius, if one wants to use it properly.

Global variables have limitations too.


Now, the approach of drawing the same number multiple times simply to discard it is not efficient. You do know the valid unique values.

Think about a deck of cards. Each card is unique. If you draw two cards from the deck, what is the chance that they are identical? 0

How do you get a random card? You shuffle the deck before drawing the cards.

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
// shuffle algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::shuffle
#include <array>        // std::array
#include <string>        // std::string
#include <random>       // std::default_random_engine
#include <chrono>       // std::chrono::system_clock

int main () {
  constexpr size_t N = 12;
  std::array<std::string,N> foo;

  for ( size_t c=0; c<N; ++c ) {
      foo[c] = static_cast<char>('A' + c/4) + std::to_string( c%4 );
  }

  std::cout << "unshuffled elements:";
  for ( auto x : foo ) std::cout << ' ' << x;
  std::cout << '\n';

  // obtain a time-based seed:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  shuffle( foo.begin(), foo.end(), std::default_random_engine(seed) );

  std::cout << "shuffled elements:";
  for ( auto x : foo ) std::cout << ' ' << x;
  std::cout << '\n';

  std::cout << "chosen four elements:";
  for ( size_t x=0; x<4; ++x ) std::cout << ' ' << foo[x];
  std::cout << '\n';

  return 0;
}
Topic archived. No new replies allowed.