Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 23.04.2012, 23:22   #1
svatorus
 
Регистрация: 23.04.2012
Сообщений: 8
По умолчанию Защита от дурака. Как правильно сделать?

Суть в том, что если вместо цифр при вводе А1, А2, В1, В2 вводить буквы, то прога игнорит дальнейшую сортировку данных и выводит все подряд . Как организовать защиту от подобного ввода? Препод по любому прикопается.

Собственно место проблемы:
void time(void)
{
cout<<"please, enter hour of A\n";
cin>>a1;
cout<<"Please, enter minute of A\n";
cin>>a2;
cout<<"please, enter hour of B\n";
cin>>b1;
cout<<"Please, enter minute of B\n";
cin>>b2;
cout<<"\n";
cout<<"\n";
}


Вся прога:

#include "stdafx.h"
#include "iostream"
#include <conio.h>
#include <iomanip>
#include <stdio.h>
using namespace std;

int a1,b1,a2,b2,i,t1,t2,t3,p,f=0,f1;
char gorod[20];
struct qwerty
{
int yesno;
char vector[20];
int number;
int time_c;
int time_m;
int time;
};
void city(void);
void time(void);

int main()
{

qwerty rasp[20]={
{1,"Kiev",666,10,15,6},
{1,"Tobolsk",79,14,56,48},
{1,"Volgograd",14,17,15,3},
{0,"Tumen",174,12,36,79},
{1,"Kiev",312,21,24,46},
{0,"Cheboksary",147,22,47,75},
{0,"Krasnodar",135,10,45,6},
{1,"Tobolsk",798,1,15,89},
{1,"Tumen",789,7,45,47},
{0,"Cheboksary",47,15,48,79},
{0,"Tumen",96,19,14,69},
{0,"Cheboksary",5,13,15,52},
{1,"Tumen",144,16,2,4},
{1,"Kiev",999,2,58,47},
{1,"Volgograd",89,3,17,19},
{0,"Tobolsk",4,14,32,5},
{0,"Krasnodar",458,23,31,74},
{1,"Kiev",457,6,58,46},
{1,"Tumen",969,7,14,47},
{1,"Cheboksary",141,3,43,26}};

city();
time();
if (((a1)>23)||((b1)>23))
{
cout<<"Incorrectly time, please enter again\n";
cout<<"\n";
time();
}
if (((a2)>59)||((b2)>59))
{
cout<<"Incorrectly time, please enter again\n";
cout<<"\n";
time();
}

t2=a1*60+a2;
t3=b1*60+b2;
for (i=0;i<=19;i++)
{
f1=1;
t1=rasp[i].time_c*60+rasp[i].time_m;
if ((p>=t2)&(p<=t3))
{
if (strcmp(gorod, rasp[i].vector)==0)
{
f=f+1;
if (rasp[i].yesno==1)
{
cout<<"Availability\n";
cout<<"Yes\n";
cout<<"Direction of train\n";
cout<<rasp[i].vector;
cout<<"\n";
cout<<"train number\n";
cout<<rasp[i].number;
cout<<"\n";
cout<<"departure time\n";
cout<<rasp[i].time_c;
cout<<":";
cout<<rasp[i].time_m;
cout<<"\n";
cout<<"travel time\n";
cout<<rasp[i].time;
cout<<"\n";
cout<<"\n";
cout<<"\n";
}
else
{
cout<<"There are no seats on the train number";
cout<<" ";
cout<<rasp[i].number;
cout<<"\n";
cout<<"\n";
}
}
}
}
if (f==0)
cout<<"Unfortunately, at a specified time no trains heading in this direction\n";
cout<<"\n";
cout<<"\n";

return 0;
}
void city(void)
{

cout<<"Please select a destination city\n";
cout<<"1.Kiev 2.Tobolsk 3.Volgograd 4.Cheboksary 5.Tumen 6.Krasnodar\n";
cin>>gorod;
}
void time(void)
{
cout<<"please, enter hour of A\n";
cin>>a1;
cout<<"Please, enter minute of A\n";
cin>>a2;
cout<<"please, enter hour of B\n";
cin>>b1;
cout<<"Please, enter minute of B\n";
cin>>b2;
cout<<"\n";
cout<<"\n";
}
svatorus вне форума Ответить с цитированием
Старый 24.04.2012, 00:36   #2
FelixXXI
Пользователь
 
Регистрация: 29.03.2010
Сообщений: 48
По умолчанию

Я бы сделал так:
Код:
	cout<<"please, enter hour of A\n";
	int A;
	while(!(cin>>A))
	{
		cin.clear();
		while(cin.get()!='\n')
			continue;
		cout<<"\aInput error. ";
	}
FelixXXI вне форума Ответить с цитированием
Старый 24.04.2012, 08:05   #3
svatorus
 
Регистрация: 23.04.2012
Сообщений: 8
По умолчанию

Благодарю! Дико выручил.
svatorus вне форума Ответить с цитированием
Старый 24.04.2012, 11:34   #4
Blade
Software Engineer
Участник клуба
 
Аватар для Blade
 
Регистрация: 07.04.2007
Сообщений: 1,618
По умолчанию

Цитата:
Сообщение от svatorus Посмотреть сообщение
Защита от дурака. Как правильно сделать?
Код:
...

int main()
{
  ...

  std::cout << "Вы дурак?" << std::endl;

  std::string result;
  std::cin >> result;

  if (result != "нет")
  {
    return 1;
  }

  ...
}
Мужество есть лишь у тех, кто ощутил сердцем страх, кто смотрит в пропасть, но смотрит с гордостью в глазах. (с) Ария
Blade вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Не понятно по каким причинам не работает защита от дурака axell24 Помощь студентам 4 29.10.2011 08:18
защита от дурака phasha Помощь студентам 5 03.10.2011 11:45
Защита от дурака Ericnex Помощь студентам 2 13.04.2011 18:35
Как правильно сделать include? Delphinchik PHP 5 13.10.2010 17:40