Re: Проверка данных при вводе. Нужна помощь!
От: dotidot Россия  
Дата: 24.12.06 14:45
Оценка:
Здравствуйте, maksqwe, Вы писали:
Вот вам костыль из моей далекой молодости
Вообще — он ужасен, но работает(сделан специально под виндовый cmd)
см. коментарии.
#ifndef _1251_to_866_H
#define _1251_to_866_H

#include <iostream>
#include <string>
#include <stdexcept>

// cp1251(в ней обычно MSVC исходник сохраняет)->cp866(виндовая консолька)
std::string
_r(const char* str)
{
    int i = 0;
    int y = 0;
    char z;
    std::string tmp;
    
    while (*(str + i) != '\0')
    {
        y = z = *(str + i);
        if ((y <= -17) && (y >= -64))
            z = 192 + y;
        if ((y <= -1) && (y >= -16))
            z = 240 + y;
        tmp+=z;
        i++;
    }
    return tmp;
}

struct __resurrect_cin
{
    __resurrect_cin(){}
    static void 
    resurrect()
    {
        if(!std::cin)
        {
            std::cin.clear();
            std::cin.ignore(4096,'\n');
            throw(std::runtime_error(_r("Ошибка ввода. Повторите.\n")));
        }
    }
};

const __resurrect_cin resurrect_cin;

std::istream& 
operator>>(std::istream& in, const __resurrect_cin&)
{
    __resurrect_cin::resurrect();
    return in;
}

#endif

пример использования кривого костыля:
#include "1251_to_866.h"
#include <iostream>
#include <сmath>

using namespace std;

double 
zad51(double x,double y)
{
    if(y<=0.0)
        if((-1<=x)&&(1<=x))
            return exp(x+3)/(1+fabs(x));
    return x*x*y;
}

int 
main()
{
    double d1,d2;
    while(true)
    {
        cout<<_r("Задание 51.\n Введите значения переменных:\n");        
        try
        {
            cin>>d1>>d2>>resurrect_cin;
            cout<<_r("\nРезультат: ")<<zad51(d1,d2)<<"\n";
            system("Pause");
            return 0;
        }
        catch(runtime_error& a)
        {
            cout<<a.what();
        }        
    }
    return 0;
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.