stuck on time!!!!

This is a discussion on stuck on time!!!! within the C++ Programming forums, part of the General Programming Boards category; Hi, Please help me out in solving this Q.I am newbie and I am not sure whether I am doing ...

  1. #1
    Unregistered
    Guest

    Question stuck on time!!!!

    Hi,
    Please help me out in solving this Q.I am newbie and I am not sure whether I am doing right thing??

    The Q is to take swim,cycle,run times from user and display the best from each leg of competition.

    thanks in advance.

    I am submiiting code for calculating the best swim time:
    Code:
    int main()
    {
    int best_swim_time=1000;
    do{
    if(swim_time<best_swim_time)//swim_time entered by                               user//
    {
    		
        be_swim_time= swim_time;
         best_swim_name= nam;
    	}
    }while(true);
    My test data:
    1st competitor:- ST:18sec,RT:3sec,CT:4sec
    2nd competitor:- ST:10sec,RT:8sec.CT:13sec
    results:best ST 2nd competitor (which is right)
    best CT,RT again 2nd competitor(which is wrong)

    1st competitor:- ST:2sec,RT:4sec,CT:9sec
    2nd competitor:- ST:9sec, RT:3sec,CT :2sec
    results:best RT & CT 2nd competitor(which is right)
    best ST again 2nd competitor(which is wrong)

  2. #2
    Unregistered
    Guest
    You need to think through your logic and then write it out in a flow chart or what ever it takes to make sense... if you can't do that then you won't be able to code it.

    Here is some quick and dirty code I banged out to help you get moving. You should be able to modify it to do what you want... I hope you will not copy it verbatum... but that it up to you.

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct competitor_info {
    	char name[30];
    	int swim_time;
    	int run_time;
    	int cycle_time;
    	int overall_time;
    };
    
    #define MAX_COMPETITORS 3
    
    int main()
    {
    	competitor_info race_results[MAX_COMPETITORS];
    	int best_time=1000;
    	int competitor=0;
    	int winner=0;
    
    	// initialize array values;
    	for (int i=0; i<MAX_COMPETITORS; ++i)
    	{
    		race_results[i].name[0] = '\0';
    		race_results[i].swim_time = 0;
    		race_results[i].run_time = 0;
    		race_results[i].cycle_time = 0;
    		race_results[i].overall_time = 0;
    	}		
    	
    
    	do {
    		cout << "enter name of competitor #" << competitor+1 << ": ";
    		cin >> race_results[competitor].name;
    
    		cout << "enter swim time: ";
    		cin >> race_results[competitor].swim_time;
    
    		cout << "enter run time: ";
    		cin >> race_results[competitor].run_time;
    
    		cout << "enter cycle time: ";
    		cin >> race_results[competitor].cycle_time;
    
    		race_results[competitor].overall_time = 
    			race_results[competitor].swim_time+
    			race_results[competitor].run_time+
    			race_results[competitor].cycle_time;
    
    		if(race_results[competitor].overall_time<best_time)
    		{
    			best_time = race_results[competitor].overall_time;
    			winner=competitor;
    		}
    
    		// check for bad input		
    
    		if (!race_results[competitor].swim_time || 
    			!race_results[competitor].swim_time || 
    			!race_results[competitor].swim_time)
    		{
    			cout << "you must compete in all events\n";
    			cout << "you have been disqualified\n";
    		}
    		else ++competitor;
    	
    	} while (competitor < MAX_COMPETITORS);
    
    	cout << "\n\n===============================================\n";
    	cout << "best overall time: " << best_time << endl;
    	cout << "winner: " << race_results[winner].name << endl;
    	cout << "swim time: " << race_results[winner].swim_time << endl;
    	cout << "run time: " << race_results[winner].run_time << endl;
    	cout << "cycle time: " << race_results[winner].cycle_time << endl;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 11:42 AM
  2. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 07:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21