hi,
Wondering if it's possible to catch non-standard input & then use that for error checking/return ?
for example;
This is (a part of) something I've written, and while it does what I want, I was wondering if there's a more proper/elegant way of handling non-standard input.Code:int input; cout<<"Enter input: "; cin>>input; if (input!=int) { cout<<"Not an int"; }
Currently I've used an initialised int using -1 as a means of doing what I want (ie catching non ints).
I thought I had read somewhere that there is a way to do this, but I can't seem to find where that was & I'm starting to think maybe I dreamt it upCode://myflush1 & 2 added/used to handle non-int/standard input. void myflush1 ( std::istream &in ) { in.clear(); } void myflush2 ( std::istream &in ) { in.ignore ( numeric_limits<streamsize>::max(), '\n' ); in.clear(); } //other code int iNumoftimetype = 11; //number of time types in saTimetype[], counting from zero. int iInitialtimetype=-1, iConverttimetype=-1; //initialised to -1 in order for 0 > exit to work double dInitialtimevalue=-1, dConverttimevalue=-1; //used for display & conversion values string saTimetype[] = {"Exit Program","millisecond(s)","second(s)","minute(s)","hour(s)","day(s)","week(s)","month(s)" ,"year(s)","decade(s)","century(ies)","millennium(s)"}; //used for text display string saFunctimetype[] = { "INVALID", "MILLISECONDS","SECONDS","MINUTES","HOURS","DAYS","WEEKS","MONTHS" ,"YEARS","DECADES","CENTURYS","MILLENNIUMS" }; //used for function call string sInitialtimetype, sConverttimetype; string sFuncinitialtimetype, sFuncconverttimetype, sTime1_2_time2_func; while (1) { cout<<"Select base type & conversion type (enter value 1 or 2 or 3 etc)...\n"; for (int n=0 ;n<=iNumoftimetype;n++ ) //numbers & displays the saTimetype array { cout<<n<<":"<<saTimetype[n]<<endl; } cout<<"Enter base type: "; myflush1 ( cin>>iInitialtimetype); myflush2 ( cin ); if ((iInitialtimetype>=1)&&(iInitialtimetype<=iNumoftimetype)&&(iInitialtimetype!=0)) { cout<<"Convert to: "; myflush1 ( cin>>iConverttimetype); myflush2 ( cin ); if (iInitialtimetype == iConverttimetype) { cout<<"Conversion will be the same\n\n"; iInitialtimetype=-1; iConverttimetype=-1; continue; } if ((iConverttimetype>=1)&&(iConverttimetype<=iNumoftimetype)&&(iConverttimetype!=0)) //if reach here, exit the loop & moves onto switch. break; if ((iConverttimetype<-1)||(iConverttimetype>iNumoftimetype)) { cout<<"Invalid selection\n\n"; iConverttimetype=-1; continue; } //here, if input!=int return error & return control to loop if (((iConverttimetype<=-1)||(iConverttimetype>=iNumoftimetype))||(iConverttimetype!=0)) { cout<<"Invalid selection type. Must be a (positive) integer.\n\n"; continue; } if (iConverttimetype==0) { cout<<"Program exiting\n"; cin.get(); return 0; } break; } if ((iInitialtimetype<-1)||(iInitialtimetype>iNumoftimetype)) { cout<<"Invalid selection\n\n"; iInitialtimetype=-1; continue; } if (((iInitialtimetype<=-1)||(iInitialtimetype>=iNumoftimetype))||(iInitialtimetype!=0)) { cout<<"Invalid selection type. Must be a (positive) integer.\n\n"; continue; } if (iInitialtimetype==0) { cout<<"Program exiting\n"; cin.get(); return 0; } } //other code![]()



LinkBack URL
About LinkBacks




.