Well, first of all, I'm new here, so it's nice to be here and I look forward to many posts to come. I just started programming a year ago and I love it.
Now, I have some code, a program that I wrote before and works as is, but I need some help making some adjustments to it. This program has two arrays for 10 "Players" and 10 "Scores". The third chunk of code calculates the highest score and prints it out, well it did. I took that out because I the revision I need is to print out the Scores from highest to lowest. Then, I need to print each Player alphabetically. I'm new to c++ and have a fairly good understanding of it, but I am lost as on how to do this.
Any help would be greatly appreciated and will give rep. for any help, if you guys do that here. Thanks.
#include "stdafx.h"
#include <iostream>
#include <istream>
#include <string>
usingnamespace std;
// Declaring my Arrays
string player_names[10];
int player_scores[10];
int main()
//This section gets the first 10 players names.
{
for (int x=0; x<=9; x++) {
cout << "\nEnter name of Player " << x+1 << ":\n" ;
cin >> player_names[x+1];
}
//This section gets the 10 players scores.
for (int x=0; x<=9; x++) {
cout << "\nEnter score of Player " << x << ":\n" ;
cin >> player_scores[x];
}
//This is declaring more variables.
int max = player_scores[0];
int max_index = 0;
//This section is to calculate who had the highest score.
for (int x=0; x<=9; x++) {
if (player_scores[x] > max) {
max = player_scores[x];
max_index = x;
}
}//This used to contain output of highest score, I need to print all scores
//from highest to lowest
//Then I need to print all players alphabetically.
system ("pause");
return 0;
}
Thanks a lot for any help, eagerly awaiting suggestions.
You have a problem in your first "for" loop where you get the first 10 player's names. When x is equal to 9, you will be writing out of bounds in your player_names array (position 10, x + 1).
Remember, array indices are zero-based, so the last index is one fewer than the dimention of your array: You can only use indices 0 through 9 in this case.
For printing the players alphabetically:
You can loop through your array and sort them into another array;
You can copy your array members to an STL list container - it has a sort function;
I know the whole x+1, but had trouble trying to implement it into my section of code where I evaluate the highest score. I am new to this and wouldn't mind a quick tutorial on how to do that, if you don't mind.
I think that you will be able to use it with just plain arrays:
1 2 3 4 5 6 7 8 9
#include <iostream>
#include <algorithm>
int main()
{
int numbers[10] = {4,3,5,6,7,1,9,2,0,8};
std::sort(numbers, numbers+9);
//Print them out in a for loop, etc
}
I can't check now, but that may work. If not, you'll have to use lists or vectors.
The only line in my example you should worry about is line 7, on line 6 I just wanted to write the code quickly so you would get the idea. The array can be changed at any time before or after you sort it.
Yea, I understood that you were using your own variable names, and I just applied this to my code. However, it doesn't seem to be working properly now. I must have messed something up trying to implement the "x+1" so that the array starts at 1.
Will post back when I resolve the issue. Thanks for all the help so far guys, I just love learning more and more about c++.
Alright, so it's running again. But, all it does is take the input of the players and scores, then it closes. So how do I output the sort function? This is how it looks now:
**EDIT**
Implemented the x+1 and everything seems to be alright. Now I still need to figure out how to out put the scores from highest to lowest and the players alphabetically.
int main()
//This section gets the first 10 players names.
{
for (int x=0; x<=9; x++) {
cout << "\nEnter name of Player " << x+1 << ":\n" ;
cin >> player_names[x+1];
}
//This section gets the 10 players scores.
for (int x=0; x<=9; x++) {
cout << "\nEnter score of Player " << x+1 << ":\n" ;
cin >> player_scores[x+1];
}
//This is declaring more variables.
int max = player_scores[0];
int max_index = 0;
//This section is to calculate who had the highest score.
for (int x=0; x<=9; x++) {
if (player_scores[x+1] > max) {
max = player_scores[x+1];
max_index = x+1;
}
}
cout << "\n The highest score is : " << player_scores[max_index] << endl; //Just to test that it //worked
{
int player_scores[10];
std::sort(player_scores, player_scores+9);
}
system ("pause");
return 0;
}
I looked into STL containers, but it seems a bit advanced. Not to mention, I don't see any function for sorting, but then again, I might be reading this wrong. I looked at this reference here:
This stuff can be so confusing sometimes, though I do enjoy the challenge.
**EDIT**
Reading up on sorting arrays now, may be able to solve this issue by myself. Will post back results and mark as solved if I do. Until then, any suggestions are welcome.
Thanks!