MiniPrint – bugs fixed version

Posted: February 9, 2012 in OOP344
#include <cstdio>
#include <cstdarg>
using namespace std;

void miniprint(const char *format, ...){
  va_list ap;
  va_start(ap, format);
  while(*format){
    if(*format == '%'){
      format++;
      switch(*format){
        case 'd':
          printf("%d", va_arg(ap,int));
          break;
        case 'f':
          printf("%0.2f", va_arg(ap,double));
          break;
        case 's':
          printf("%s", va_arg(ap,char*));
          break;
        case 'x':
          printf("%x", va_arg(ap,int));
          break;
        case 'X':
          printf("%X", va_arg(ap,int));
          break;
        case 'p':
          printf("%p", va_arg(ap,void*));
          break;
        default:
          putchar(*format);
      }
      format++;
    }
    else{
      putchar(*format++);
    }
  }
  va_end(ap);
}

int main(){
  miniprint("hello I am %s and I make %f Dollars a year\n", "Homer", 12345.67);
  return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s