Help with code.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 37
Reputation: annagraphicart is an unknown quantity at this point 
Solved Threads: 0
annagraphicart annagraphicart is offline Offline
Light Poster

Help with code.

 
0
  #1
Sep 18th, 2007
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.

  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
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,653
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with code.

 
0
  #2
Sep 18th, 2007
This is the sort of program where a structure or class comes in handy -- makes sorting the data a whole lot simpler.
  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
  1. for(int i = 0; i < 3; i++)
  2. cin score[i];
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 37
Reputation: annagraphicart is an unknown quantity at this point 
Solved Threads: 0
annagraphicart annagraphicart is offline Offline
Light Poster

Re: Help with code.

 
0
  #3
Sep 18th, 2007
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?
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,653
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with code.

 
0
  #4
Sep 18th, 2007
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.
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 824 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC