我应该如何在主函数中初始化时间

How should I initialize the time in the main function?

本文关键字:初始化 时间 函数 我应该      更新时间:2023-10-16

这个程序应该在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
}