1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
using namespace std;
void check(bool isGood, string message, string fileName, string endLine);
void check(bool isGood, string message);
void getInt(int &var);
void getInt(int &var, int lower, int upper);
int main(){
ofstream output;
int n;
double x;
char ch;
bool yes;
string fileName;
ifstream input;
cout<<"Enter a name for the output file- ";
cin>>fileName;
output.open(fileName.c_str());
check(output.good(),"Failure to open the file '",fileName,"'");
input.open("input.txt");
check(input.good(),"Failure to open 'input.txt'");
input>>n;
while(input.good()){
output<<n;
input>>n;
}
cout<<"Enter an int- ";
cin>>n;
check(cin.good(),"'cin' has been disabled");
cout<<"You entered the int "<<n<<endl;
getInt(n);
cout<<"You entered "<<n<<endl;
getInt(n,2,10);
cout<<"You entered "<<n<<endl;
/*
yes=getYorN(ch);
cout<<"You entered '"<<ch<<"'\n";
if(yes)
cout<<"This means yes\n";
else
cout<<"This means no\n";
x=0;
getBool(yes);
cout<<"You entered the bool "<<yes<<endl;
getDouble(x);
cout<<"You entered the double "<<x<<endl;
*/
}
void check(bool isGood, string message, string fileName, string endLine) {
if (!isGood) {
cerr<< message << fileName << endLine << endl;
exit(1);
}
}
void check(bool isGood, string message){
check(isGood, message, '\0','\0');
}
void getInt(int &var) {
string empty;
cout << "enter an int-"<<endl;
cin >> var;
while(!cin.good()){
cin.clear();
getline(cin, empty);
cout << "You did not enter an int. Please enter an int: "<< endl;
cin>>var;
}
}
void getInt(int &var, int lower, int upper) {
getInt(var);
while (!((var >= lower) && (var <= upper))) {
getInt(var);
}
}
|