Link to home
Start Free TrialLog in
Avatar of nilk10
nilk10

asked on

Generating random numbers within a certain range

I keep on getting this error message:

nine.cpp(37) : error C2228: left of '.showNumbers' must have class/struct/union

I am not sure why I am getting this error and it is the only one.  Therefore I can not see if the program is doing what it is supposed to do.

Help!
.cpp file

#include "StdAfx.h"

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

#include "Sixrand.h"

Sixrand::Sixrand()
{
}

void Sixrand::setNumbers(int int_numbers[6])
{
	randNumbers[6] = int_numbers[6];
}

int Sixrand::getNumbers()
{   
	int num_count = 0; // counter for the number of valid number 
	
	while (num_count < 6)
	{
		srand(unsigned (time(0)));
		
                int anum = 1 + rand() % 53;
	
		if (checkNumbers(randNumbers, anum))
		{
			randNumbers[num_count] = anum;
			num_count++;
		}
	}

	return randNumbers[6];
}

bool Sixrand::checkNumbers(int anum, int int_numbers[6])
{
	for (int k=0; k < 6; k++)
	{
		if (int_numbers[k] == anum || int_numbers[k] == 0)
		{
			return false;
		}
	}
	return true;
}


void Sixrand::showNumbers(int [])
{
	getNumbers();

	for (int i = 0; i < 6; i++)
	{
		cout << getNumbers() << " " << endl;
	}
}


int main()
{
	Sixrand numGeneration; //create Sixrand object

 	numGeneration.showNumbers();

	return 0; 

}

Open in new window

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Your function definition doesn't match how you're calling it.

void Sixrand::showNumbers(int [])

numGeneration.showNumbers();

Also, you are just defining an object and forgot to create it , which will lead to Null Reference Exception at run time , fix with
int main()
{
	Sixrand numGeneration=new Sixrand(); //create Sixrand object

 	numGeneration.showNumbers();

	return 0; 

}

Open in new window

>> Also, you are just defining an object and forgot to create it , which will lead to Null Reference Exception at run time , fix with
This is C++ not C#, there is nothing wrong with the definition of numGeneration, it is a stack based object.

The example you give is invalid C++ and will not compile.
Sorry, have missed the zone , shame on me ;--))

>> Sorry, have missed the zone , shame on me ;--))
It happens -- to me, many times :)
Avatar of Chris-in-Clapham
Chris-in-Clapham

Please can you show the contents of Sixrand.h. I'm assuming this should contain the class decleration. The error message suggests that the compiler doesn't understand Sixrand to be a valid class.
Avatar of nilk10

ASKER

Attached is the Sixrand class
class Sixrand // Sixrand class definition
{
public:
	~Sixrand();
	Sixrand(int); // constructor that initializes a Sixrand object
	Sixrand();
	void setNumbers(int []); // function that gets the random numbers
	int getNumbers(); // function that retrieve the random numbers
	bool checkNumbers(int, int []); // function that evaluates the random numbers
	void showNumbers(int []); // function that displays the random numbers
 
private:
	int randNumbers[6]; // random numbers

}; // end of class Sixrand

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The header looks fine. When I compile it in VS2008 I get two errors. The first, as evilrix points out, is that as defined the method showNumbers should take an array of integers but is being passed no value. As the input parameter is never used it could just be dropped from the definition. The other error I get is on line 38 of your original source where you call checkNumbers with an array of integers as the first parameter instead of a single integer as the prototype specifies.

>>  where you call checkNumbers with an array of integers as the first parameter instead of a single integer as the prototype specifies.
The parameters are inverted.
Incidentally, the line
    randNumbers[6] = int_numbers[6];
is probably not doing what you intended. The method name suggests that it should copy the whole of int_numbers into randNumbers. However, what it will actually do is try to assign past the end of randNumbers (an array with 6 elements has valid indexes from 0...5). This isn't a problem at the moment as the method isn't being used anywhere. You could either use a loop to do the assignment or you could use a container class (e.g. a vector) which would come with a copy constructor.
Looking more closely, I don't think the code will work as it stands even if you do fix the compile errors. When you first create your object there is no initialisation of the randNumbers array. There is therefore no guarantee as to what it will contain. The odds are though that at least one of the values will be zero.

The first time through the loop in getNumbers a call will be made to checkNumbers. This will find the zero in one of the positions and return false. No value will be set in randNumbers and so the loop will carry on indefinitely.

A solution to this would be to pass num_count into checkNumbers along with the other parameters (although strictly speaking there is no need to pass randNumbers as it is a class member and so accessible from within checkNumbers anyway). You could use this value as the upper limit of the for loop and therefore only check anum against array values that have been assigned.

As a final point around good coding practice, consider what you would have to do if you wanted your code to generate 7 or 8 (or N) random numbers instead of 6. Because the number 6 is hard coded in multiple places this would be non-trivial.