C++ function/ search and if not found add to array

C++ function/ search and if not found add to array

Page 1 of 1

0 Replies - 754 Views - Last Post: 10 April 2008 - 01:11 PM Rate Topic: -----

#1 Zelore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-April 08

C++ function/ search and if not found add to array

Posted 10 April 2008 - 01:11 PM

			
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void treeList (string, int &, string  [], int  []);
void sortVisits (string [], string [], int [], int);
void sortTrees (string [], int [], int);

int main()
{
	string		trees[400];
	int			treeCount[400];
	string		studentsName[40];
	string		studentsLast[40];
	int			treesVisited[40] = {0};
	int			rating = 0;
	string		favTree[40];
	int			timesVisited[100] = {0};
	int			amtOfNames = 0;
	int			locationOfTrees = 0;
	ifstream	infile;
	int			nameRow = 0;
	int			counter = 0;
	double		avgRating = 0;
	string		tempTree;
	string		answerFromUser;
	string		fileOpener;
	int			outputCounter = 0;

	for (int x=0; x<400;x++){
	treeCount[x]=0;
	}
	answerFromUser = "Yes";
	while(answerFromUser=="Yes")
	{
		cout << "Please input the name of the file you would like to open. ";
		cin >> fileOpener;
		infile.open(fileOpener.c_str());
		while(! infile.eof())
		{	counter = 0;

			infile >> studentsName[nameRow] >> studentsLast[nameRow] 
			>> treesVisited[nameRow];

			while(counter < treesVisited[nameRow])
			{
				infile >> tempTree;
				treeList (tempTree, locationOfTrees, trees, treeCount);
				counter++;
			}

			infile >> rating >> favTree[nameRow];
			avgRating = (rating) + avgRating;
			nameRow++;
		}
		counter = 0;
		cout << locationOfTrees;
		while(counter < locationOfTrees)
		{
			cout << trees[counter] << endl;
			counter++;
		}

		avgRating = avgRating / nameRow;
		DONE!!sortVisits (studentsName, studentsLast, treesVisited, nameRow);
		sortTrees  (trees, treeCount, locationOfTrees);
		calculate favorite tree


		cout << endl << setw(20) << left << "Students Names" << setw(20) 
			 << right << "Trees Seen" << endl << endl;
		while(outputCounter < nameRow)
		{	
			cout << setw(20) << left << studentsName[outputCounter] + " " + studentsLast[outputCounter] 
				 << setw(20) << right << treesVisited[outputCounter] << endl;
			outputCounter++;
		}

		cout << endl << setw(25) << left << "Common Tree Name" << setw(25) << right << "Reported" << endl << endl;
		while(outputCounter < locationOfTrees)
		{
			cout << setw(25) << left << trees[outputCounter] << setw(25) << right 
				 << timesVisited[outputCounter] << endl;
			outputCounter++;
		}

		output sorts
		output favorite tree
		output average
		ask for other average
		cout << "Would you like to enter a different file?? (Yes/No) ";
		cin >> answerFromUser;
		cout << avgRating;
	}
		return 0;
}
void treeList (string tree, int & locationOfTrees, string  trees[], int  treeCount[])
{	bool foundTree = false;
	int  countingLoop;
	cout << locationOfTrees;
	for (countingLoop = 0; countingLoop < locationOfTrees; countingLoop++)
	{

		if (tree==trees[countingLoop])
		{
			treeCount[countingLoop]++;
			foundTree=true;			
		}
		if (foundTree)
		{
			trees[locationOfTrees] = tree;
			treeCount[locationOfTrees]++;
			locationOfTrees++;
		}
		
	}
}

void sortVisits (string fname [], string lname [], int numTrees [], int numStudents ) 
{
	int		pos;
	int		selection;
	int		numTreesTemp;
	int		big;
	string	fnameTemp; 
	string	lnameTemp;
	string	favTreeTemp;
	for (selection = 0; selection < numStudents-1; selection++)
	{
		big = selection;
		
		for (pos = 0; pos < numStudents; pos++)
		{
			if (numTrees[pos] < numTrees [big])
			{
			big = pos;
			}
		numTreesTemp = numTrees[big];
		numTrees[big]=numTrees[selection];
		numTrees[selection]=numTreesTemp;
		lnameTemp = lname[big];
		lname[big] = lname[selection];
		lname[selection]=lnameTemp;
		fnameTemp = fname[big];
		fname[big] = fname[selection];
		fname[selection] = fnameTemp;
		}
	}
}

void sortTrees (string treeName [], int numVisited [], int treeCount )
{
	int		pos;
	int		selection;
	int		numTreesTemp;
	int		big;
	string	favTreeTemp;
	for (selection =0; selection < treeCount-1; selection++)
	{
		big = selection;
		
		for (pos=0; pos < treeCount; pos++)
		{
			if (numVisited [pos] > numVisited [big])
			{
			big = pos;
			}
		}
		numTreesTemp = numVisited[big];
		numVisited[big]=numVisited[selection];
		numVisited[selection]=numTreesTemp;
		favTreeTemp = treeName[big];
		treeName[big] = treeName[selection];
		treeName[selection] = favTreeTemp;

	}
}




I am trying to read in a file that has a setup of
"name" 7 7 trees a number and a students pick of a tree.
"name" 8 8 trees a number and a students pick of a tree.

what i need this program to do (and the trouble im having is) i need it to search the array of trees to see if the next tree is in there and if not add it. my trees will read into the temp tree (which brings it into the function) and then i dont know why it isnt adding it to the list. if its in the list then its sopposed to not add it to the list and update the times the tree was seen.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1