I have problem on my program. Can anyone help me to fix it and explain to me a little bit?

#include <iostream>
#include <iomanip>

using namespace std;

void displayResult();
void welcomeMessage();
void game();

int main()
{
	welcomeMessage();
	displayResult();
	displayResult(frequency, expected, intRepeats);

} // end main

void welcomemessage
{
	cout << "Dice Rolling Simulation\n\n" 
		 << "This program allows you to simulate the rolling of a pair of dice.\n" << endl; 
	
}

void game()
{
	unsigned long x;										
	unsigned long modulus = 2147483647;  
	unsigned long multiplies = 16807;						
	unsigned long inciement = 0;							 
	unsigned long intSeeds;		 
	float num;		 
	int intRepeats, roll;	 
	int roll_1, roll_2;	 


	cout << "Enter random number seed: ";		 
	cin >> intSeeds; 
		 
	cout << "Enter the number of rolls: ";		 
	cin >> intRepeats;  
	
	x = intSeeds;											//x is the PRN, the first user selected  
		 
	for (int i = 0; i < intRepeats; i++)	 
	{ 
		x = ((multiplies * x) + inciement) % modulus;	 
		num = x / float (modulus);  
		roll_1 = int (num * 6) +1;						

		x = ((multiplies * x) + inciement) % modulus; 
		num = x / float (modulus);  
		roll_2 = int (num * 6) +1;	

		roll = roll_1 + roll_2;								 
		frequency[(roll)]++;								
	}
		
	return 0;
}

void displayResult(const int arraySize, double frequency[], double expected[], int intRepeats, double exptRoll[])
{
	const int arraySize = 13;								
	double frequency[arraySize] = { 0 };					
	double expected[arraySize] = {0, 0, 2.8, 5.6,			
					8.3, 11.1, 13.9,		
					16.7, 13.9, 11.1, 
					8.3, 5.6, 2.8}; 

	double exptRoll [arraySize] = {0,  1.0/36, 2.0/36.0,		
                                        3.0/36.0, 4.0/36.0,			
					5.0/36.0, 6.0/36.0, 
					5.0/36.0, 4.0/36.0, 
					3.0/36.0, 2.0/36.0, 
					1.0/36.0};


	cout << "\nGenerating rolls... \n " << endl;
	cout << "\nFinal statistics: \n " << endl;			


	//Output the results of the dice rolling (sum, frequency, actual percent and expected rolls, expected percent)  
	cout << ""  << endl;												
	cout << "Point" << setw(17) << "Number of Rolls" << setw(17) << "Actual Percent" 
					<< setw(17) << "Expected Rolls" << setw(17) << "Expected Percent" << endl;

	for (int value = 2; value < arraySize; value++)										//begin generating the table
  	{
		cout << setw(5) << value  ;			
		cout << setw(17) << static_cast<int>(frequency[value]) ;
		cout << setw(16) << ((frequency[value]/intRepeats)*100) << "%" ;
		cout << setw(17) <<  (int)(exptRoll[value-1] * intRepeats);	
		cout << setw(16) << setprecision (3) << expected[value]<< "%"  << endl;	
	}

}

Dani AI

Generated

This thread shows a few distinct problems that cause compiler errors and wrong results: mismatched function declarations/definitions, undefined identifiers at the call site, scope/name typos, and a few logic/initialization pitfalls. correctly flagged that symbols passed to displayResult are not visible in main. pointed out the name/signature mismatches and suggested globals; passing data is usually clearer and safer than globals.

Concrete fixes and a clean workflow:

  • Make every prototype match its definition exactly (same name, same parameter list).
  • Have game produce the roll data (frequency counts + number of rolls) and have displayResult accept those as parameters (const reference for arrays/vectors). Avoid return in non-main functions.
  • Initialize the frequency container before rolling.
  • Compute percentages with floating-point arithmetic (cast at least one operand to double).
  • Align expected probabilities so indices correspond to roll sums (or explicitly document a shift).

Small example of recommended signatures and main flow (conceptual; not a copy of the posted code):

#include <vector>

void welcomeMessage();
std::vector<int> game(unsigned long seed, int &rollCount);   // returns frequency, writes rollCount
void displayResult(const std::vector<int>& frequency, int rollCount, const std::vector<double>& expectedPct);

int main() {
  welcomeMessage();
  int rolls = 0;
  auto freq = game(seedValue, rolls);
  displayResult(freq, rolls, expectedPercentages);
}

Practical tips:

  • Use std::vector<int> freq(arraySize, 0) to zero-initialize counts.
  • Percent = 100.0 * freq[i] / double(rollCount).
  • Keep array size consistent (e.g., size 13 to index 0..12 so sums 2..12 are safe).
  • For random numbers prefer <random> (std::mt19937 + std::uniform_int_distribution) or ensure your LCG uses types wide enough to avoid overflow (uint64_t where appropriate).
  • Fix typos/case mismatches (for example welcomeMessage vs welcomemessage) and remove non-portable calls like system("pause").

These changes resolve the compile-time name/scope errors and make the statistics calculation robust and clear.

Recommended Answers

All 6 Replies

So whats' the problem?

So whats' the problem?

trying to put three functions into the main

Where is your arguments defined? frequency, expected, intRepeats?

displayResult(frequency, expected, intRepeats);

Where is your arguments defined? frequency, expected, intRepeats?

displayResult(frequency, expected, intRepeats);

I declare all the variables in their functions. I not sure I do the right way or not.

If you use the variable in global scope then you dont have to pass anything to the function,

your function declaration had no parameters,, and the function itself has 4.

The function name was misspelled welcomemessage instead of welcomeMessage
and needed () at the end

welcomeMessage()

#include <iostream>
#include <iomanip>

using namespace std;

void displayResult();
void welcomeMessage();
void game();

const int arraySize = 13;
double frequency[arraySize];					
double expected[arraySize] = {0, 0, 2.8, 5.6,			
					8.3, 11.1, 13.9,		
					16.7, 13.9, 11.1, 
					8.3, 5.6, 2.8}; 

double exptRoll [arraySize] = {	0,  1.0/36, 2.0/36.0,		
								3.0/36.0, 4.0/36.0,			
								5.0/36.0, 6.0/36.0, 
								5.0/36.0, 4.0/36.0, 
								3.0/36.0, 2.0/36.0, 
								1.0/36.0};

int main()
{
	welcomeMessage();
	displayResult();
	game();
	displayResult();
	system("pause");
	return 0;

} // end main

void welcomeMessage()
{
	cout << "Dice Rolling Simulation\n\n" 
		 << "This program allows you to simulate the rolling of a pair of dice.\n" << endl; 
	
}

void game()
{
	unsigned long x;										
	unsigned long modulus = 2147483647;  
	unsigned long multiplies = 16807;						
	unsigned long inciement = 0;							 
	unsigned long intSeeds;		 
	float num;		 
	int intRepeats, roll;	 
	int roll_1, roll_2;	 


	cout << "Enter random number seed: ";		 
	cin >> intSeeds; 
		 
	cout << "Enter the number of rolls: ";		 
	cin >> intRepeats;  
	
	x = intSeeds;											//x is the PRN, the first user selected  
		 
	for (int i = 0; i < intRepeats; i++)	 
	{ 
		x = ((multiplies * x) + inciement) % modulus;	 
		num = x / float (modulus);  
		roll_1 = int (num * 6) +1;						

		x = ((multiplies * x) + inciement) % modulus; 
		num = x / float (modulus);  
		roll_2 = int (num * 6) +1;	

		roll = roll_1 + roll_2;								 
		frequency[(roll)]++;								
	}
}

void displayResult()
{
	int intRepeats = 1;			
	
	cout << "\nGenerating rolls... \n " << endl;
	cout << "\nFinal statistics: \n " << endl;			


	//Output the results of the dice rolling (sum, frequency, actual percent and expected rolls, expected percent)  
	cout << ""  << endl;												
	cout << "Point" << setw(17) << "Number of Rolls" << setw(17) << "Actual Percent" 
					<< setw(17) << "Expected Rolls" << setw(17) << "Expected Percent" << endl;

	for (int value = 2; value < arraySize; value++)										//begin generating the table
  	{
		cout << setw(5) << value  ;			
		cout << setw(17) << static_cast<int>(frequency[value]) ;
		cout << setw(16) << ((frequency[value]/intRepeats)*100) << "%" ;
		cout << setw(17) <<  (int)(exptRoll[value-1] * intRepeats);	
		cout << setw(16) << setprecision (3) << expected[value]<< "%"  << endl;	
	}

}

is what you like I think,

int main()
{
	welcomeMessage();
	game();
	displayResult();
	system("pause");
	return 0;

} // end main
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.