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. Hi, help to solve the problem on C # Find the product of digits of a given n-digit number. Numbers up to and including five characters are allowed using the switch operator.

    1. Divide the number like this, so that the remainder only turns out and multiply all these remainders.
      Either way, or I didn't understand the problem

  2. #include
    #include

    using namespace std;

    int main() {

    setlocale(LC_ALL, “RUS”);

    int dayNumber;
    cout << "Введите день недели: " <> dayNumber;

    switch (dayNumber)
    {
    case 1:
    cout << "Понидельник:" "Экскурсия. " << endl;
    break;
    case 2:
    cout << "Вторник: Studies. " << endl;
    break;
    case 3:
    cout << "Среда: At 17:00 training. " << endl;
    break;
    case 4:
    cout << "Четверг: grappling competition. " << endl;
    break;
    case 5:
    cout << "Пятница: tusovka" << endl;
    break;
    case 6:
    cout << "Субота: Meeting" << endl;
    break;
    case 7:
    cout << "Воскресение: Anything. " < 8;
    cout << "Ошибка! Enter the correct number. For example from 1 to 7 " << endl;
    break;
    }

    }

  3. Console.WriteLine(“Введите день недели, let's see what you have planned”);
    string str = Console.ReadLine();
    switch (str)
    {
    case “monday”:
    Console.WriteLine(“On Monday you have a meeting with the office”);
    break;
    case “Tuesday”:
    Console.WriteLine(“Meeting up with friends on Tuesday 22:00 “);
    break;

    case “Wednesday”:
    Console.WriteLine(“A date is planned for Wednesday at the Restaurant with a girl”);
    break;
    case “Thursday”:
    Console.WriteLine(“Grocery shopping for home is scheduled for Thursday “);
    break;
    case “Friday”:
    Console.WriteLine(“Car oil change scheduled for Friday “);
    break;
    case “Saturday”:
    Console.WriteLine(“Visit planned for Saturday “);
    break;
    case “Sunday”:
    Console.WriteLine(“Nothing scheduled for Sunday “);
    break;
    default:
    Console.WriteLine(“You entered the wrong day of the week”);
    break;

    }
    Console.ReadLine();

  4. #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “rus”);

    char answer; // will store the user's choice
    bool var = true; // do while loop control variable

    cout <> answer; // value input

    switch (toupper(answer)) // switch принимает переменную answer и ищет подходящий case
    {
    case ‘A’: // если answer равно 1, messages of this case will be displayed on the screen
    cout << "Станция метро Trinitat Nova: ";
    cout << "Время в пути – 15 мин." << endl;
    var = false;
    break; // выход из switch. иначе будет переход case(2) etc.
    case 'B': // если answer равно 2
    cout << "Станция метро Casa de l’Aigua: ";
    cout << "Время в пути – 19 мин." << endl;
    var = false;
    break;
    case 'C':
    cout << "Станция метро Torre Baro Vallbona: ";
    cout << "Время в пути – 25 мин." << endl;
    var = false;
    break;
    case 'D':
    cout << "Станция метро Ciutat Meridiana: ";
    cout << "Время в пути – 30 мин." << endl;
    var = false;
    break;
    case 'E':
    cout << "Станция метро Can Cuias: ";
    cout << "Время в пути – 38 мин." << endl;
    var = false;
    break;
    default: // если ни один case не сработал, default work
    cout << "Станции под номером " << answer << " no! ";
    cout << "Сделайте правильный выбор (from 1 to 5): ";

    }
    } while (var); //цикл повторится, until var changes its value to false

    return 0;

  5. #include

    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “Rus”);
    int answer = 0;
    bool var = true;

    cout <> answer;
    switch (answer)
    {
    case 1:
    cout << "Понедельник:\n"
    "13:00 – Visit Momn"
    "17:00 – Поход в спорт.зал\n";
    break;
    case 2:
    cout << "Вторник:\n"
    "12:00 – Learning to Programn"
    "18:00 – Shopping trip"
    "20:00 – Репетиция группы\n";
    break;
    case 3:
    cout << "Среда:\n"
    "12:00 – Freelance ordern"
    "17:00 – Поход в спорт.зал\n";
    break;
    case 4:
    cout << "Четверг:\n"
    "12:00 – Learning to Programn"
    "18:00 – Встретится с девушкой\n";
    break;
    case 5:
    cout << "Пятница:\n"
    "15:00 – Pick up order from mailn"
    "17:00 – Поход в спорт.зал\n";
    break;
    case 6:
    cout << "Суббота:\n"
    "14:00 – Встреча с друзьями\n";
    break;
    case 7:
    cout << "Воскресенье:\n"
    "На сегодня ничего не запланированно\n";
    break;
    default:
    cout <= 1 && answer <= 7)
    var = false;
    } while (var);

    return 0;

    }

  6. This code has an infinite loop, it continues despite the right case. I understand, what's the matter with if (answer == …), but why is this wrong – I don't understand.

    char answer;
    bool var = true;

    cout <> answer;
    switch (answer)
    {
    case’a’:
    case’A’: cout << "This is way to Ana de Armas.\n";
    cout << "Time: 15 min.";
    break;
    case'b':
    case'B': cout << "This is way to Batumi.\n";
    cout << "Time: 19 min.";
    break;
    case'c':
    case'C': cout << "This is way to Cilicia.\n";
    cout << "Time: 25 min.";
    break;
    case'd':
    case'D': cout << "This is way to Danish.\n";
    cout << "Time: 35 min.";
    break;
    case'i':
    case'I': cout << "This is way to Central Park.\n";
    cout << "Time: 41 min.";
    break;
    default:
    cout << "Wrong value. Try again.\n";
    break;
    }
    if (answer == ('a' | 'A' | 'b' | 'B' | 'c' | 'C' | 'd' | 'D' | 'i' | 'I')) {
    var = false;
    }
    } while (var);

    return 0;

  7. didn't bother much , outputs and thanks for that

    #include
    using namespace std;
    int main()
    {
    char a1;
    cout <> A1;

    switch (A1) {

    case ‘a’:
    case ‘A’:
    cout << "Vinnitsia 12:30";
    break;

    }
    }

  8. #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “RUS”);

    int daynumber;
    bool var = true;
    cout <> daynumber;

    switch (daynumber)
    {
    case 1:
    cout << "на этот день запланирован поход на рыбалку\n";
    break;
    case 2:
    cout << "на этот день запланировано поездка в город с родителями\n";
    break;
    case 3:
    cout << "на этот день запланирован поход на футбольную тренировку\n";
    break;
    case 4:
    cout << "на этот день запланировано прогулка с друзьями\n";
    break;
    case 5:
    cout << "на этот день запланировано поход к репетитору\n";
    break;
    case 6:
    cout << "на этот день запланировано прогулка в лес\n";
    break;
    case 7:
    cout << "на этот день запланирован отдых\n";
    break;
    default:
    cout << "вы ввели не правильный порядковый номер, try again!" <= 1 && daynumber <= 7)
    var = false;
    } while (var);

    }
    RIGHT?

Leave a Reply

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