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?

Dani AI

Generated

— the input code belongs right after you create the container that holds each bowler's name and score and before the sort. 's point about using a single object for name+score is important: that prevents mismatches when you reorder entries. Below is a compact pattern you can drop into your program that keeps each name tied to its score, prompts the user for each score, validates input, then sorts by score.

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

std::vector<std::pair<std::string,int>> bowlers = { {"John",0}, {"Anne",0}, {"Mary",0} };

for (auto &p : bowlers) {
  std::cout << p.first << "'s score: ";
  while (!(std::cin >> p.second)) {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Please enter an integer for " << p.first << ": ";
  }
}

std::stable_sort(bowlers.begin(), bowlers.end(),
                 [](auto &a, auto &b){ return a.second < b.second; });

for (auto &p : bowlers)
  std::cout << "name:" << p.first << " score:" << p.second << '\n';

Notes and troubleshooting:

  • Put the input loop immediately after you set up bowlers (so the sort sees the user-entered scores).
  • Use stable_sort if you want ties to keep the original name order.
  • If you read names from the keyboard with getline and later use operator>>, call std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); to discard the leftover newline.
  • Prefer vector and pair/struct to parallel arrays; it avoids off-by-one/indexing confusion (use i or range-based loops, not hard-coded indices). For more on sort, see the C++ reference: std::sort.

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.