The basics of programming in c++ for beginners

Tasks: The switch statement

In this article you will find tasks, to help you master the basic skills to use in their programs the statement multiple choiceswitch. I read about this in the article can be The switch statement in C ++.  Most requested – try first to solve the task yourself and then either sneak peek (If so and it did not work), or compare your decision with the, what is set out here. Let's go!

1. Create a schedule for the week. The user enters a numeric representation of the day of the week and it is displayed on the screen, then, which is scheduled for this day.

2. Change code, which was considered in the articleThe switch statement in C ++, in the following way: The user to calculate the travel time does not need to enter station numbers, and the letters(A,B,C,D,E). You must provide user input and small, and large letters (get rid of the case-sensitive).  That is, if you have entered or D, or d – should work the same case.  The syntax for this action is the:

switch (expression)
{
case ‘a’:
case ‘A’: //code
break;
etc. …………….
}

Another task can be viewed at this article. For all questions, leave a comment below the article.

29 thoughts on “Tasks: The switch statement

  1. #include
    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    // consider in solving such a problem: Let's, the user has a numbered list of the metro stations in Barcelona.
    // You must write code, wherein the dialogue with the user is implemented,
    // namely, asked to enter room underground station. After entering the Station Number
    // we have to show it on the screen and the name of the travel time. If stations with such
    //,no number, inform and propose to enter the number again.

    int num = 0;//metro station number

    do{
    if(on one >= 1 || on one <= 5)
    cout <> on one;
    switch(on one)
    {
    case 1 : cout << "\nСтанция метро ВОСТОК Время в пути 22 минуты \n";
    break;
    case 2 : cout << "\nСтанция метро СОВЕТСКАЯ Время в пути 19 минут\n";
    break;
    case 3 : cout << "\nСтанция метро ВОЛГА Время в пути 11 минут\n";
    break;
    case 4 : cout << "\nСтанция метро ЗВЕЗДА Время в пути 16минут\n";
    break;
    case 5 : cout << "\nСтанция метро ЛЕНИНСКАЯ Время в пути 9 минут\n";
    break;
    default : cout << "\nНет такого номера станции!\n";
    }
    if(on one 5){
    num = 0;//overwrite the exit variable
    cout << "\nДля продолжения выбора станции нажмите 1\n";
    cout <> on one;}
    cout <= 1 && num = 1 || on one <= 5), costs ||(or), then cout <= 1 && on one <= 5);-(&&) – it doesn't work! Prompt!

  2. I repeat :I had to edit my previous comment
    #include
    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    // consider in solving such a problem: Let's, the user has a numbered list of the metro stations in Barcelona.
    // You must write code, wherein the dialogue with the user is implemented,
    // namely, asked to enter room underground station. After entering the Station Number
    // we have to show it on the screen and the name of the travel time. If stations with such
    //,no number, inform and propose to enter the number again.

    int num = 0;//metro station number

    do{
    if(on one >= 1 || on one <= 5)
    cout <> on one;
    switch(on one)
    {
    case 1 : cout << "\nСтанция метро ВОСТОК Время в пути 22 минуты \n";
    break;
    case 2 : cout << "\nСтанция метро СОВЕТСКАЯ Время в пути 19 минут\n";
    break;
    case 3 : cout << "\nСтанция метро ВОЛГА Время в пути 11 минут\n";
    break;
    case 4 : cout << "\nСтанция метро ЗВЕЗДА Время в пути 16минут\n";
    break;
    case 5 : cout << "\nСтанция метро ЛЕНИНСКАЯ Время в пути 9 минут\n";
    break;
    default : cout << "\nНет такого номера станции!\n";
    }
    if(on one 5){
    num = 0;//overwrite the exit variable
    cout << "\nДля продолжения выбора станции нажмите 1\n";
    cout <> on one;}
    cout <= 1 && on one <= 5);
    getch();
    return 0;

    }

  3. in the problem 2 var is initiated before the loop and is false. If the default block is triggered and var changes to true, the loop becomes infinite

Leave a Reply

Your email address will not be published. Required fields are marked *