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

时间:2019-01-30 16:48:18

标签: c++ struct

该程序应在Visual Studio中运行,但不运行。它说我在time函数中声明的main变量做错了。

  

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 << "\n\nYou did this program " << count << "times\n";
    }

    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;
    }    
}

1 个答案:

答案 0 :(得分:0)

问题在于,实际上您的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
}