我应该如何在主函数中初始化时间
How should I initialize the time in the main function?
这个程序应该在Visual Studio中工作,但它不起作用。它说我对我在 main
函数中声明的 time
变量做错了什么。
source_file.cpp(59):警告 C4700:使用了未初始化的局部变量"time"
我知道我应该初始化time
但我不知道该怎么做。
当我尝试时
input time (0,0, ' ')
它显示了更多的错误。
source_file.cpp(43):错误 C2440:"正在初始化":无法从"初始值设定项列表"转换为"输入"
source_file.cpp(43):注意:没有构造函数可以采用源类型,或者构造函数重载分辨率不明确
另外,我的编码风格很混乱,所以我对此感到抱歉。这不是因为我懒惰,而是我是一个初学者,我喜欢循环和条件语句。
struct input
{
char ampm;
char dayNight;
int hour;
int minute;
};
input take ( input );
input convert ( input );
void give ( input );
int main ()
{
input time(0,0,'');
char ans = 0;
int count = 0;
int morea = 0, morep = 0;
do {
time = take ( time );
time = convert ( time );
give ( time );
++count;
if ( time.ampm == 'a' ) {
++morea;
}
else if ( time.ampm == 'p' ) {
++morep;
}
cout << "Do you want to do it again? (y/n)";
cin >> ans;
} while ( ans == 'y' || ans == 'Y' );
if ( ans != 'y' || ans != 'Y' ) {
cout << "nnYou did this program " << count << "timesn";
}
if ( morea > morep ) {
cout << "You converted more AM time";
}
else if ( morea < morep ) {
cout << "You converted more PM time";
}
else if ( morea == morep ) {
cout << "The conversion type was equal";
}
return 0;
} //int main
input take ( input time ) {
cout << "nPlease enter 12-hour format or 24-hour format (12-hour = a, 24-hour = p) ";
cin >> time.ampm;
if ( time.ampm == 'a' ) {
cout << "Please enter day or night (day = x, night = y) ";
cin >> time.dayNight;
}
cout << "Please enter the hour ";
cin >> time.hour;
cout << "Please enter the minute ";
cin >> time.minute;
return time;
}
input convert ( input time ) {
if ( time.ampm == 'p' ) {
if ( time.hour >= 13 && time.hour < 24 ) {
time.hour = time.hour - 12;
}
else if ( time.hour == 24 || time.hour == 00 ) {
time.hour = 12;
}
}
else if ( time.ampm == 'a' ) {
if ( time.hour == 12 && time.dayNight == 'y' ) {
time.hour = 00;
}
if ( time.hour == 24 && time.dayNight == 'y' ) {
time.hour = 00;
}
else if ( time.hour >= 1 && time.hour < 12 ) {
time.hour = time.hour + 12;
}
else if ( time.hour == 12 && time.dayNight == 'x' ) {
time.hour = 12;
}
}
return time;
}
void give ( input time ) {
if ( time.ampm == 'a' ) {
cout << "The new time in 24-hour format is " << time.hour << ":" << time.minute << endl;
}
else {
cout << "The new time in 12-hour format is " << time.hour << ":" << time.minute << endl;
}
}
问题是你正在将一个外行变量传递给你的take
函数,而实际上你的take
函数应该返回一个时间值。
由于take
的目的是让用户输入时间值,因此不应将时间值作为其参数。它需要做的就是返回用户输入的时间值。
喜欢这个
input take(); // no parameter
input convert(input);
void give(input);
...
int main () {
char ans = 0;
...
input time = take(); // no parameter
...
}
input take () { // no parameter
input time; // local variable for the users input
cout << "nPlease enter 12-hour format or 24-hour format (12-hour = a, 24-hour = p) ";
cin >> time.ampm;
...
return time; // return the entered time value
}
相关文章:
- 是否可以初始化不可复制类型的成员变量(或基类)
- C++使用整数的压缩数组初始化对象
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 具有静态存储持续时间的常量初始化变量的初始化顺序
- 静态存储持续时间初始化
- 在不同翻译单元中具有静态存储持续时间的依赖非局部常量浮点变量的常量初始化
- 均衡符号和初始化之间的时间和空间复杂性是否存在差异
- 我应该如何在主函数中初始化时间
- C++中结构的存储持续时间和成员初始化
- 初始化结构向量需要大量的编译时间
- 以合理的编译时间静态初始化巨大的uint8_t数组
- 变量初始化的时间有关系吗
- 对于静态初始化的大数组(例如65536条目),编译时间非常长
- 是块作用域静态或线程存储持续时间变量初始化失败的原因
- c++中静态数据初始化的时间和方式
- 数组初始化的时间复杂度是多少?
- 初始化带有静态存储时间的std::数组,带有参数Pack扩展和附加值
- 是静态存储持续时间初始化为NULL或全零的对象中未初始化的指针
- 在main()之前初始化对象的静态存储持续时间
- 如何初始化CUDA,以便我可以进行有效的执行时间测量