Okay, so i've gotten some help, however i'm still missing some things.
In the code below,
I have a Bowler's name, and their score.

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string names[3] = {"John","Anne","Mary"};
  int score[3] = {5,1,2};

  //i.e
  //John's score = 5
  //Anne's score = 1
  //Mary's score = 2
  
  //sort by score
  for ( int i = 0; i < 3; 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]; //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
      }
    }
  }

  //show them sorted
  cout << "Sorting by score in ascending order\n";
  for ( int k = 0; k < 3; k++ )
  {
    cout << "name:" << names[k] << " score:" << score[k] <<endl;
  }
  return 0;
}

However, I want to change it where the "int score" is, so that the user can input the scores manually. I want the program to have a

cout << "Anna's Bowling Score: ";

code.

Can somebody help me out?

Recommended Answers

All 3 Replies

This is the sort of program where a structure or class comes in handy -- makes sorting the data a whole lot simpler.

struct bowler
{
    string name;
    int score;
};

>>However, I want to change it where the "int score" is, so that the user can input the scores manually
use cin inside a loop to do that

for(int i = 0; i < 3; i++)
   cin score[i];

Where do I put the second code you sent me?

Before or After I ask the user for the score?
Will it be like this?

cout << "Anna Marie's Score: ";
cin >> score[i];

or do I have to replace the with [1]?

I'm sorry I dont quite understand.

since you have the names in an array and the scores in another array you can use a loop to do that, and add the code at about line 15 in your original program.

for(int i = 0; i < 3; i++)
{
    cout << names[i] << " Score: ";
    cin >> score[i];
    cin.ignore(); // ignore '\n' left in the keyboard buffer
}
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.