Hello! I'm a newbie here...

Straight to the point, I tried to create a program as stated in the title above musing C++. Below are the code I used:

#include <iostream>
#include <cstdlib>


const board=8;
const pop=20;

using std::cout;
using std::endl;


double table[board][board];
int r;
int c;

int main()

{

	// Initialize random number generator.


	for(r=0; r<pop; r++)//row
	{ 
		for(c=0; c<board; c++)

		table[r][c] = rand() % board-1;

	}

	cout << table[r][c] << endl; //display table


	return 0;

}

but, it fail to link. the error was:

Linking...
LIBCD.lib(wwincrt0.obj) : error LNK2001: unresolved external symbol _wWinMain@16
Debug/test2.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

test2.exe - 2 error(s), 0 warning(s)

can someone help me how to solve this..?

Dani AI

Generated

As hinted, the immediate linker problem usually comes from building the wrong project type: the linker is looking for a GUI entry point while the code defines a normal main. Switching the project to a Console application (or changing the Linker SubSystem to Console) fixes the link stage. ’s revised build shows that compiling as a console app gets past the linker, but there are several logic and safety issues left in the original code that cause undefined behavior or wrong results.

Common mistakes to fix

  • Declare compile-time constants with types, e.g. const int board = 8;.
  • Match array dimensions and loops: the number of rows you write (pop) must be the number of rows the array reserves. Writing beyond the declared size causes undefined behavior.
  • Don’t print using loop indices after the loops have finished; print inside the nested loop to show every element.
  • Be careful with rand() expressions: rand() % board - 1 subtracts after the modulus (giving negative values). Use rand() % board for 0..board-1, or rand() % (board-1) + 1 for 1..board-1. Seed the generator with srand((unsigned)time(nullptr)) if using rand().
  • Prefer safer containers (std::vector) and C++11 <random> (std::mt19937 + std::uniform_int_distribution) to avoid VLA/overflow and get better randomness.

Compact example (C++11) that avoids those pitfalls:

#include <vector>
#include <random>
#include <iostream>

int main() {
  int rows = 20, cols = 8;
  std::vector<std::vector<int>> table(rows, std::vector<int>(cols));
  std::mt19937 rng{std::random_device{}()};
  std::uniform_int_distribution<int> dist(0, cols - 1);
  for (int r = 0; r < rows; ++r) {
    for (int c = 0; c < cols; ++c) {
      table[r][c] = dist(rng);
      std::cout << table[r][c] << ' ';
    }
    std::cout << '\n';
  }
}

Final notes: avoid system("pause") unless you need a Windows-specific pause, use size_t or int consistently for indices, and verify project settings in the IDE if the linker still expects a GUI entry point. confirmed the problem was solved after the adjustments.

Recommended Answers

All 4 Replies

Do you have it set up as a Win32 console application or did you choose Win32 project?

erm... I think that is the problem. maybe I've clicked at Win32 application, rather than Win32 console application. I'll change it first and try to see the output.

Anything will be update later. Thanks for the help..

#include <iostream>
#include <stdlib.h>

 int main()
 {  

const int board=8;
const int pop=20; 
using std::cout;
using std::endl; 
double table[board][board];

int r;
int c;

system("pause");
// Initialize random number generator.  

    for(r=0; r<pop; r++)//row
    {       

    for(c=0; c<board; c++)
    { 
        table[r][c] = rand() % 8 -1 ;// % board-1; 

    }   cout << table[r][c] << endl; //display table
    system("pause");    
}

system("pause");
//return 0; 

}

THis worked !

After some corrections, I manage to do it. Thanks for all the help! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.