读取和存储用户输入时出现问题 我的程序在C++中遇到了严重的问题。我需要阅读用户输入的一行信息,例如: Bill Jones 20 07:30 09:30 08:00 14:00 00:00 00:00 10:00 13:00 00:00 00:00
所有这些都应该存储在读取和存储用户输入时出现问题 我的程序在C++中遇到了严重的问题。我需要阅读用户输入的一行信息,例如: Bill Jones 20 07:30 09:30 08:00 14:00 00:00 00:00 10:00 13:00 00:00 00:00,c++,class,C++,Class,所有这些都应该存储在Worker类的一个实例中。我遇到的问题是时间,因为他们使用分号。我完全卡住了,到处找了,但找不到任何帮助。这是我目前掌握的代码: #include <iostream> #include <string> using namespace std; class Time; class Worker; ostream &operator << (ostream &, const Time &); istream &
Worker
类的一个实例中。我遇到的问题是时间,因为他们使用分号。我完全卡住了,到处找了,但找不到任何帮助。这是我目前掌握的代码:
#include <iostream>
#include <string>
using namespace std;
class Time;
class Worker;
ostream &operator << (ostream &, const Time &);
istream &operator >> (istream &, Time &);
class Time
{
private:
int hour;
int minute;
public:
Time();
void setHour(int a)
{ hour=a; }
void setMinute(int a)
{ minute=a; }
int getHour() const
{ return hour; }
int getMinute() const
{ return minute; }
Time operator + (const Time &);
};
class Worker
{
public:
int start;
int end;
string fName;
string lName;
double payrate;
double payment;
Time monday,tuesday,wednesday,thursday,friday;
friend istream &operator >> (istream &, Time &);
friend ostream &operator << (ostream &, const Time &);
};
Time::Time()
{
hour = 0;
minute = 0;
}
ostream &operator << (ostream &strm, const Worker &obj)
{
strm << obj.fName << " " << obj.lName << " " << obj.payrate;
return strm;
}
istream &operator >>(istream &strm, Worker &obj)
{
strm >> obj.fName;
strm >> obj.lName;
strm >> obj.payrate;
string token;
int h,m;
string t;
for (int i=0; i<token.size(); i++) {
if(token[i]==':')
{
h = atoi (t.c_str());
t.clear();
}
else
{
t=t+token[i];
}
m=atoi(t.c_str());
}
obj.monday1.setHour(h);
obj.monday1.setMinute(m);
return strm;
}
Time Time::operator+ (const Time &right)
{
Time temp;
temp.hour = hour + right.hour;
temp.minute = minute + right.minute;
return temp;
}
int main ()
{
Time time1, time2;
Worker worker[100];
int num;
cout << "Please enter the number of workers on the payrole: ";
cin >> num;
cout << "Please input the worker’s last name, first name, pay rate, start time, and end time from "<< endl;
cout << "Monday through Friday separated by space." << endl;
for (int i=0; i<num; i++)
{
cin >> worker[i];
}
for (int i=0; i<num; i++)
{
cout << worker[i];
}
return 0;
}
#包括
#包括
使用名称空间std;
上课时间;
班主任;
ostream和operator>(istream和,Time和);
上课时间
{
私人:
整小时;
整数分钟;
公众:
时间();
无效设置小时(整数a)
{hour=a;}
无效设置分钟(整数a)
{minute=a;}
int getHour()常量
{返回时间;}
int getMinute()常量
{返回分钟;}
时间运算符+(常数时间&);
};
班主任
{
公众:
int启动;
内端;
字符串fName;
字符串名称;
双倍工资率;
双倍支付;
时间周一、周二、周三、周四、周五;
friend istream和operator>>(istream和,Time和);
friend ostream&operator>obj.payrate;
字符串标记;
int h,m;
字符串t;
对于(int i=0;i num;
cout在下面的代码块中,您使用的是令牌
和t
的内容,之前没有初始化它们的值:
string token;
int h,m;
string t;
for (int i=0; i<token.size();i++){
if(token[i]==':')
{
h = atoi (t.c_str());
t.clear();
}
字符串令牌;
int h,m;
字符串t;
对于(int i=0;II)最近没有用C++工作,但是我认为在某处有一个字符串函数将分割输入字符串——伪代码<代码> String String(“,”toStra[ 13 ]);< /Cord>?