[英]Insert value into class variable from main function
This is the code i wrote to add two time objects, can anyone help? 这是我编写的用于添加两个时间对象的代码,任何人都可以帮忙吗? I want that user should input time and it adds both time together. 我希望该用户输入时间,并将两个时间加在一起。
Please tell me where i went wrong! 请告诉我我哪里出问题了! please. 请。
class time
{
public: int hh,mm,ss;
};
int main()
{
time t1;
time t2;
cout<<"Enter hour : ";
cin>>t1.hh;
cout<<endl<<"Enter minutes : ";
cin>>t1.mm;
cout<<endl<<"Enter seconds : ";
cin>>t1.ss;
cout<<endl<<"Enter hour : ";
cin>>t2.hh;
cout<<endl<<"Enter minutes : ";
cin>>t2.mm;
cout<<endl<<"Enter seconds : ";
cin>>t2.ss;
cout<<endl;
cout<<t1.hh<<":"<<t1.mm<<":"<<t1.ss;
cout<<" + ";
cout<<t2.hh<<":"<<t2.mm<<":"<<t2.ss;
cout<<" = ";
cout<<t1.hh+t2.hh<<":"<<t1.mm+t2.mm<<":"<<t1.ss+t2.ss;
getch();
return 0;
}
Compiling produces the following errors: 编译会产生以下错误:
main.cpp:13: error: expected
;' before 't1'
main.cpp:13:错误:预期
main.cpp:14: error: expected;' before 't1'
main.cpp:14: error: expected;' before 't1'
main.cpp:14: error: expected;' before 't1'
;'
main.cpp:14: error: expected;' before 't1'
;' before 't2' 在“ t2”之前
main.cpp:14: error: expected
main.cpp:17: error: 't1' was not declared in this scope main.cpp:17:错误:未在此范围内声明“ t1”
main.cpp:24: error: 't2' was not declared in this scope main.cpp:24:错误:未在此范围内声明“ t2”
It would be easier if you provide the error you're having. 如果提供您遇到的错误,将会更容易。
However, you should certainly change 但是,您当然应该改变
class t1;
class t2;
to 至
time t1;
time t2;
Moreover, you should avoid using the name time
since it's a rather classic function in c++ . 而且,您应该避免使用名称time
因为它是c ++中的一个非常经典的函数 。 You could call your class Time
to ensure a reader won't misunderstand your code. 您可以将您的班级称为Time
以确保读者不会误解您的代码。
Of course, you should change the declaration of t1
and t2
as well. 当然,您也应该更改t1
和t2
的声明。 It means you could try: 这意味着您可以尝试:
class Time
{
public: int hh,mm,ss;
};
int main()
{
Time t1;
Time t2;
[...]
Change to: 改成:
int main()
{
time t1;
time t2;
Also you do not handle overflows when adding times (aka you can get more than 60 seconds): 同样,您在添加时间时也不会处理溢出(也就是您可以获得60秒以上的时间):
time t3;
t3.hh = t1.hh + t2.hh;
t3.mm = t1.mm + t2.mm;
t3.ss = t1.ss + t2.ss;
if (t3.ss >= 60)
{
t3.ss -= 60;
t3.mm += 1;
}
if (t3.mm >= 60)
{
t3.mm -= 60;
t3.hh += 1;
}
cout << t3.hh << t3.mm << t3.ss << endl;
I'm assuming you have the following lines not shown: 我假设您有以下未显示的行:
#include <iostream>
#include <stdlib.h>
using namespace std;
Change this line … 更改此行…
class t1;
class t2;
… to … … 至 …
class time t1;
class time t2;
… and your program should compile. …并且您的程序应该编译。
Also, getch
is a part of the conio
library, which I don't have for some reason, so I'm used getchar
instead: 另外, getch
是conio
库的一部分,由于某种原因我没有使用它,因此我改用getchar
:
getchar();
return 0;
Assembled together by the tips above, this should compile and do the job: 通过以上提示组装而成,这应该可以编译并完成工作:
#include <iostream>
using namespace std;
class Time
{
public: int hh,mm,ss;
};
int main()
{
Time t1;
Time t2;
cout<<"Enter hour : ";
cin>>t1.hh;
cout<<endl<<"Enter minutes : ";
cin>>t1.mm;
cout<<endl<<"Enter seconds : ";
cin>>t1.ss;
cout<<endl<<"Enter hour : ";
cin>>t2.hh;
cout<<endl<<"Enter minutes : ";
cin>>t2.mm;
cout<<endl<<"Enter seconds : ";
cin>>t2.ss;
cout<<endl;
cout<<t1.hh<<":"<<t1.mm<<":"<<t1.ss;
cout<<" + ";
cout<<t2.hh<<":"<<t2.mm<<":"<<t2.ss;
cout<<" = ";
cout<<t1.hh+t2.hh<<":"<<t1.mm+t2.mm<<":"<<t1.ss+t2.ss;
getchar();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.