can someone please give me a hand in adding arrays to my game here is what i have.

#include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>

using namespace std;

void initMessages()
{
const char* END_OF_ARRAY = "end_of_array";
const int YES_MSG = 0;
const int NO_MSG = 1;
}
char* getMessage(const int messageId)
{
return messages[messageId].c_str();
}

int main()
{
	int a,b,c,d,e,f,g;                      // Declaration of strings
	string str1, str2, str3;

	e = 1;
	
	string theArray[2];
	theArray[3] = END_OF_ARRAY;

    
	while (e == 1)
	{
		g = 11;                             // G represents the Varible counter
		a = 0;								// Random num generated from zero
		f = 0;
	string str2 = "yes";
	string str3 = "no";

	for(c = 4; c <=100; c++)
	{
		srand((unsigned)time(NULL));        // Set a seed for random-num Generator.
		a = (rand() % 100) +1;

	}

    d = 11;
	while (d > 0)
	{
	g = g-1;
	d = d-1;
	if (d == 10)
	{
		cout << "Number of tries left: " << g << endl << endl;
		cout << "Hello" << endl;
		cout << "Welcome to my Guessing Game" << endl;
		cout << "Try to guess a number between 1 and 100 in under 10 tries: ";

	    cin >> b;
	}

	else if ( (d == 9) && (b == a))
	{
		cout << "Number of tries left: " << g << endl << endl;
		cout << "Good Job" << endl;
	d = 0;
	}

	else if ( (d < 10) && (b < a))
	{
		cout << "Number of tries left: " << g << endl << endl;
		cout << "Your guess was too low, why don't you guess again?  ";
		cin >> b;
	}
	else if ( (d < 10) && (b > a))
	{
		cout << "Number of tries left: " << g << endl << endl;
		cout << "Your guess was too high, why don't you guess again?  ";
		cin >> b;
	}
	else if ( (d < 10) && (b == a))
	{
		cout << "Number of tries left: " << g << endl << endl;
		cout << "Took you long enough!!! You finally got it!" << endl;
	d = 0;
	}
	}
if ( (d == 0) && (b != a))
{
	cout << "The number was: " << a << endl << endl;
	cout <<  "Too Bad was your brain tired?. You didn't manage to guess it.\nWas this your first time playing?";
}
else if ( (d == 0) && (b == a))
		{
			cout << "Number of tries left: " << g << endl << endl;
            cout << "CONGRATULATIONS!!! You finally got it!" << endl;
	d = 0;
	}

while (f != 1)
{
            cout << endl << endl << "Would you like to play again? (yes/no) ";
            cin >> str1;

if (str1.compare(str2) == 0)
{
	e = 1;
	f = 1;
}
else if (str1.compare(str3) == 0)
{
	e = 2;
	f = 1;
}
else
{
	cout << "Can you hit yes or no?";
}
	}
}
    cin.get();
    cin.get();
	return 0;
}

Recommended Answers

All 8 Replies

If you can give me (us) an idea of what you want to use the arrays for, it will help us direct the focus.

I am just trying to add and array for counting into my game and i am getting stuck

There are MANY problems with you code i'm just gonna post a few to get you started with something to sort out.

My first comment is that you should be using #include <ctime> rather than time.h

number 2:

char* getMessage(const int messageId)
{
    return messages[messageId].c_str();
}

You have not defined messages...i suggest you do something about that.

number 3:

string theArray[2];
	theArray[3] = END_OF_ARRAY;

theArray[3] doesn't exsist...yu declare and array of size 2 giving you elements 0..1 then you try and access number 3...not possible its an out of range error and is not safe.
Since his is a double problem here is part 2. END_OF_ARRAY is no definded within this scope thus you cannot use it here.

number 4:

for(c = 4; c <=100; c++)
	{
	    srand((unsigned)time(NULL));        // Set a seed for random-num Generator.
		a = (rand() % 100) +1;
	}

You only need to seed rand() once in your program not 97 times. Also what is the point in generating 97 random numbers when it is only the final one that you will actually use!

And last one is not so much a problem just will help you and everyone else understand what is going on.
Use good descriptive variables names...rather than things like a b c str1 which mean nothing to anybody.

Chris

I agree with all of Freaky_Chris's suggestions too.

When you add the arrays for counting, what will they contain?

Will they contain the guesses that they have already made?

Will they contain something about the numbers from previous games?

Arrays are generally for a collection of something...what do you want to collect?

I have changed the <time.h> to <ctime> but since my program is small and that it does not judge a score or take in a lot of numbers into the program since it is rand generated i have found it very hard to incoorperate an Array into my simple game. I would like to have an Array just count the average times it took to guess the number.

I have changed the <time.h> to <ctime> but since my program is small and that it does not judge a score or take in a lot of numbers into the program since it is rand generated i have found it very hard to incoorperate an Array into my simple game. I would like to have an Array just count the average times it took to guess the number.

Question: why are you adding an array? Just to have one? You should look at what you want to accomplish with the program and step back and ask yourself, "What do I need to make this happen?" You may well decide that you need an array, but don't start with "I need an array, how can I make my program so it has one?" Start with the question "What data do I need to store?" From that, the next question is "What is the best way to store this data?' The answer may well be to use an array. As Freaky_Chris mentioned, use descriptive names. They will help both you and us and will clarify what needs to be stored, which is Murtan's point too, I believe. Answering the questions Murtan posed will help you in the design.

i would like to use the Array to use for previouse games, and use the arry for using to store how many guesses it took to count.

I you would like to keep track of previous executions of your program I suggest you to write these informations in a file - you can't keep an array from one execution to another afaik Oo

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.