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

nilk10Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Your function definition doesn't match how you're calling it.

void Sixrand::showNumbers(int [])

numGeneration.showNumbers();

0
elimesikaCommented:
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

0
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

elimesikaCommented:
Sorry, have missed the zone , shame on me ;--))

0
evilrixSenior Software Engineer (Avast)Commented:
>> Sorry, have missed the zone , shame on me ;--))
It happens -- to me, many times :)
0
Chris-in-ClaphamCommented:
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.
0
nilk10Author Commented:
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

0
evilrixSenior Software Engineer (Avast)Commented:
This now builds, I commented what I changed with the prefix RX so you can see what was modified.
#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;

// RX: #include "Sixrand.h"
class Sixrand // Sixrand class definition
{
public:
        // RX: ~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
        void showNumbers(); // function that displays the random numbers
 
private:
        int randNumbers[6]; // random numbers

}; // end of class Sixrand

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;
        
                // RX: if (checkNumbers(randNumbers, anum))
                if (checkNumbers(anum, randNumbers))
                {
                        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;
}


// RX: void Sixrand::showNumbers(int [])
void Sixrand::showNumbers()
{
        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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris-in-ClaphamCommented:
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.

0
evilrixSenior Software Engineer (Avast)Commented:
>>  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.
0
Chris-in-ClaphamCommented:
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.
0
Chris-in-ClaphamCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.