|
 |
|
 |
 |
|
10-14-2004, 01:29 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 9
|
Help on starting new program
Hi everyone. I'm back!!
As it is I'm playing catchup in my C++ class, I'm about a chapter and a half behind the rest of the class. I'm falling behind due to personal issues (i.e. depression), but am in counseling for it so hopefully I'll have a breakthrough
This next assignment I'm supposed to do for my class has got me completely famboozled. The program is supposed to ask the user for the names of three racers, then type in their finishing times, and then the program is supposed to diplay which racer got first, second and third.
This assignment is out of chapter 4 in my book which is covering if and if/else if statments. In my head, I'm thinkin the "simplest" way to write this program would be to write some code that would simply evaulate the finishing time for each racer, assign the smallest time to a firstplace variable, next largest time to secondplace variable and the last largest time to thirdplace variable. Then all I'd need to do is display the results.
Now I might be completely off in how I'm thinking to write this program. I need some input/opinion on how to go about this. If I'm wrong, then do I simply write a series of IF / ELSE IF statements to evaluate the finishing times and then assign them to the appropiate variable (i.e. firstplace)? And if that is how its supposed to be done, how do I associate the appropiate runner's name with the correct finishing time that was entered by the user.
Well my brain is fried. Thanks in advance. As it is, if someone could point me in the right direction instead of giving me a full blown answer, I'd appreciate that. I find I learn better when searching out the info and reading and doing it myself. Not to sound conceited or anything. And yes, I did try searching this forum first.
Thanks again.
Matt Stacey
__________________
|
|
|
10-14-2004, 02:43 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
I'd read it into some sort of class ie:
Code:
class finish_time {
int hours;
int minuts;
int seconds;
}
class racer{
string name;
finish_time time;
}
Have an array, or stack of racer's.
Then read in the name of the racer, store it int the name member of the class racer, read in the finsh time, store it in the time assigned to racer, then sort the array with time, as the sort key, and show the sorted ones.
|
|
|
10-14-2004, 03:49 AM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
What did the class cover, up to now bootleg?
__________________
|
|
|
10-14-2004, 07:31 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 9
|
The class has covered up to loops like while, for and do loops. Actually that was the first part of the next chapter, they are already in the second part as of this week. I don't know what that covers exactly, I'd need to look at a syllabus which I don't have with me right now.
__________________
|
|
|
10-14-2004, 07:36 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
I see. I'll see if I can make it tomorrow. I'll start tomorrow most likely with your code if someone doesn't beat me to it.
__________________
|
|
|
10-14-2004, 07:52 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 9
|
Thanks for the reply. I'm actually trying to figure out how I'm going to write this program right now. I'm gonna be up for a while. What I don't understand is how I'm supposed to use only If Else-If statements to do it. I can see how I'd use them to evaluate the run times to see who got first, second and third place. But how do I display the runner's names with the correct finish times. It would be something like
Code:
cout << "First place winner is" <<name1 "with a finish time of" <<runtime1 "!" <<endl;
or it could be something like
Code:
cout <<name1 "has placed" <<firstplace "with a finishing time of" <<runtime1 <<endl;
I've been reading ahead in my book about loops, and arrays are brought up in chapter six. And it looks like to me using arrays and loops to write this program would be easier but all I have is the IF Else IF statments from chapter four. I don't think I'm ever going to get caught up. Anyhow, thanks again for your help.
__________________
|
|
|
10-14-2004, 08:07 PM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Oh in that case, just program something straightforward and ugly like you are doing right not. We don't have much of a choice do we?
__________________
|
|
|
10-14-2004, 08:20 PM
|
#8 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 9
|
I apologize if I offended you.
__________________
|
|
|
10-14-2004, 08:39 PM
|
#9 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Offending me is impossible. You mis-interpreted my post.
The code will be straight forward and ugly. However, if I am restricted to your current tools, I couldn't do better myself. So my advise is to do it straight forward.
Make a bunch of variables:
3 names (use getline() to combine first name and sur name during input of names).
9 time variables (unsigned, 3 per contestant).
3 ranking variables (1 rank per contestant).
Then fiddle with it:
Code:
TotalTimeFirstPlayer = FirstPlayerHours *3600 + FirstPlayerMinutes * 60 + FirstPlayerSeconds;
if(TotalTimeFirstPlayer < TotalTimeSecondPlayer && TotalTimeFirstPlayer < TotalTimeThirdPlayer)
cout<<FirstPlayer<<" wins!<<endl;
... etcetera...
What else could you (or I) do?
You *could* fiddle with enum as well:
Code:
enum ranks {FIRST, SECOND, THIRD};
But don't expect anything from it right now.
__________________
|
|
|
10-14-2004, 11:20 PM
|
#10 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Only problem that way would be, how do you combine the entered name with the current time ?
With the tools you've got at hand it would have to be alot of butt ugly if() statements.
|
|
|
10-14-2004, 11:49 PM
|
#11 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 9
|
Valmont, and Redhead, your both right. And I did misunderstand your post Valmont. Glad to know I didn't piss you off
I didn't mean to drop this whole thing on you guy's. Its cool you take out of your spare time to post on these boards. I'm going to try and make it into class tomorrow (today actually, its 2am here) and ask my teacher about it. He'll be able to shed some light on the whole thing.
Thanks again.
Matt
__________________
|
|
|
10-15-2004, 12:06 PM
|
#12 (permalink)
|
|
Registered User
Join Date: Oct 2004
Posts: 9
|
Hey, just so you kmnow, no I didn't make it to class. I'm still gonna have to ask my teacher about this stupid program. In the meantime I'm just skipping it for the time being. Once I get caught up on loops where the rest of the class is at I'll come back to it. If anyone has already started on it then by all means do post the code. It would be nice to have an example of how something like this works from someone who knows what they are doing. Thanks again.
Matt
__________________
|
|
|
10-16-2004, 03:25 AM
|
#13 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Quote:
Originally posted by redhead
Only problem that way would be, how do you combine the entered name with the current time ?
With the tools you've got at hand it would have to be alot of butt ugly if() statements.
|
Hence my remark.
__________________
|
|
|
10-16-2004, 06:53 AM
|
#14 (permalink)
|
|
Registered User
Join Date: Oct 2004
Location: Dank cellar
Posts: 18
|
I am also a student and this is what I came up with.
Code:
/*
Robert Stout
stoutrobert@gmail.com
October 16, 2004
*/
#include <iostream>
#include <string>
using namespace std;
#define RACERS 3
struct Racer
{
string sName;
int iMinutes;
};
int Compare( const void *p1, const void *p2 )
{
Racer *r1 = (Racer *)p1;
Racer *r2 = (Racer *)p2;
return( r1->iMinutes - r2->iMinutes );
}
void main()
{
Racer racers[RACERS];
cout << "Race Evaluator" <<endl <<endl;
/////////////////////////////
// Input name and finish time
for( int i = 0; i != 3; i++ )
{
cout << "Enter the name of racer #" << i << ": ";
cin >> racers[i].sName;
cout << "Enter the finish time in minutes of racer #" << i << ": ";
cin >> racers[i].iMinutes;
}
/////////////////////////////
// Sort
// The first argument: pointer to the first element of the array.
// The second argument: number of items in the array.
// The third argument: size (in bytes) of one element of the array.
// The fourth argument: pointer to a compare function.
qsort( racers, RACERS, sizeof(Racer), Compare );
/////////////////////////////
// Display
cout << endl << "Placement Order" << endl;
cout << "Racer 1: " << racers[0].sName << endl;
cout << "Racer 2: " << racers[1].sName << endl;
cout << "Racer 3: " << racers[2].sName << endl;
}
Quote:
Console Output
Race Evaluator
Enter the name of racer #0: Ted
Enter the finish time in minutes of racer #0: 120
Enter the name of racer #1: Mark
Enter the finish time in minutes of racer #1: 304
Enter the name of racer #2: Kevin
Enter the finish time in minutes of racer #2: 214
Placement Order
Racer 1: Ted
Racer 2: Kevin
Racer 3: Mark
|
I am not a master of C++ programming, so if anyone has suggestions as to how this code could be improved I welcome comments and suggestions.
__________________
|
|
|
10-16-2004, 07:13 AM
|
#15 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
perhaps a for(.. ;i < 3; ..) since if suddently someone decides to have I skip two, the != 3 will make it true even when it reaches 23.
And maybe a cin.getline(), since cin >> will make a false assignment, if the racername is entered with a space in it.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 02:15 AM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|