0

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?

4
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
0

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

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

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...

0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.