C++ 字符串处理时遇到无限循环 #包括 #包括 使用名称空间std; 上课时间 { 整小时=0,扫描小时=0; int分钟=0,scan_分钟=0; 公众: 无效输入格式为1的时间(无效) { cout60 | | h

C++ 字符串处理时遇到无限循环 #包括 #包括 使用名称空间std; 上课时间 { 整小时=0,扫描小时=0; int分钟=0,scan_分钟=0; 公众: 无效输入格式为1的时间(无效) { cout60 | | h,c++,algorithm,C++,Algorithm,问题最可能的原因是线路: #include<iostream> #include<cstdio> using namespace std; class Time { int hours=0,scan_hours=0; int minutes=0,scan_minutes=0; public: void input_time_in_format_1(void) { cout<<"\n\t Enter the time in the

问题最可能的原因是线路:

#include<iostream>
#include<cstdio>

using namespace std;

class Time
{
    int hours=0,scan_hours=0;
    int minutes=0,scan_minutes=0;

public:

void input_time_in_format_1(void)
{
    cout<<"\n\t Enter the time in the format hh:mm A.M./P.M.:  ";
    if(scanf("%d",&hours)==1)
    {
        if(scanf(":%d A.M.",&minutes)==1)
        {   
            hours=(hours+12)%24;
                if(hours>13||minutes>60||hours<0||minutes<0)
                {   
                    cout<<"\n\t Wrong entry";
                    hours=minutes=0;
                }
                else    
                    cout<<"\n\t Your time has been accepted";
        }
        else if(scanf(":%d P.M.",&minutes)==1)
        {
            if(hours>13||minutes>60||hours<0||minutes<0)
                {   
                    cout<<"\n\t Wrong entry";
                    hours=minutes=0;
                }

            else
                cout<<"\n\t Your time has been accepted";   
        }   
        else
        {   
            cout<<"\n\t Wrong entry";
            hours=0;
            minutes=0;
        }

    }
    else
    {   
        cout<<"\n\t Wrong entry";
        hours=0;
    }


}
void input_time_in_format_2(void)
{
    cout<<"\n\t Enter the time in the format hh:mm hours:  ";
    if(scanf("%d",&hours)==1 && hours<24)
    {
        if(scanf(":%d hours",&minutes)==1 && minutes<60)
            cout<<"\n\t Your time has been accepted";

        else
        {   
            cout<<"\n\t Wrong entry";
            hours=minutes=0;
        }
    }
    else
    {   
        cout<<"\n\t Wrong entry";
        hours=0;
    }
}


void show_time_in_format_1(void)
{
    cout<<"\n\t Showing time in 24 hours format: "<<hours<<":"<<minutes<<" hours";
}

void show_time_in_format_2(void)
{
    if(hours>12)
        cout<<"\n\t Showing time in 12 hours format: "<<(hours%12)<<":"<<minutes<<" P.M.\n\n";
    else
        cout<<"\n\t Showing time in 12 hours format: "<<hours<<":"<<minutes<<" A.M.\n\n";
}
void add_time(void)
{
    cout<<"\n\t Please enter the hours and minutes respectively";
    cin>>scan_hours;
    cin>>scan_minutes;
    if(minutes+=scan_minutes>=60)
    {
        hours+=(scan_hours+1);
        hours%=24;
    }
    else
    {
        hours+=(scan_hours);
        hours%=24;
    }
}


};

int main()
{
    Time t1;
    int choice;
    do
    {
        cout<<"\n\t\t\t --Welcome--"<<"\n\n\t Please enter your choice \n\t1.Input time in 12 hours format\n\t2.Input time in 24 hours format\n\t3.Show time in 12 hours format\n\t4.Show time in 24 hours format\n\t5.Add user input time\n\t6.Exit....-->";
        cin>>choice;
        switch(choice)
        {
            case 1:cin.ignore();
                t1.input_time_in_format_1();
                break;

            case 2:
                t1.input_time_in_format_2();
                break;

            case 3:
                t1.show_time_in_format_2();
                break;

            case 4:
                t1.show_time_in_format_1();
                break;

            case 5:
                t1.add_time();
                break;

            case 6:
                cout<<"\n\t Thank You";
                exit(0);

            default:
                cout<<"\n\n\t Wrong entry";
                break;

        }

}while(1);

return 0;
}   
这是失败的,因为istream包含新行,这将导致
cin
失败

建议致电:

cin >> choice;

在输出提示/菜单之前,istream将为空,用户输入的对菜单的响应将被放入变量'choice;

中。您是否尝试过使用调试器迭代每个循环?我不知道如何操作。那么,现在是学习如何使用调试器的最佳时机了。这并不难理解“开始,是任何严肃开发者的基本技能。如果Linux上是一个常用的调试器。1)声明:<代码>使用命名空间STD:通常是错误的。2)这是一个C++程序,所以请删除<代码> C <代码> TAGCAN,你建议我从哪里可以学到?
cin.ignore();