Click to See Complete Forum and Search --> : please help me, program due tomorrow


quick
December 11th, 2003, 11:54 AM
ok i have two problems.

the first one i am trying to calculate how many times something happens and increment an element in array each time that happens. this is for multiple objects that i have to increment an element for. my code looks like this:

while(!infile.eof())
{
location = search(starNames, target, numStars);

if(location != -1)
if(signal >= hvalues[location] || signal <= lvalues[location])
for(int i = 0; i < MAX; i++)
{
anomalies[location] = tot +1;

}
else
cout<<"Star doesn't exist." <<endl;

getline(infile, target);
infile >> signal;
infile.ignore(256,'\n');

}

the search function will produce the index of the target in the starNames array(location). it will check to see if the signal is in the correct range and if it is, will add one to the corresponding element in the anomalies array. however i can't get it to total up the anomalies and increment correctly.


another question i have is how to add all of the elements in an array together and output that answer. this seems really simple but i seem to be getting errors that i shouldn't be. this is my function i have so far:

int findtotal(string anomalies[], int& numStars)
{
int total = 0;
int tot = 0;
numStars = 5;
for(int i =0;i<numStars;i++){
anomalies[i] = tot + tot;
total= tot;
}
return total;
}

i am not sure if this is right because i get an error in the call from main:

int total;
total = findtotal(anomalies, numStars);

saying it can't convert a int to a string or something but the anomalies array is all ints.

please help.

kuphryn
December 11th, 2003, 12:05 PM
Why is anomalies defined as a string object?

One solution is an array of int. Another solution is an STL container such as a vector.

Kuphryn

quick
December 11th, 2003, 12:13 PM
oh wow i didn't even see that anomalies was a string array. thanks for catching that... i have fixed it and it seems to work now
thanks.

any insight of the first problem?

quick
December 11th, 2003, 12:38 PM
ok i seem to have gotten a lot closer to my desired output for the anomalies array:

while(!infile.eof())
{
location = search(starNames, target, numStars);

if(location != -1)
if(signal >= hvalues[location] || signal <= lvalues[location])
anomalies[location] = anomalies[location] +1;
else
anomalies[location] = 0;

else
cout<<"Star doesn't exist." <<endl;

getline(infile, target);
infile >> signal;
infile.ignore(256,'\n');

}

doing that gives me the correct number for 3 out of the 5 stars.
its giving one anomaly for two of the stars. im unsure why its doing that.