CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2003
    Posts
    11

    please help me, program due tomorrow

    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.

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    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

  3. #3
    Join Date
    Dec 2003
    Posts
    11
    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?

  4. #4
    Join Date
    Dec 2003
    Posts
    11
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured