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.
// Switch_1.cpp: the main project file.
#include “stdafx.h”
#include “iostream”
#include “conio.h”
#include “time.h”
using namespace System;
using namespace std;
void mon()
{
system(“cls”);
cout<<"\n По расписанию самый тяжелый день…";getch();system("cls");
}
void tue()
{
system("cls");
cout<<"\n Сегодня уже лучше, но ещё многое стоит преодалеть.";getch();system("cls");
}
void wed()
{
system("cls");
cout<<"\n Так, заветная середина.";getch();system("cls");
}
void thu()
{
system("cls");
cout<<"\n Ура, Finally, tomorrow Friday!";getch();system("cls");
}
void fri()
{
system("cls");
cout<<"\n Просто без коментариев…";getch();system("cls");
}
void sat()
{
system("cls");
cout<<"\n Имея – not appreciate, losing – suffer…\n I mean the weekend ;D";getch();system("cls");
}
void sun()
{
system("cls");
cout<<"\n Не уходи…Please not today…";getch();system("cls");
}
int main()
{
setlocale(LC_ALL, "russian");
srand(time(0));
int a;
bool u=0;
while(!in)
{
cout<<"\n\n\n 1. Понедельник 5. Friday n n 2. Вторник 6. Saturday n n 3. Среда 7. Sunday n n 4. Четверг 8. Выход из программы \n\n ";
cout<>a;
switch(a)
{
case 1:system(“cls”);cout<<"\n\tПонедельник ";getch();mon();
break;
case 2:system("cls");cout<<"\n\tВторник ";getch();tue();
break;
case 3:system("cls");cout<<"\n\tСреда ";getch();wed();
break;
case 4:system("cls");cout<<"\n\tЧетверг ";getch();letter();
break;
case 5:system("cls");cout<<"\n\tПятница ";getch();fri();
break;
case 6:system("cls");cout<<"\n\tСуббота ";getch();sat();
break;
case 7:system("cls");cout<<"\n\tВоскресенье ";getch();sun();
break;
case 8:return 0;
break;
default:system("cls");cout<<"\n Вы ввели невозможное значение! Введите ещё раз.";
}
}
getch();
cout<<"\n\n\n";
return 0;
}
if you know a little bit with functions and recursion, you can do so:
#include
using namespace std;
void k() {
char n;
cin >> n;
switch (n) {
case ‘a’:
case ‘A’:
cout << endl << "Станция метро Can Cuias :" << endl << "Время в пути – 38 мин." << endl;
break;
case 'b':
case 'B':
cout << endl << "Станция метро Ciutat Meridiana:" << endl << "Время в пути – 30 мин." << endl;
break;
case 'c':
case 'C':
cout << endl << "Станция метро Torre Baro Vallbona:" << endl << "Время в пути – 25 мин." << endl;
break;
case 'd':
case 'D':
cout << endl << "Станция метро Casa de l’Aigua:" << endl << "Время в пути – 19 мин." << endl;
break;
case 'e':
case 'E':
cout << endl << "Станция метро Trinitat Nova:" << endl << "Время в пути – 15 мин." << endl;
break;
default:
cout << endl << "Извините, but with a letter defining the station no." << endl < “;
return k();
break;
}
}
int main() {
setlocale(LC_ALL, “Russian”);
cout < “;
k();
cout << endl;
system("PAUSE");
return 0;
}
it seems to me, much easier)
this is not right, I think!
this is not right, I think!
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “rus”);
int answer = 0;
bool var = true;
cout << "Введи день недели, that is scheduled to uznacht (from 1 to 7): "<> answer; // value input
switch (answer)
{
case 1:
cout << "ПОНЕДЕЛЬНИК." << endl;
cout << "Отходим после выходных." << endl;
break;
case 2:
cout << "ВТОРНИК " << endl;
cout << "Подготовка к работе " << endl;
break;
case 3:
cout << "СРЕДА " << endl;
cout << "РАБОТАЕМ! " << endl;
break;
case 4:
cout << "ЧЕТВЕРГ " << endl;
cout << "Отдыхаем после работы. " << endl;
break;
case 5:
cout << "ПЯТНИЦА " << endl;
cout << "Подготовка к выходным. " << endl;
break;
case 6:
cout << "СУББОТА " << endl;
cout << "Генеральная репетиция выходного. " << endl;
break;
case 7:
cout << "ВОСКРЕСЕНЬЕ " << endl;
cout << "ВЫХОДНОЙ!!! " << endl;
break;
default:
cout << "Э-э-э… Day of the week with the number " << answer << " no! ";
cout << "У тебя вчера был ВЫХОДНОЙ? " << endl;
cout << "Соберись! Take a minute and the right choice (from 1 to 7): ";
cout <= 1 && answer <= 7)
var = false;
} while (var);
cout << endl;
cout << endl;
cout << "Для особо одаренных напомню полное расписание на неделю:"<< endl;
cout << endl;
cout << "ПОНЕДЕЛЬНИК." << endl;
cout << "Отходим после выходных." << endl;
cout << endl;
cout << "ВТОРНИК " << endl;
cout << "Подготовка к работе " << endl;
cout << endl;
cout << "СРЕДА " << endl;
cout << "РАБОТАЕМ! " << endl;
cout << endl;
cout << "ЧЕТВЕРГ " << endl;
cout << "Отдыхаем после работы. " << endl;
cout << endl;
cout << "ПЯТНИЦА " << endl;
cout << "Подготовка к выходным. " << endl;
cout << endl;
cout << "СУББОТА " << endl;
cout << "Генеральная репетиция выходного. " << endl;
cout << endl;
cout << "ВОСКРЕСЕНЬЕ " << endl;
cout << "ВЫХОДНОЙ!!! " << endl;
cout << endl;
return 0;
}
/* I got so */
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “rus”);
char answer = 0;
bool var = true;
cout <> answer;
switch (answer)
{
case ‘a’:
case ‘A’:
cout << "Станция метро Trinitat Nova: ";
cout << "Время в пути – 15 мин." << endl;
break;
case 'b':
case 'B':
cout << "Станция метро Casa de l’Aigua: ";
cout << "Время в пути – 19 мин." << endl;
break;
case 'c':
case 'C':
cout << "Станция метро Torre Baro Vallbona: ";
cout << "Время в пути – 25 мин." << endl;
break;
case 'd':
case 'D':
cout << "Станция метро Ciutat Meridiana: ";
cout << "Время в пути – 30 мин." << endl;
break;
case 'e':
case 'E':
cout << "Станция метро Can Cuias: ";
cout << "Время в пути – 38 мин." << endl;
break;
default: // если ни один case не сработал, default work
cout << "Станции под номером " << answer << " no! ";
cout <= ‘a’ && answer = ‘A’ && answer <= 'E'))
var = false;
} while (var);
return 0;
}
#include
using namespace std;
int main()
{
setlocale(0, “”);
int day;
for(int i =0; i<999; i ){
cout<> day;
cout<<endl;
if(day!= 1 && day!=2 && day!=3 && day!=4 && day!=5 && day!=6 && day!=7){
cout<<endl;
cout<<"Ошибка"<<endl;
cout<<endl;
}
else{
switch(day){
case 1:
cout<<"Надо помыть пол"<<endl;
cout<<endl;
break;
case 2:
cout<<"Сходить в гости к бабушке"<<endl;
cout<<endl;
break;
case 3:
cout<<"Надо встретить Тома"<<endl;
cout<<endl;
break;
case 4:
cout<<"Сходить в спортзал"<<endl;
cout<<endl;
break;
case 5:
cout<<"Позвонить кредитору"<<endl;
cout<<endl;
break;
case 6:
cout<<"Помочь папе с машиной"<<endl;
cout<<endl;
break;
case 7:
cout<<"Приготовить семейный ужин"<<endl;
cout<<endl;
break;
}
}
}
return 0;
}
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “rus”);
int answer = 0;
bool var2 = true;
bool var = true;
char aa2 = 0;
cout << "###################\n# Ваше расписание #\n###################\n\n";
do
{
var2 = true;
var = true;
do
{
cout <> answer;
switch (answer)
{
case 1:
cout << " Понеделиник. " << endl;
cout << " Планы. " << endl;
break;
case 2:
cout << " Вторник. " << endl;
cout << " Планы. " << endl;
break;
case 3:
cout << " Среда. " << endl;
cout << " Планы. " << endl;
break;
case 4:
cout << " Четверг " << endl;
cout << " Планы. " << endl;
break;
case 5:
cout << " Пятница. " << endl;
cout << " Планы. " << endl;
break;
case 6:
cout << " Суббота. " << endl;
cout << " Планы. " << endl;
break;
case 7:
cout << " Воскресенье. " << endl;
cout << " Планы. " << endl;
break;
default:
cout << "День под номером '" << answer << "' отсутствует." <=1 && answer<=7)
{
var = false;
}
} while (var);
cout <> aa2;
if (aa2 == ‘y’ || aa2 == ‘Y’)
{
var2 = true;
}
else
{
var2 = false;
}
} while (var2);
return 0;
}