Thursday, May 23, 2013

Variadic Example #3

/*  Here's one more example on using variadic functions that I was playing with.  This on scans an entered string and looks for '!', reporting if it's found with "DETECTED", then applies a few different treatments to the string depending on what int parameters are sent down.  Mostly I did this to see if I could, with the idea being: could it be possible to pseudo-replicate what happens with a "printf" function?
 */
#include <iostream>
#include <stdarg.h>
using namespace std;

void newp(char* dat,...);

int main(){
    char rr[99];
    int ii=999;
    cout<<"Enter a string:";
    cin >> rr;
    newp(rr,1,2,1,3,1);
    cin >> ii;
   
}
void newp(char* dat,...){
     char gist;
     va_list o;
     int i=0,j=strlen(dat),k=0;
     for(k;k<j;k++){
         if(dat[k]=='!')cout<<"DETECTED!"<<endl;
     }
     va_start(o,dat);
     while(gist=va_arg(o,int)){
         switch(gist){
             case 1:
                 cout << dat << dat <<endl;
                 break;
             case 2:
                 cout << endl << "WORD!" << endl;
                 break;
             case 3:
                 cout.setf(ios::right);
                 for(i=61;i<85;i++){
                     cout.width(i);
                     cout<<i<<")"<<dat;
                 }
                 cout<<endl;
                 break;
             default:
                 cout<<"Undefined parameter"<<endl;
                 break;
         }
     }
}

No comments:

Post a Comment