注册 登录
编程论坛 VC++/MFC

新手求思路,求指点。急!!谢谢!!!

yang20110816 发布于 2011-04-06 22:56, 836 次点击
声明一个表示时间的结构体,可以精确表示年,月,日,小时,分,秒;提示用户输入年,月,日,小时,分,秒的值,然后完整地显示出来。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Time{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
};
int main(){
    int year,month,day,hour,minute,second;
   Time ti={year,month,day,hour,minute,second};
        cout<<"Year: "<<ti.year<<endl;
         cout<<"Month: "<<ti.month<<endl;
          cout<<"Day: "<<ti.day<<endl;
            cout<<"Hour: "<<ti.hour<<endl;
             cout<<"Minute: "<<ti.minute<<endl;
              cout<<"Second: "<<ti.second<<endl;

return 0;
}
检查出现C:\Program Files\VC++\MyProjects\p63\31.cpp(16) : warning C4700: local variable 'year' used without having been initialized
C:\Program Files\VC++\MyProjects\p63\31.cpp(16) : warning C4700: local variable 'month' used without having been initialized
C:\Program Files\VC++\MyProjects\p63\31.cpp(16) : warning C4700: local variable 'day' used without having been initialized
C:\Program Files\VC++\MyProjects\p63\31.cpp(16) : warning C4700: local variable 'hour' used without having been initialized
C:\Program Files\VC++\MyProjects\p63\31.cpp(16) : warning C4700: local variable 'minute' used without having been initialized
C:\Program Files\VC++\MyProjects\p63\31.cpp(16) : warning C4700: local variable 'second' used without having been initialized
但是可以执行;
      #include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Time{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
};
int main(){
    int year,month,day,hour,minute,second;
        cout<<"please enter the year,month,day,hour,minute,second"<<endl;
    cin>>"year: ">>"month: ">>"day: ">>"hour: ">>"minute: ">>"second: ">>endl;
    Time ti={year,month,day,hour,minute,second};
        cout<<"Year: "<<ti.year<<endl;
         cout<<"Month: "<<ti.month<<endl;
          cout<<"Day: "<<ti.day<<endl;
            cout<<"Hour: "<<ti.hour<<endl;
             cout<<"Minute: "<<ti.minute<<endl;
              cout<<"Second: "<<ti.second<<endl;

return 0;
}
    这个检查出现一个错误
error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
      求解释!!
2 回复
#2
qq10235692232011-04-07 08:52
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Time{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
};
int main()
{
    int year=2011,month=4,day=7,hour=9,minute=0,second=0;  //编译器的意思是你的变量在使用前没有赋值
    Time ti={year,month,day,hour,minute,second};
        cout<<"Year: "<<ti.year<<endl;
        cout<<"Month: "<<ti.month<<endl;
        cout<<"Day: "<<ti.day<<endl;
        cout<<"Hour: "<<ti.hour<<endl;
        cout<<"Minute: "<<ti.minute<<endl;
        cout<<"Second: "<<ti.second<<endl;

   return 0;
}
#3
yang201108162011-04-07 20:53
回复 2楼 qq1023569223
谢啦,明白!!
 我第一种的也明白了,我在“>>second”后面多了个“>>endl;” 错在这了。
 
1