In: Computer Science
in C++
Using your previous homework code:
1) Add a default constructor for the runners. ( one that has no parameters)
2) Add a constructor that takes a name and a bib number
3) Add a constructor that takes a name , a bib number and a sex.
Add setter functions that sets the time:
1) one that takes a string of the format : hh:mm:ss
2) one that takes an int that is the number of seconds the runner took to complete the race.
Please Note: You decide what format inside the class to store the time amount.
Add getter functions that return the time:
1) one that returns a string in the format : hh:mm:ss
2) one that returns an int that this the time in seconds.
Add a comment section to your class and explain why you chose some things.
1) What data type did you decide on in the class for the time, why
2) What was an alternative? Is your decision a big decision or a small decision? why
Please note : Again pick one C++ data type inside the class to store the time.
Do not use two variables to store the time.
Please note: if you write conversion functions that if expected. Do not make them public.
Please note: on the time returned as a string please test and verify that times like these contain the correct zeros not blanks. the format will always be either: ( b = blank, hh = hours, mm = minutes, ss = seconds )
bh:mm:ss
hh:mm:ss
so a time like 1:03:02 will NOT be displayed as : 1:3:2. I strongly suggest testing in these areas.
You may want to convert from a string to an integer for you students here is some
sample code
String conversion using stoi() or atoi()
stoi() : The stoi() function takes a string as an
argument and returns its value. Following is a simple
implementation:
filter_none
edit
play_arrow
brightness_4
// C++ program to demonstrate working of stoi() // Work only if compiler supports C++11 or above. #include <iostream> #include <string> using namespace std;
int main() { string str1 = "45"; string str2 = "3.14159"; string str3 = "31337 geek";
int myint1 = stoi(str1); int myint2 = stoi(str2); int myint3 = stoi(str3);
cout << "stoi(\"" << str1 << "\") is " << myint1 << '\n'; cout << "stoi(\"" << str2 << "\") is " << myint2 << '\n'; cout << "stoi(\"" << str3 << "\") is " << myint3 << '\n';
return 0; } |
Output:
stoi("45") is 45 stoi("3.14159") is 3 stoi("31337 geek") is 31337 Please Note: You may decide that you need a function that returns a 2 character long string given an integer. This is starter code . It does not fully help you in this project you will need to modify or add a bit of stuff to get it to be truly helpful. the statement temp+=number%10+48; could be coded as : temp+=number%10+'0'; Here is some starter code:
string convertInt(int number) { if (number == 0) return "0"; string temp=""; string returnvalue=""; while (number>0) { temp+=number%10+48; number/=10; } for (int i=0;i<temp.length();i++) returnvalue+=temp[temp.length()-i-1]; return returnvalue; }
this is my code from my previous homework:
#include<iostream>
#include<string>
using namespace std;
class Runner
{
private:
string bibnumber;
char gender;
int age;
string name;
string time;
public:
Runner()
{
bibnumber=" ";
gender=' ';
age=0;
name=" ";
time=" ";
}
int getAge()
{
return age;
}
string getName()
{
return name;
}
string getTime()
{
return time;
}
string getBin()
{
return bibnumber;
}
char getGender()
{
return gender;
}
void setCombinedGenderBib ( string bib)
{
gender=bib[0];
bibnumber=bib.substr(1,bib.length());
}
void setName(string name)
{
this->name=name;
}
void setTime(string time)
{
this->time= time;
}
void setAge(int age)
{
this->age= age;
}
};
int main()
{
Runner runner;
runner.setName("john");
runner.setAge(23);
runner.setTime("01:04:37");
runner.setCombinedGenderBib("M4321");
cout<<"Name : "<<runner.getName()<<endl;
cout<<"Bin Number : "<<runner.getBin()<<endl;
cout<<"Age : "<<runner.getAge()<<endl;
cout<<"Gender : "<<runner.getGender()<<endl;
cout<<"Time : "<<runner.getTime()<<endl;
return0;
Hi , following implementation meets all the above requirements. Hope you find it intresting...
#include<iostream>
#include<string>
#include <string.h>
using namespace std;
class Runner{
private:
string bibnumber;
char gender;
int age;
string name;
string time;
public:
//1) Add a default constructor for the runners. ( one that has no
parameters)
Runner(){
bibnumber=" ";
gender=' ';
age=0;
name=" ";
time=" ";
}
//2) Add a constructor that takes a name and a bib number
Runner(string name,string bibnumber){
this->name=name;
this->bibnumber=bibnumber;
gender=' ';
age=0;
time=" ";
}
//3) Add a constructor that takes a name , a bib number and a
sex.
Runner(string name,string bibnumber,char gender){
this->name=name;
this->bibnumber=bibnumber;
this->gender=gender;
age=0;
time=" ";
}
//1) one that takes a string of the format : hh:mm:ss
void setTime(string time){
this->time=time;
int tempTime=getSecondsTim();
setTime(tempTime);
}
#define HOUR 3600
#define MIN 60
//2) one that takes an int that is the number of seconds the runner
took to complete the race.
// right......................
void setTime(int numOfSeconds){
int hour=numOfSeconds/HOUR;
int second=numOfSeconds % HOUR;
int minute=second/MIN;
second %= MIN;
char s[50];
//( b = blank, hh = hours, mm = minutes, ss = seconds )
sprintf(s,"%.1d:%.2d:%.2d",hour,minute,second);
this->time=s;
}
int getAge(){
return age;
}
string getName(){
return name;
}
//1) one that returns a string in the format : hh:mm:ss
//Note:time is always stored in proper format by setters...
string getTime(){
return time;
}
//2) one that returns an int that this the time in
seconds.
// right................
int getSecondsTim(){
int n = time.length();
// declaring character array
char char_array[n + 1];
// copying the contents of the
// string to char array
strcpy(char_array, time.c_str());
char *token=strtok(char_array,":");
int hh,mm,ss;
int i=0;
while(token!=NULL){
if(i==0){
hh=stoi(token);
// printf("---------");
}
if(i==1){
mm=stoi(token);
// printf("---------");
}
if(i==2){
ss=stoi(token);
// printf("---------");
}
i++;
token=strtok(NULL,":");
}
return hh*3600 + mm*60 + ss;
}
string getBin(){
return bibnumber;
}
char getGender(){
return gender;
}
void setCombinedGenderBib( string bib){
gender=bib[0];
bibnumber=bib.substr(1,bib.length());
}
void setName(string name){
this->name=name;
}
void setAge(int age){
this->age= age;
}
};
int main(){
Runner runner;
runner.setName("john");
runner.setAge(23);
runner.setTime(590);
//runner.setTime("01:04:37");
runner.setCombinedGenderBib("M4321");
cout<<"Name : "<<runner.getName()<<endl;
cout<<"Bin Number :
"<<runner.getBin()<<endl;
cout<<"Age : "<<runner.getAge()<<endl;
cout<<"Gender :
"<<runner.getGender()<<endl;
cout<<"Time : "<<runner.getTime()<<endl;
return 0;
}