| | |
Help with code.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 37
Reputation:
Solved Threads: 0
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.
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
code.
Can somebody help me out?
In the code below,
I have a Bowler's name, and their score.
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
cout << "Anna's Bowling Score: ";
Can somebody help me out?
Last edited by Ancient Dragon; Sep 18th, 2007 at 11:14 pm. Reason: add line numbers
This is the sort of program where a structure or class comes in handy -- makes sorting the data a whole lot simpler.
>>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)
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
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < 3; i++) 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.
•
•
Join Date: Sep 2007
Posts: 37
Reputation:
Solved Threads: 0
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?
or do I have to replace the [i] with [1]?
I'm sorry I dont quite understand.
Before or After I ask the user for the score?
Will it be like this?
C++ Syntax (Toggle Plain Text)
cout << "Anna Marie's Score: "; cin >> score[i];
or do I have to replace the [i] 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.
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < 3; i++) { cout << names[i] << " Score: "; cin >> score[i]; cin.ignore(); // ignore '\n' left in the keyboard buffer }
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.
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the C++ Forum
- Previous Thread: windows api help
- Next Thread: Skip initial 4 bytes in a binary stream
Views: 824 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






