Re: How to determine ranks
Code:
char name1[SIZE], name2[SIZE], name3[SIZE], name4[SIZE], name5[SIZE];
double time1, time2, time3, time4, time5;
Turn these into an array and you can transform your code by using loops.
2 Attachment(s)
Re: How to determine ranks
Ok, but how do I Input the names of each of 5 runners and their respective times.
This is what I tried:
const int runners = 5;
char name1[21], name2[21], name3[21], name4[21], name5[21];
double time[runners];
int rank;
//Display the purpose oof this program
cout<<"This program will determine the ranks of 5 runners according to their time."<<endl;
cout<<endl;
//Request the names and times of each runner.
cout<<"1. Enter the name of the runner"<<endl;
cin>>name1[21];
cout<<endl; //For line spacing
cout<<"2. Enter the name of the next runner"<<endl;
cin>>name2[21];
cout<<endl; //For line spacing
cout<<"3. Enter the name of the next runner"<<endl;
cin>>name3[21];
cout<<endl; //For line spacing
cout<<"4. Enter the name of the next runner"<<endl;
cin>>name4[21];
cout<<endl; //For line spacing
cout<<"5. Enter the name of the next runner"<<endl;
cin>>name5[21];
cout<<endl; //For line spacing
I uploaded an example of what my output file is suppose to look like and the screenshot of the program after I type in the first name and hit enter.
Re: How to determine ranks
This is not correct.
Code:
cout<<"1. Enter the name of the runner"<<endl;
cin>>name1[21];
cout<<endl; //For line spacing
You probably mean:
Code:
cout<<"1. Enter the name of the runner"<<endl;
cin>>name1;
cout<<endl; //For line spacing
However, get rid of this char arrays and use std::string.
http://www.codeguru.com/cpp/cpp/stri...cle.php/c13267
Re: How to determine ranks
Thanks for the tips. I changed my entire source code and used arrays instead, but I still don't know how to determine ranks for 5 people according to their lap times??? I'm not very familiar of using loops for doing this. again your help is appreciated.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ofstream outputfile;
outputfile.open("c:\\Project_2\\M20349431_output_2.txt");
ifstream infile;
infile.open("c:\\Project_2\\M20349431_output_2.txt");
//Define variables
const char runners = 100;
char names[100][runners];
//Display the purpose oof this program
cout<<"This program will determine the ranks of 5 runners according to their time."<<endl;
cout<<endl;
//Request the names of each runner.
cout<<"1. Enter the names of all the runners"<<endl;
cin>>names[0];
cout<<endl;
cin>>names[1];
cout<<endl;
cin>>names[2];
cout<<endl;
cin>>names[3];
cout<<endl;
cin>>names[4];
cout<<endl;
//Display the names
cout<<"The names you entered are:"<<endl;
cout<<names[0]<<endl;
cout<<names[1]<<endl;
cout<<names[2]<<endl;
cout<<names[3]<<endl;
cout<<names[4]<<endl;
cout<<endl;
//Request Time
cout<<"2. Enter the times for all the runners"<<endl;
cout<<endl;
const int times = 5;
double time[times];
cout<<"Enter the time for "<<names[0]<<endl;
cin>>time[0];
cout<<endl;
while(time[0] <= 0)
cout<<"Error! Time Must Be Greater Than Zero. Restart Program"<<endl;
cout<<"Enter the time for "<<names[1]<<endl;
cin>>time[1];
cout<<endl;
cout<<"Enter the time for "<<names[2]<<endl;
cin>>time[2];
cout<<endl;
cout<<"Enter the time for "<<names[3]<<endl;
cin>>time[3];
cout<<endl;
cout<<"Enter the time for "<<names[4]<<endl;
cin>>time[4];
cout<<endl;
//Display names and times together
cout<<"You entered:"<<endl;
cout<<endl;
cout<<setprecision(2)<<fixed;
cout<<left<<setw(20)<<names[0]<<" -------- "<<right<<setw(6)<<time[0]<<endl;
cout<<left<<setw(20)<<names[1]<<" -------- "<<right<<setw(6)<<time[1]<<endl;
cout<<left<<setw(20)<<names[2]<<" -------- "<<right<<setw(6)<<time[2]<<endl;
cout<<left<<setw(20)<<names[3]<<" -------- "<<right<<setw(6)<<time[3]<<endl;
cout<<left<<setw(20)<<names[4]<<" -------- "<<right<<setw(6)<<time[4]<<endl;
cout<<endl;
cout<<"Open the output file to view the results and the ranks of the all the runners"<<endl;
system("pause"); //Pause the program so that the user can view the results
return 0;
Re: How to determine ranks
Quote:
I'm not very familiar of using loops for doing this. again your help is appreciated.
Code:
cout<<"The names you entered are:"<<endl;
cout<<names[0]<<endl;
cout<<names[1]<<endl;
cout<<names[2]<<endl;
cout<<names[3]<<endl;
cout<<names[4]<<endl;
cout<<endl;
Code:
long lCnt;
cout<<"The names you entered are:"<<endl;
for (lCnt = 0; lCnt < 5; lCnt++)
{
cout<<names[lCnt]<<endl;
}
cout<<endl;
Re: How to determine ranks
Quote:
Originally Posted by
Skizmo
Code:
cout<<"The names you entered are:"<<endl;
cout<<names[0]<<endl;
cout<<names[1]<<endl;
cout<<names[2]<<endl;
cout<<names[3]<<endl;
cout<<names[4]<<endl;
cout<<endl;
Code:
long lCnt;
cout<<"The names you entered are:"<<endl;
for (lCnt = 0; lCnt < 5; lCnt++)
{
cout<<names[lCnt]<<endl;
}
cout<<endl;
This gives me the same output for the names, I need help determining rank 1, rank2, rank3, rank4 and rank5 according to the runners times. The name with the lowest time would be rank1 and so on.
Re: How to determine ranks
Quote:
Originally Posted by
tejen
This gives me the same output for the names, I need help determining rank 1, rank2, rank3, rank4 and rank5 according to the runners times. The name with the lowest time would be rank1 and so on.
If it were my program, I would create a struct called "Runner" or something along those lines. You could have a string for the runners name included, and then a float for their time. Create a vector of 5 runners. Then, using a sort (for this a bubble sort would suffice) you can reorder the vector in order of which time was the lowest, then the next, then the next, etc. Now, since the runners are completely reordered in the vector, you can use that for loop to output each of their names and times in correct order.
Re: How to determine ranks
Thanks for the tip. I haven't gotten as far as learning about structs, vectors and sorts ( maybe thats why i'm having such a hard time trying to figure this out). But I will try what you suggested. again, Thank you.