我的开关无法正常工作

时间:2014-08-26 21:39:08

标签: c++ visual-studio-2010 switch-statement

case 1: //Option 1
    {
        cout<<"Enter Car Brand:"<<endl;
        cin>>search_car; //Identify which car brand to be search
        search_car.front() = std::toupper( search_car.front() );
        for(int x=0; x<cnt; x++) //Loop to last account, by using cnt we can stop when the array is a null
        {
            if(search_car==car_brand[x])//Detecting which car brand same as user input
            {
                cout<<"\n\tCar Registration Number:"<<car_num[x]<<endl;
                cout<<"\n\tCar Make or Brand:"<<car_brand[x]<<endl;
                cout<<"\n\tCar Model:"<<car_model[x]<<endl;
                cout<<"\n\tCar Colour:"<<car_colour[x]<<endl;
                cout<<"\n\tYear of Manufacturing:"<<car_year[x]<<endl;
                cout<<"\n\tEngine Capacity:"<<car_eng_cap[x]<<"cc"<<endl;
                cout<<"\n\tTransmission:"<<car_tran[x]<<endl;
                cout<<"\n\tCost of Car: RM"<<car_cost[x]<<endl;
            }
            else
                cout<<"No data is founded."<<endl;
            break;
        }
    }
case 2: //Option 2
    {
        cout<<"Enter Car Model:"<<endl;
        cin>>search_car; //Identify which car model to be search
        search_car.front() = std::toupper( search_car.front() );
        for(int y=0 ; y<cnt ; y++)
        {
            if(search_car==car_model[y])//Detecting which car model same as user input
            {
                cout<<"\n\tCar Registration Number:"<<car_num[y]<<endl;
                cout<<"\n\tCar Make or Brand:"<<car_brand[y]<<endl;
                cout<<"\n\tCar Model:"<<car_model[y]<<endl;
                cout<<"\n\tCar Colour:"<<car_colour[y]<<endl;
                cout<<"\n\tYear of Manufacturing:"<<car_year[y]<<endl;
                cout<<"\n\tEngine Capacity:"<<car_eng_cap[y]<<"cc"<<endl;
                cout<<"\n\tTransmission:"<<car_tran[y]<<endl;
                cout<<"\n\tCost of Car: RM"<<car_cost[y]<<endl;
            }
            else
                cout<<"No data is founded."<<endl;
            break;
        }
    }
case 3: //Option 3
    {
        cout<<"Enter Year of Manufacturing:"<<endl;
        cin>>search_car;
        for(int x=0; x<cnt; x++)
        {
            if(search_car==car_year[x])//Detecting which car manufacturing year same as user input
            {
                cout<<"\n\tCar Registration Number:"<<car_num[x]<<endl;
                cout<<"\n\tCar Make or Brand:"<<car_brand[x]<<endl;
                cout<<"\n\tCar Model:"<<car_model[x]<<endl;
                cout<<"\n\tCar Colour:"<<car_colour[x]<<endl;
                cout<<"\n\tYear of Manufacturing:"<<car_year[x]<<endl;
                cout<<"\n\tEngine Capacity:"<<car_eng_cap[x]<<"cc"<<endl;
                cout<<"\n\tTransmission:"<<car_tran[x]<<endl;
                cout<<"\n\tCost of Car: RM"<<car_cost[x]<<endl;
            }
            else
                cout<<"No data is founded."<<endl;
            break;
        }
    }
case 4: //Option 4
   {
       cout<<"Enter Car Price:"<<endl;
       cin>>search_car;
       for(int x=0 ; x<cnt; x++)
       {
           if(search_car==car_cost[x])//Detecting which car price same as user input
           {
               cout<<"\n\tCar Registration Number:"<<car_num[x]<<endl;
               cout<<"\n\tCar Make or Brand:"<<car_brand[x]<<endl;
               cout<<"\n\tCar Model:"<<car_model[x]<<endl;
               cout<<"\n\tCar Colour:"<<car_colour[x]<<endl;
               cout<<"\n\tYear of Manufacturing:"<<car_year[x]<<endl;
               cout<<"\n\tEngine Capacity:"<<car_eng_cap[x]<<"cc"<<endl;
               cout<<"\n\tTransmission:"<<car_tran[x]<<endl;
               cout<<"\n\tCost of Car: RM"<<car_cost[x]<<endl;
            }
           else
                cout<<"No data is founded."<<endl;
           break;
       }
   }
case 5: //Option 5
    {
        cout<<"Operation Canceled."<<endl;
        break;
    }

似乎无论我在选项中输入什么号码,输出都会让我键入汽车品牌,在我输入并输入后,它会弹出我们的第二个选项让我键入汽车模型.. ..似乎开关盒不工作,我该如何解决这个问题?

[增订]

这是我想问的另一个问题:

我有这样的代码:

案例1://选项1

{

cout&lt;&lt;&#34; \ t请输入汽车品牌:&#34;;

CIN&GT;&GT; car_brand [X];

}

中断;

为了改变进入汽车品牌,我要做的第一个角色将是大写的?例如,我输入丰田,输出将成为丰田。

4 个答案:

答案 0 :(得分:4)

您的break完全错误。

将他们移到 for循环外面

答案 1 :(得分:4)

你已经将break语句放在for循环中,这将使你从循环中脱离出来,然后进入下一个switch case。如果你想从switch case断开,break语句应该是你的switch case中右括号之前的最后一行。

case 1:
{
    for(...)
    {
        ...
    }
    break;
}

答案 2 :(得分:4)

您的案件结束时没有breakbreak语句位于循环内。

答案 3 :(得分:1)

正如已经指出你的情况是错误的。例如,在这种情况下

case 1: //Option 1
    {
        cout<<"Enter Car Brand:"<<endl;
        cin>>search_car; //Identify which car brand to be search
        search_car.front() = std::toupper( search_car.front() );
        for(int x=0; x<cnt; x++) //Loop to last account, by using cnt we can stop when the array is a null
        {
            if(search_car==car_brand[x])//Detecting which car brand same as user input
            {
                cout<<"\n\tCar Registration Number:"<<car_num[x]<<endl;
                cout<<"\n\tCar Make or Brand:"<<car_brand[x]<<endl;
                cout<<"\n\tCar Model:"<<car_model[x]<<endl;
                cout<<"\n\tCar Colour:"<<car_colour[x]<<endl;
                cout<<"\n\tYear of Manufacturing:"<<car_year[x]<<endl;
                cout<<"\n\tEngine Capacity:"<<car_eng_cap[x]<<"cc"<<endl;
                cout<<"\n\tTransmission:"<<car_tran[x]<<endl;
                cout<<"\n\tCost of Car: RM"<<car_cost[x]<<endl;
            }
            else
                cout<<"No data is founded."<<endl;
            break;
        }
    }

根据是否找到汽车而退出循环。此案例标签的控制权也会传递给下一个案例标签。

这种情况应该是这样的(我想只有一辆具有给定品牌的汽车可以存在,否则你应该输出;循环内的所有信息)

case 1: //Option 1
    {
        cout<<"Enter Car Brand:"<<endl;
        cin>>search_car; //Identify which car brand to be search
        search_car.front() = std::toupper( search_car.front() );

        int x = 0; 
        while ( x < cnt && search_car != car_brand[x] ) x++; //Loop to last account, by using cnt we can stop when the array is a null

        if ( x != cnt )//Detecting which car brand same as user input
        {
            cout<<"\n\tCar Registration Number:"<<car_num[x]<<endl;
            cout<<"\n\tCar Make or Brand:"<<car_brand[x]<<endl;
            cout<<"\n\tCar Model:"<<car_model[x]<<endl;
            cout<<"\n\tCar Colour:"<<car_colour[x]<<endl;
            cout<<"\n\tYear of Manufacturing:"<<car_year[x]<<endl;
            cout<<"\n\tEngine Capacity:"<<car_eng_cap[x]<<"cc"<<endl;
            cout<<"\n\tTransmission:"<<car_tran[x]<<endl;
            cout<<"\n\tCost of Car: RM"<<car_cost[x]<<endl;
        }
        else
        }
            cout<<"No data is founded."<<endl;
        }

        break;
    }

尝试以类似的方式重写所有其他案例。