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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// * Asking for responses
#include <sstream> // ios; is
#include <iostream> // cin, cout
#include <fstream> // basic_ifstream ifstream, ofstream,fstream,filebuf
using namespace std;
int dst= 1; // destination for the log
ofstream DsF; // ofstream addFile(outName);
string dspName="E:/Displays.txt";
ostringstream OuB;
// Creates a file with log of activity *******************************************************
void Dsp( int dest, // 0-none, 1-cout, 2-cout and file, 3-just file
ofstream & DsF,
ostringstream & bufi,
char bott='\n') {
string me ("Dsp: ");
if (dest != 0) {
if(dest < 3) cout << me << bufi.str() ; // << bott; 1,2 van a cout
if(dest != 1) DsF << me << bufi.str() ; // << bott; 2,3 van a Archivo
bufi.str(""); bufi.clear();
}
}
// Receive a group of strings with "n" posible responces i.e
// first of all valiates those. you can skip the validation
// the procedure returns a number, wich is the position of the response (0 to n-1, but also the response)
int getOptInt (int numRspons, string strRspons[], string& str_rE) {
string me="getOptInt: "; // identifier for the routine
int j, large=0, fin=0, icnt, irsp = numRspons;
string guess, str_ax;
unsigned char chr_rE;
// validating the responses in the strRspons array.
if ( irsp <= 0 ) { // at lease one response
OuB<< me << "Parameter (num of responses) invalid-"<< irsp <<"<-"<<endl; Dsp(dst, DsF, OuB);
exit (21);
}
for( j=0; j < irsp; j++ ) { // rescue responses
if( ( icnt = strRspons[j].size() ) == 0 ) { // look for the real number of responses
irsp = j;
break;
}
else {
if( large < (icnt = (strRspons[j].size())-1) ) large = strRspons[j].size() ; // maxsize of response
// each character in strRspons, should by aplphanum
for ( ; icnt >= 0; icnt--) {
str_ax = strRspons[j].substr(icnt,1);
chr_rE = (unsigned int) str_ax[0];
if( (chr_rE < 48 || chr_rE > 122 ) || // non printable verify the ascii chart
(chr_rE < 65 && chr_rE > 57 ) || // is not a number
(chr_rE < 97 && chr_rE > 90 ) ) // is not a letter
{
OuB<< me << "Invalid content - Position:"<< j<<", cont->"<< str_ax<<"<-"<<endl; Dsp(dst, DsF, OuB);
exit (21);
}
}
// Maybe its necesary to validate no repetead answers. ...
}
}
// Any way you make simplier
OuB<< me <<" Please, select you response from the list: \n";
for( j = 0; j < irsp ; j++) {
OuB<< " "<<strRspons[j] <<","; // << strRspons[j].size() ;
}
OuB<< endl; Dsp(dst, DsF, OuB);
OuB<< me << " R_"; Dsp(dst, DsF, OuB);
icnt = 0;
while (fin != 1) { // just trying a way of response
guess.clear();
do { // chr by char
// ***********************************************
chr_rE = cin.get();
if (chr_rE != '\n' ) guess += chr_rE;
} while ( chr_rE != '\n');
// ***********************************************
if( guess.size() == 0) {
OuB<< me <<"Error: min length is 1 char"<<endl; Dsp(dst, DsF, OuB);
OuB<<"Response R_"; Dsp(dst, DsF, OuB);
guess.clear();
}
else
if( guess.size() > large)
{
OuB<< me <<"Error: max lenth response is "<<large<<endl; Dsp(dst, DsF, OuB);
OuB<<"Response R_"; Dsp(dst, DsF, OuB);
guess.clear();
}
else
{
for( j=0; j<irsp; j++ ) {
if( strRspons[j] == guess ) {
icnt = j + fin++;
str_rE = strRspons[j];
break;
}
}
if ( fin == 0) {
OuB<< me <<"Option is not a valid selection: "<<guess<<endl; Dsp(dst, DsF, OuB);
OuB<<"Response R_"; Dsp(dst, DsF, OuB);
guess.clear();
}
}
}
OuB <<endl; Dsp(dst, DsF, OuB);
return icnt;
}
int main() {
string me="main: ";
string str_rE;
int numRspons = 15, iSel;
string strRspons[10] {"A","B","C","D","F","G","None","All","DontKnow"};
OuB<< me <<"Selection: "<<(iSel= getOptInt (numRspons, strRspons, str_rE))<<", Option: "<<str_rE<<endl;
Dsp(dst, DsF, OuB);
system("pause");
return 0;
}
|