Can ya'll take a look at this?

#include <iostream> 
 #include <string>  
 
 using namespace std;
 
 int main()
 {
     
     string names[3] = {"John","Anne","Mary"};   
     int score[i]; 
     
     cout << "Enter Scores" << endl << endl;   
     cout << "Anna Marie: "; 	
     cin >> score[i];
         
       
     
     //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[3];            
                                  tmp_string = names[i]; 
                                  
     //now swap the names array            
     score[3] = score[j];           
     names[i] = names[j]; 
     
     //now swap the names array           
     score[j] = temp;          
     names[j] = tmp_string; 
     
     //now swap the names array       
     }     
     }   
     }
     
     //Ask for Scores   
         
     for ( int k = 0; k < 3; k++ )   
     {     cout << names[k] << " : " << score[k] <<endl;   
     }   return 0; 
     }

It's saying the "i" in scores is undeclared?
What do I do?

Recommended Answers

All 4 Replies

how does the compiler know what the value of 'i' is here:

string names[3] = {"John","Anne","Mary"}; 
     int score[i];

Can ya'll take a look at this?

string names[3] = {"John","Anne","Mary"};   
     int score[i];

It's saying the "i" in scores is undeclared?
What do I do?

You have not declared your i before this line. An array needs its size set - you have done this correctly in your string array. You need to declare your i...

You have not declared your i before this line. An array needs its size set - you have done this correctly in your string array. You need to declare your i...

Well, I want whatever the user inputs, to be what the scores are.
I was told that that would work. AH! what should I do?

you will have to allocate the array using the new operator, something like this:

int* scores = 0; // an unallocated array
int nItems = 0; // number of items user will enter to set size of array

cout << "enter array size";
cin >> nItems;

// now allocate the array size
scores = new int[nItems];
//
//
// delete the array before finishing the function
delete[] scores;

what is lines 18-42 supposed to do in your original post? It certinly is not sorting anything, and is referencing elements in the array that do not exist, such as at line 28.

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.