Sorry about that.

Please help here.
Extremely important.

It says I need to declare the "i" in scores
But im not sure what to do.

#include <iostream> 
 #include <string>  
 
 using namespace std;
 
 int main()
 {
     //Declaring variables
     string names[3] = {"John","Anne","Mary"};   
     int score[3]; 
     int points[3];
     
     //Ask for scores. 
     cout << "Enter Scores" << endl << endl;   
     cout << "Anna Marie: "; 	
     cin >> score[3];
         
       
     
     //sort by score   
     for ( int i = 0; i < 3; i++ ) 	
     score[i];   
     {     
           for ( int j = 0; j < 3; j++ )     
               {       
                       if ( score[i] < score[j] )       
                       {          
                                  string tmp_string;          
                                  int temp;           
                                  temp = score[i];            
                                  tmp_string = names[i];
 //Points                                  
 for ( int i = 0; i < n; i++ ) 
 {
  if ( names[i].score == 0 )
    names[i].points = 0;
  else
    names[i].points = i + 1;
}
                                  
     //now swap the names array            
     score[i] = score[j];           
     names[i] = names[j]; 
     
     //now swap the names array           
     score[j] = temp;          
     names[j] = tmp_string; 
     
     //now swap the names array       
     }     
     }   
}
     
     //display   
         
     for ( int k = 0; k < 3; k++ )   
     {     cout << names[k] << " : " << score[k] << points[3] << endl;   
     }   return 0; 
}
Member Avatar for iamthwee

It would appear you are getting yourself confused.

Maybe someone told you to use a struct or class. But I can't see it being defined at the beginning. I personally think that using a class or struct might be too abstract for you to understand. I would carry on down the parallel array route.

What do you think?

> for ( int i = 0; i < 3; i++ )
> score;
See, that's the end of your for loop, and the end of the declaration of you i loop variable.

What were you trying to achieve by just writing score

It would appear you are getting yourself confused.

Maybe someone told you to use a struct or class. But I can't see it being defined at the beginning. I personally think that using a class or struct might be too abstract for you to understand. I would carry on down the parallel array route.

What do you think?

\

Ok.
Well, When I use the parallel array,
I'll declare

string names[3]={"Anna", "John", "Mari"};
int score[3];

I want the user to be able to insert the score themselves, and that is what is assigned to the names. Get it?

Then.. How would I do a sort in a parallel array?
I need it to be sorted by scores.

>>Then.. How would I do a sort in a parallel array?
You are headed in the right direction, just need to learn how to access each element of the array. Array elements are numberd from 0 to the size of the array, in your program the array score has three elements numbered 0, 1 and 2. So if you want someone to enter the three values from the keyboard then use a loop that counts from 0 to 3, like this:

for(int i = 0; i < 3; i++)
{
    // display the prompt
    cout << "Enter score for " << names[i] << "\n";
    // get one score from the keyboard
    cin >> score[i];
    // now flush the keyboard of the <Enter> key that the user entered after the number
    cin.ignore();
}

Delete lines 23 and 24 of your post because they are unnecessary and do nothing at all.

And your sort algorithm needs some improvement. The j loop should start at (i+1), not from 0 because it does not need to check the values from 0 to i -- they are already sorted.

for(int i = 0; i < 2; i++)
{ 
    for(j = i+1; j < 3; j++)
    {

         // check and swap if needed
    }
}

Finally, its always more efficient to declare a const int at the beginning of the program that contains the size of arrays then use that throughout the rest of the program. That way if you want to change the array size to something else you only have to change it in one place instead of in millions of places. There's an example

const int arraySize = 3; // number elements in all arrays

int names[arraySize] = { ... }
int scores[arraySize];

for(int i = 0; i < arraySize; i++)
{
   // blabla
}

ok. Its saying that the "n" is undeclared, and the "i".

Nothing is working... I dont know what to do.
I think I should start over.
What do you think I should do, for the progrm I'm trying to create?

>>I think I should start over
No, you should read the suggestions people have already given you and fix the problems. Then post your most recent code because I can't see your monitor from my chair :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.