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?

Dani AI

Generated

Short version: the compiler error comes from trying to make int score[i] when i doesn't exist, and the rest of the program has several logic and bounds problems. and already spotted the undeclared i; suggested dynamic allocation but a safer, simpler modern approach is to keep names and scores together and use a container that manages size automatically.

Example workflow (safe and easy):

  • keep the three names in a container,
  • read one score per name in a loop (validate the input),
  • build a single vector of (score, name) pairs,
  • sort that vector by score (no manual index juggling),
  • print the sorted results.

A minimal C++11+ implementation follows (uses automatic memory management and the standard sort):

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main() {
    std::vector<std::string> names = {"John","Anne","Mary"};
    std::vector<int> scores;
    scores.reserve(names.size());
    for (const auto &n : names) {
        std::cout << n << ": ";
        int s;
        if (!(std::cin >> s)) return 1;
        scores.push_back(s);
    }
    std::vector<std::pair<int,std::string>> items;
    for (size_t i = 0; i < names.size(); ++i) items.emplace_back(scores[i], names[i]);
    std::sort(items.begin(), items.end(), [](const auto &a, const auto &b){ return a.first > b.first; });
    for (auto &p : items) std::cout << p.second << " : " << p.first << '\n';
}

Troubleshooting notes:

  • Watch for stray semicolons or misplaced braces that break loop scopes (the original nested-loop block is not actually inside the for loop).
  • Never index past size()-1 (the code uses index 3 for arrays of length 3 — that is out of bounds).
  • If you do use manual new[], pair it with delete[], but prefer std::vector or std::array for safety and clarity.

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.