Explain Arrays Please

Pages: 1... 56789
if(isupper(some_int) some_counter++

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

#include <iostream>
#include <ctype.h>
using namespace std;

int main()
{
	int count = 0;
	char myStr[50] = "This is a test to see how many uppercase we HAVE!";	

	for (int i = 0; i < 50; i++)	// 50 being length of array
	{
		if (isupper(myStr[i])) {
			count++;
			cout << "Found: " << myStr[i] << endl;
		}
	}
	cout << endl << endl << "Number of uppercase was " << count;
	return 0;
}
My code, getting wrong output though. I know i did the arrays wrong i believe(did this very quickly had to meet someone tonight) and for some reason i get a really large number for my number of digits.


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
#include <iostream>
#include <cctype>
#include <fstream>

using namespace std;

const char FileName[] = "text.txt";

int main()
{
	string line;
	ifstream inMyStream(FileName);
	int c = 0;
	int upperCaseCount[26] = { 0 };
	int lowerCaseCount[26] = { 0 };
	char oneLetter;

	if (inMyStream.is_open())
		{
			while (getline(inMyStream, line))
			{
				c += line.length();
			}
		
			for (unsigned n = 0; n < line.length(); ++n)
			{
				oneLetter = char(line[n]);
				if (oneLetter >= 'A' && oneLetter <= 'Z') 
				{
					upperCaseCount[int(oneLetter) - 'A']++;
				}
				else if (oneLetter >= 'a' && oneLetter <= 'z')
				{
					lowerCaseCount[int(oneLetter) - 'a']++;
				}
			}
		}
		inMyStream.close();

		cout << "Uppercase Characters: " << upperCaseCount << endl;
		cout << "Lowercase Characters: " << lowerCaseCount <<endl;
		cout << "Digits: " << c << endl;

	return 0;
}
Fixed it changed array to just a normal integer.
On Last program of the class now, im trying to be super organized about writing this one so i can get it done faster and correctly, but yet again the whole array issue still confuses me, especially since they are getting kind of complicated at this point. Currently I'm reading from a file with a bunch of lines of data in it and have to set that equal to an array. However i think that all that data can be stored in the array by just the name of that particular set of data. And i have absolutely no idea how to do this.
Last edited on
Post your assignment details then I may be able to point you in the right direction.
I think i figured that part out, ive actually almost got the program done. My biggest issue is figuring out what im supposed to do with this one function the teacher provided. Heres what ive got so far and the instructions.
https://www.dropbox.com/s/dam8j4r5mr0htiv/Program%20Four.zip
If your referring to convertToFloat(), you need that to convert the string read from the file to a floating point number - everything in the data file is ASCII text regardless if it was a string array, float or whatever so it needs to be converted back to numeric before you can perform calculations on it.
Eh?
wtf 7 page thread ?
We have been talking a whole semester.
oh my
wtf 7 page thread ?

its about 3 different questions (or more) under one thread lol.

Eh?

I'm assuming by that comment you didn't know what I meant :)

Quote from the PDF:
Some of the Creature members are strings, so that won’t be a problem. However, some of the Creature members are floats. So, this function will have to call the convertToFloat() function (provided later) in order to convert the string to a float and then placed in the Creatures array.


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

#include <iostream>
#include <sstream>	// for convertToFloat function
using namespace std;

float convertToFloat(string s);

int main()
{
	// assume the myStringFloat value has been read
	// from the datafile and its part of your struct
	char myStringFloat[] = "10.56";

	float myRetVal;		// to hold the result of the function call

	// befor this call we are ascii text in a char array, we call
	// the function to convert it to numeric
	myRetVal = convertToFloat(myStringFloat);

	// test it
	cout << "Returned value was " << myRetVal;

	return 0;
}

float convertToFloat(string s)
{
	istringstream i(s);
	float x;
	if (!(i >> x))
		x = 0;
	return x;
}
Im still confused as to where im supposed to use this. I think im starting to understand what its used for though.

Yuck this program has turned into a mess, im trying to finish the two functions i haven't done yet, but i dont really understand what one is supposed to do beyond, i think it moves each part of the array down one so i can make more room, but it does it by creature name... i think.
Last edited on
Better Question, how do you remove and element form an array then move the other elements back one position to fill in the empty space.
Im thinking along these lines.

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
void deleteCreature(int numCreatures, Creatures profile[])
{
	
	for (int x = 0; x < numCreatures; x++)
	{
		cout << "The following is a list of all the creatures you take care of:"
			<< profile[x].name << endl << endl << endl;
	
		cout << "What creature do you wish to remove?" << endl
			<< "CREATURE NAME: ";
		cin.ignore();
		getline(cin, profile[numCreatures].name);

		std::vector<int> profile;
		
		auto it = std::find(array.begin(), array.end(), profile[x].name);
		if (it != array.end())
		{
			array.erase(it);
		}
		else 
		{
			std::cerr << "Could not find profile!\n";
		}
		
		cout << "You have removed " << profile[x].name << "." << endl << endl;*/
	}


}}
EDITED
Last edited on

Well, now that my fibre internet is back online I can answer your first question... (it died on me in the middle of typing grrr)

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

//
//	$function saveCreaturesToFile
//
void saveCreaturesToFile(Creatures data[], int numCreatures)
{
	fstream dataFile;
	char filename[100];

	cout << "Enter the filename for your creatures data (ex: filename.txt): ";
	cin >> filename; cin.sync();

	// attempt to open the file.
	dataFile.open(filename, ios::out);
	// have we failed?
	if (dataFile.fail())
		cout << endl << endl << "Failed to create " << filename << ", unable to save creatures." << endl << endl;
	else
	{
		// file is open, write out our creatures.
		for (int i = 0; i < numCreatures; i++)
		{
			dataFile << data[i].name << "#";
			dataFile << data[i].description << "#";
			dataFile << data[i].averageLength << "#";
			dataFile << data[i].averageHeight << "#";
			dataFile << data[i].location << "#";
			dataFile << data[i].dangerous << "#";
			dataFile << data[i].creatureCosts.careHours << "#";
			dataFile << data[i].creatureCosts.careCost << "#";
			dataFile << data[i].creatureCosts.foodCost << "#";
			dataFile << data[i].creatureCosts.suppliesCost << "#";
		}
		dataFile.close();	// im done, close the file.
		cout << endl << "Your creatures were successfully saved to the " << filename << " file." << endl << endl;
	}

}


The above example is based on the below structure layout.

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

//
//	Structure Cost
//
struct Cost
{
	float careHours;
	float careCost;
	float foodCost;
	float suppliesCost;
	// constructor to set default values.
	Cost() { careHours = 0; careCost = 0; foodCost = 0; suppliesCost = 0; }
};


//
//	Structure Creatures
//
struct Creatures
{
	char name[100];
	char description[100];
	float averageLength;
	float averageHeight;
	char location[100];
	bool dangerous;
	Cost creatureCosts;
	// constructor to set default values.
	Creatures() { name[0] = 0; description[0] = 0; averageLength = 0; averageHeight = 0; location[0] = 0; dangerous = false; }
};


For the recent edited question...

if you have a array of letters (keeping things simple) like:

char vowels[5] = {'a', 'e', 'i', 'o', 'u' };

vowels[0] = a
vowels[1] = e
vowels[2] = i
vowels[3] = o
vowels[4] = u

If I was to delete 'i' from the array then i would be deleting the contents of index 2, so what you then need to do is move contents of index 3 down to index position 2, index position 4 down to index position 3.

if going by your assignment, same thing except your moving the structure array and updating a counter that holds the number of creatures.
Last edited on

You will need to create an array of 100 elements of the Creatures data type


As an extension of my last reply, the assignment states arrays not vectors, vectors it would have been much simpler but that's not what its asked for.
Im utterly stuck on the deleteCreatures function and the movevArrayElements, they work in tandom and i cant figure them out at all, i read about this whole vector thing so i thought id give it a try and its not going to well.

https://www.dropbox.com/s/1bbc4oicw74i6af/Prog4.cpp current code.
Last edited on

The problem is they have not specified vectors and just arrays so I would avoid using them, especially if you haven't covered them in your lessons yet.
We haven't but everyone i ask says that im should do it with vectors and that using arrays is impossible, I know its possible because the guy I've been working with got it to work right after i left his house last night. Can you show me how it should be done. Everything i do just makes command prompt angry and vomits out billions of lines of failure.
Pages: 1... 56789