#include <iostream> 
#include <string> 

using namespace std; 

int main()
{
      string names[4] = {"Anna" , "Jenny", "George" , "Michael"};   
      int score[4];
      
      for(int i = 0; i < 3; i++) 
      { 
            cout << names[i] << ": ";     
            cin >> score[i];     
            cin.ignore(); 
            }
            
              //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];           
                                       score[i] = score[j];           
                                       names[i] = names[j];           
                                       score[j] = temp;          
                                       names[j] = tmp_string;
                            }    
                    }   
              } 
              
              for ( int k = 0; k < 3; k++ )   
              {
                      cout << endl;  
                      cout <<names[k] << " = "<< score[k] << "        ";
                     
                     
                      }
              system("pause");
}

I've compiled it (with dev-c++), and it runs well.
I just need a few minor things.

1. I need it to sort descendingly, not ascendingly.
2. I need to know how to either print the output, or get the output sent to a word processor so it can be printed.
3. When the output is displayed, is there a way to clear the screen before-hand, so the input is cleared?

--thanks a million, guys.

Dani AI

Generated

Good start — the program structure is fine and ’s hint about the comparator is the right direction. A couple of practical points that fill gaps in the thread and make the program cleaner and more portable.

First, check your loop bounds: you declared four names but your input/sort loops only run three iterations, so the fourth entry is never handled. Use a single count (or compute the array length) and reuse it for all loops so you don’t get off-by-one bugs.

To get printable output you have two easy choices:

  • Run the compiled program and redirect stdout to a text file, then open that file in Word:
    yourprogram.exe > scores.txt
  • Write the results directly from the program with <fstream>. Save as a .txt or .csv (comma- or tab-separated) so Word/Excel can open it cleanly. Example pattern:
    std::ofstream out("scores.txt");
    out << names[i] << '\t' << scores[i] << '\n';

    Avoid trying to create a real .doc file by hand — plain text or CSV is simplest and reliable for printing.

To clear the console before printing results you can:

  • Use the Windows shortcut (non-portable): system("cls");
  • Use ANSI escape sequences on terminals that support them: std::cout << "\x1B[2J\x1B[H";
    Note: system() calls are platform-specific and have security caveats; ANSI codes aren’t supported on older Windows consoles. If portability or security matters, write output to a file and open that instead of trying to manipulate the console.

Finally, consider pairing names and scores in one container and using std::sort with a comparator to sort descending — it keeps the name/score association robust and simplifies the logic. This also avoids manual swap bugs and makes the code easier to maintain.

Recommended Answers

All 3 Replies

1. I need it to sort descendingly, not ascendingly.

try rethinking this part of your code:

if( score[i] < score[j] )       
{          
...
}

Lol. Yeah... i feel pretty stupid. =p
I got it though

Do you think you can help with anything else?

Is the printing to a file/Word part of the assignment or just so you can print it out and turn it in?

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.