943,935 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 954
  • C++ RSS
Sep 18th, 2007
0

Help with code.

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string names[3] = {"John","Anne","Mary"};
  9. int score[3] = {5,1,2};
  10.  
  11. //i.e
  12. //John's score = 5
  13. //Anne's score = 1
  14. //Mary's score = 2
  15.  
  16. //sort by score
  17. for ( int i = 0; i < 3; i++ )
  18. {
  19. for ( int j = 0; j < 3; j++ )
  20. {
  21. if( score[i] < score[j] )
  22. {
  23. string tmp_string;
  24. int temp;
  25.  
  26. temp = score[i];
  27. tmp_string = names[i]; //now swap the names array
  28.  
  29.  
  30. score[i] = score[j];
  31. names[i] = names[j]; //now swap the names array
  32.  
  33. score[j] = temp;
  34. names[j] = tmp_string; //now swap the names array
  35. }
  36. }
  37. }
  38.  
  39. //show them sorted
  40. cout << "Sorting by score in ascending order\n";
  41. for ( int k = 0; k < 3; k++ )
  42. {
  43. cout << "name:" << names[k] << " score:" << score[k] <<endl;
  44. }
  45. return 0;
  46. }

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
C++ Syntax (Toggle Plain Text)
  1. cout << "Anna's Bowling Score: ";
code.

Can somebody help me out?
Last edited by Ancient Dragon; Sep 18th, 2007 at 11:14 pm. Reason: add line numbers
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
annagraphicart is offline Offline
37 posts
since Sep 2007
Sep 18th, 2007
0

Re: Help with code.

This is the sort of program where a structure or class comes in handy -- makes sorting the data a whole lot simpler.
C++ Syntax (Toggle Plain Text)
  1. struct bowler
  2. {
  3. string name;
  4. int score;
  5. };

>>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
C++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i < 3; i++)
  2. cin score[i];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Sep 18th, 2007
0

Re: Help with code.

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?
C++ Syntax (Toggle Plain Text)
  1. cout << "Anna Marie's Score: ";
  2. cin >> score[i];

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

I'm sorry I dont quite understand.
Reputation Points: 10
Solved Threads: 0
Light Poster
annagraphicart is offline Offline
37 posts
since Sep 2007
Sep 18th, 2007
0

Re: Help with code.

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.
C++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i < 3; i++)
  2. {
  3. cout << names[i] << " Score: ";
  4. cin >> score[i];
  5. cin.ignore(); // ignore '\n' left in the keyboard buffer
  6. }
Last edited by Ancient Dragon; Sep 18th, 2007 at 11:16 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: windows api help
Next Thread in C++ Forum Timeline: Skip initial 4 bytes in a binary stream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC