Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 10-14-2004, 01:29 AM   #1 (permalink)
B00tleg
Registered User
 
B00tleg's Avatar
 
Join Date: Oct 2004
Posts: 9
B00tleg is on a distinguished road
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
__________________
B00tleg is offline   Reply With Quote
Old 10-14-2004, 02:43 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-14-2004, 03:49 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
What did the class cover, up to now bootleg?
__________________
Valmont is offline   Reply With Quote
Old 10-14-2004, 07:31 PM   #4 (permalink)
B00tleg
Registered User
 
B00tleg's Avatar
 
Join Date: Oct 2004
Posts: 9
B00tleg is on a distinguished road
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.
__________________
B00tleg is offline   Reply With Quote
Old 10-14-2004, 07:36 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 10-14-2004, 07:52 PM   #6 (permalink)
B00tleg
Registered User
 
B00tleg's Avatar
 
Join Date: Oct 2004
Posts: 9
B00tleg is on a distinguished road
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.
__________________
B00tleg is offline   Reply With Quote
Old 10-14-2004, 08:07 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
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?
__________________
Valmont is offline   Reply With Quote
Old 10-14-2004, 08:20 PM   #8 (permalink)
B00tleg
Registered User
 
B00tleg's Avatar
 
Join Date: Oct 2004
Posts: 9
B00tleg is on a distinguished road
I apologize if I offended you.
__________________
B00tleg is offline   Reply With Quote
Old 10-14-2004, 08:39 PM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 10-14-2004, 11:20 PM   #10 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-14-2004, 11:49 PM   #11 (permalink)
B00tleg
Registered User
 
B00tleg's Avatar
 
Join Date: Oct 2004
Posts: 9
B00tleg is on a distinguished road
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
__________________
B00tleg is offline   Reply With Quote
Old 10-15-2004, 12:06 PM   #12 (permalink)
B00tleg
Registered User
 
B00tleg's Avatar
 
Join Date: Oct 2004
Posts: 9
B00tleg is on a distinguished road
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
__________________
B00tleg is offline   Reply With Quote
Old 10-16-2004, 03:25 AM   #13 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 10-16-2004, 06:53 AM   #14 (permalink)
RobertStout
Registered User
 
RobertStout's Avatar
 
Join Date: Oct 2004
Location: Dank cellar
Posts: 18
RobertStout is on a distinguished road
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.
__________________
RobertStout is offline   Reply With Quote
Old 10-16-2004, 07:13 AM   #15 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 06:14 AM
Help on interest program B00tleg Standard C, C++ 2 10-07-2004 08:50 PM
Program Call to AS400 using JTOpen sde Java 0 05-12-2004 07:08 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM
Starting out with C anon C 0 02-24-2003 01:06 PM


All times are GMT -8. The time now is 02:15 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle