Thread: pass two string in this program...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    34

    pass two string in this program...

    i am trying to write a program that work like sprintf but i have one problem!
    when I pass two string into string_print function output isnt correct why?!
    for example
    string a="x %s %s %d";
    string_print(a,"dsad","ds",5);
    and how can i fix this warning "second parameter of 'va_start' not last named argument"

    Code:
    #include <iostream>
    #include<windows.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include<string>
    using namespace std;
    string string_print(string a,...);
    
    
    string string_print(string a,...)
    {
      string result;
      int i=0,arg_num=0,T_str=0;
      int size;
    
    
       /******calculate arg numbers */
      while(a[i++]!='\0')
        if(a[i]=='%')
          arg_num++;
      va_list listptr;
      va_start(listptr,arg_num);
      /******start puting into string*/
      for(i=0;i<arg_num;i++)
        while(a[T_str++]!='\0')
          if(a[T_str]=='%')
          {
            if(a[T_str+1]=='d')    /******  %d */
              {
                 a[T_str]= va_arg(listptr,int)+'0';
                 size=a.size();
                 a.append(" ");
                 for(i=T_str+1;i<size;i++) //move one character left  (example: ob %5 f-->ob 5 f)
                 a[i]=a[i+1];
              }
    
    
             if(a[T_str+1]=='c')
               {
                 a[T_str]= va_arg(listptr,int);
                 size=a.size();
                 a.append(" ");
                 for(i=T_str+1;i<size;i++) //move array one character left
                 a[i]=a[i+1];
               }
             if(a[T_str+1]=='s')
               {
    
    
                   int f_e=0;
                   int size_a,size_input,c_size_input;
                   string input;
                   /***put input string at the end of a and get the input size*/
                  input=va_arg(listptr,char*);
                  size_input=strlen(input.c_str());
                  a.append(input.c_str());
                  /************************************/
                  size_a=strlen(a.c_str());//get new size
                  c_size_input=size_input;//c:copy
    
    
    /***************put s equal to input size (example %s,dfg-->%sss)*******/
                 while(c_size_input>1)
                   {
                      a.append(" "); //add 1   to string size
                      c_size_input--;
                      for(i=size_a+f_e;i>T_str+1;i--)
                         a[i]=a[i-1];
                      f_e++;
                   }
    /***************put input item instead of s*******/
              size_a=strlen(a.c_str());
              c_size_input=size_input;
              int first_ch=size_a-size_input; //value of first input character
              for(int first_put=T_str+1;c_size_input>0;c_size_input--)
                 a[first_put++]=a[first_ch++];
    
    
    
    
    /***************make extra items in a NULL*******/
                c_size_input=size_input;
                for(int first_null=size_a-size_input;c_size_input>-2;c_size_input--)
                  a[first_null++]='\0';
    /***************move  one character left (example %fd d-->fd d)**********/
               for(i=T_str;i<size_a;i++)
                 a[i]=a[i+1];
    
    
               }//end of if
          }//end of while
      va_end(listptr );
      return a;
    }
    int main()
    {
      string a="x %s %c  %d";
      cout<<string_print(a,"dsad",'g',5);
    
    
    
    
        return 0;
    }//12 1
    Last edited by king_zart; 11-22-2012 at 01:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pass string array from C++ to C#
    By mosu' in forum C++ Programming
    Replies: 1
    Last Post: 08-06-2007, 06:31 AM
  2. pass string into main
    By chico1st in forum C++ Programming
    Replies: 7
    Last Post: 07-21-2007, 11:06 PM
  3. Using MC++ How do you pass a String between forms?
    By Dream Theater in forum Windows Programming
    Replies: 1
    Last Post: 04-30-2005, 06:23 AM
  4. how to pass a string as an argument?
    By waxydock in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2005, 05:40 PM
  5. can c let me pass string references??
    By adaly in forum C Programming
    Replies: 9
    Last Post: 01-09-2002, 04:17 PM