Hollow Looi
Hollow Looi

Reputation: 57

My switch is not working

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

It seem that no matter i type what number in option , the output will be let me key in the car brand, after i type and enter , it will just pop our second option to let me key in the car model....it seem the switch case is not working ,how do i fix this?

[UPDATED]

Here is another problem i wanna ask :

I have code like this :

case 1: //Option 1

{

cout<<"\tPlease enter Car Brand:";

cin>>car_brand[x];

}

break;

What I gonna do in order to change the enter car brand the 1st character will be uppercase? For example i type toyota and the output will become Toyota.

Upvotes: 0

Views: 128

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

As already has been pointed out your cases are wrong. For example in this case

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

you exit the loop independing on whether the car is found or not. Also the control from this case label is passed on to the next case label.

This case should look like (I suppose that only one car with the given brand can exist otherwise you should output all information inside the ;loop)

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

try to rewrite all other cases the similar way.

Upvotes: 1

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

You do not have break at the end of your cases. The break statements are inside the loops.

Upvotes: 4

Beed
Beed

Reputation: 460

You have put the break statements inside your for loops, which will break you from the loop and then fall through to the next switch case. If you want to break from the switch case, the break statement should be the last line before the closing brace in your switch case.

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

Upvotes: 4

Roddy
Roddy

Reputation: 68033

Your breaks are in completely the wrong place.

Move them outside the for loops!

Upvotes: 4

Related Questions