Explain Arrays Please

Pages: 1... 6789

Well keeping things simple, look at this example..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include <iostream>
using namespace std;

int main()
{
	// assume this is my structure array, all works the same
	char vowels[5] = { 'a', 'e', 'i', 'o', 'u' };

	// number of letters, just like your number of creatures.
	int letterCountMax = 5;
	// used by the do..while to move through the array
	int index = 0;

	// assume this i was passed using cin to letter variable  :)
	char letter = 'i';	

	// simple check to see if we found something.
	bool found = false;

	do
	{
		// have we got the letter we are looking for?
		if (vowels[index] == letter) {
			
			// ive found the letter  :)

			// create a loop starting from the index where
			// my letter was found to the last element stored
			// in letterMaxCount - 1 (i.e 0-4 = 5 chars)
			for (int i = index; i < letterCountMax; i++)
				vowels[i] = vowels[i + 1];

			// since we removed one, deduct the number of
			// elements (like your creatures one)
			letterCountMax--; 

			found = true;	// found it.

			// break out the loop
			break;

		}
		else
			index++;	// move up

	} while (index < letterCountMax);

	if (!found) 
		cout << endl << "Couldnt find what you were looking for..." << endl << endl;
	else
	{
		// lets just show what we have now
		for (int i = 0; i < letterCountMax; i++)
			cout << "Letter at index " << i << " is " << vowels[i] << endl;
	}

	return 0;
}
Like this?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void deleteCreature(int numCreatures, Creatures profile[])
{
	int index = 0;
	do
	{
		if (profile[x] == numCreatures)
		{
			for (int i = index; i < numCreatures; i++)
				profile[x] = profile[x + 1];
			numCreatures--;
			wasMoved = true;
			break;

		}
		else
			index++;

	} while (index < numCreatures);
Your deleteCreature shows the list of current names, asks the question for which name to remove then calls the moveArrayElements function to do the moving. The example I gave you would be in the moveArrayElements

so something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

//
//	$function:	deleteCreature
//
int deleteCreature(Creatures data[], int numCreatures)
{
	string name;
	int creatures;

	if (numCreatures == 0)
		return 0;

	creatures = numCreatures;

	cout << endl << endl << "The following is a list of all the creatures you take care of:" << endl;
	for (int i = 0; i < numCreatures; i++)
		cout << data[i].name << endl;

	cout << endl << "What creature would you like to remove? ";
	getline(cin, name); cin.sync();
	// remove it.
	if (moveArrayElements(data, numCreatures, name)) {
		cout << endl << "You have removed " << name << endl;
		creatures--;	// we are one less as we removed 1
	}

	return creatures;	// update with new creatures count

}
So this when used properly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
int deleteCreature(int numCreatures, Creatures profile[])
{
	string name;
	int creatures;
	creatures = numCreatures;

	if (numCreatures == 0)
	{
		return 0;
	}

	cout << endl << endl << "The following is a list of all the creatures you take care of:" << endl;
	for (int i = 0; i < numCreatures; i++)
	{
		cout << profile[i].name << endl;
	}

	cout << endl << "What creature would you like to remove? ";
	getline(cin, name); cin.sync();
	
	if (moveArrayElements(numCreatures, name, profile)) {
		cout << endl << "You have removed " << name << endl;
		creatures--;
	}
	return creatures;
}

 bool moveArrayElements(int numCreatures, name, Creatures profile[]) //error to do with name here.
{
	int index = 0;
	int creatureMaxCount = numCreatures;
	int i = 0;

	bool wasMoved = false;
	do
	{
		if (profile[i] == name)// some error to do with this is making cmd angry
		{
			for (i = index; i < creatureMaxCount; i++)
				profile[i] = profile[i + 1];
			creatureMaxCount--;
			wasMoved = true;
			
			return wasMoved;
			break;
		}
		else
			index++;

	} while (index < creatureMaxCount);
}
Last edited on

bool moveArrayElements(int numCreatures, name, Creatures profile[])

You havent declared the type for name, ie. string name
i tried that fix

EDIT: correction it moves the error to here
1
2
3
4
5
6
7
8
9
10
if (profile[i] == name)// some error to do with this is making cmd angry
		{
			for (i = index; i < creatureMaxCount; i++)
				profile[i] = profile[i + 1];
			creatureMaxCount--;
			wasMoved = true;
			
			return wasMoved;
			break;
		
}
Last edited on
You dont need the wasMoved variable, just return true; would do.

You need to also have a return false; at the end of the array function for when the name wasn't found.

Also the if statement on line 37 needs to test against the structure name member.
So
1
2
3
4
5
6
7
 }
		else
			index++;
		return false;// here? or make a if(!file)

	} while (index < creatureMaxCount);
}

Like this?

if (profile[i] == profile[i].name)

Something like this (haven't tested as posting from mobile)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

bool moveArrayElements(int numCreatures, string name, Creatures profile[]) 
{
	int index = 0;
	int creatureMaxCount = numCreatures;

	do
	{
		if (profile[i].name == name)
		{
			for (int i = index; i < creatureMaxCount; i++)
				profile[i] = profile[i + 1];
			creatureMaxCount++;
			return true;
		}
		else
			index++;

	} while (index < creatureMaxCount);

	return false;	// if we get here nothing was found.
}
Last edited on


if (profile[i].name == name) //only error is left here the i in this part isn't declared.

i wrote half this code on my tablet because my laptop died and i had to search all over for a hdd and a copy of windows, and could not spare the time to stop because its due thursday.
Last edited on
i should be index, sorry mobiles are not the best, and I'm currently in a bar having a few beers ;-)



Haha, and you still have time to help me +1 for humanity. i gota finish this program before i can go have fun :]

I'm now getting these errors


1
2
3
4
5
6
7
8
C:\CSC 2100\Program Four>g++ Prog4.cpp -o test
Prog4.cpp: In function 'void printCreatures(int, Creatures*)':
Prog4.cpp:310:32: error: expected primary-expression before '<<' token
Prog4.cpp:312:25: error: expected primary-expression before '<<' token
Prog4.cpp:314:23: error: expected primary-expression before '<<' token
Prog4.cpp:316:25: error: expected primary-expression before '<<' token
Prog4.cpp:318:26: error: expected primary-expression before '<<' token
Prog4.cpp:330:4: error: expected primary-expression before '<<' token


C:\CSC 2100\Program Four>


For these lines of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
			cout << "CREATURE " << (count + 1) << ": ";

			cout << "Name: " << setw(22) << profile[count].name << endl;

			cout << "\nDescription:\n"; << profile[count].description << endl;

			cout << "\nLength:"; << setw(22) << profile[count].length << endl;

			cout << "Height:"; << setw(22) << profile[count].height << endl;

			cout << "Location:"; << setw(22) << profile[count].location << endl;

			cout << "Dangerous?"; << setw(22) << profile[count].dangerous << endl;

			cout << "Number of Hours to Care for Creatures:"
				<< setw(35) << profile[count].management.hours << endl;

			cout << "Cost Per Hour of Taking Care of Creatures:"
				<< setw(35) << profile[count].management.cost << endl;

			cout << "Cost to Feed Creature:"
				<< setw(35) << profile[count].management.costFood << endl;

			cout << "Grooming & Supplies Cost:";
			<< setw(35) << profile[count].management.costSupplies;



This is in the printCreatures function, is ment to display all the data in the array.
Last edited on

That's dedication for you ;-) - when I start making no sense you know ive had too many :)

Anyway....

You have a ; where it shouldnt be in several lines of your code, i.e. Name is ok but description looks like this

cout << "\nDescription:\n"; << profile[count].description << endl;

and it should be

cout << "\nDescription:\n" << profile[count].description << endl;

basically when it see's the ; it thinks its the end of an instruction.


Remember the finer details for your assignment, i.e. the structure should be in prog4.h header file and not in your cpp file... just minor things but still contribute to marks.

:)
Well i have some more issues and ill try to compile them all, then post them if i cant figure them out myself. Enjoy your night good sir, if your head doesn't hurt too bad in the morning id appreciate acquiring your assistance yet again.

meh i don't suffer from hangovers, I'm a big boy now ;-)

Before you head off to bed tonight post you program again, either here or by private message.. don't mind either way and I'll take a look in the morning. remember the time difference though I'm in England

Good luck with the coding, i can see already you have come a long way.


Having said that there's a good bunch of people on here who can help in the meantime. good luck :)
Your 6 hours ahead of me right? Ill get it posted before you wake up.

No bother, good luck.
Pm'ed you
Pages: 1... 6789