Like Tree1Likes
  • 1 Post By whiteflags

pass two string in this program...

This is a discussion on pass two string in this program... within the C++ Programming forums, part of the General Programming Boards category; i am trying to write a program that work like sprintf but i have one problem! when I pass two ...

  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.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    i think i found the problem. when i use 2 string the line 56 doesnt work
    and still i cant understand why!?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    If you are going to be using C++, then it is better to do is properly. Here is an example using the latest standard:

    Code:
    #include <iostream>
    #include <initializer_list>
    
    void foo(std::initializer_list<int> test)
    {
    	for (int n : test)
    		std::cout << n << std::endl;
    }
    
    int main()
    {
    	foo({1, 2, 3, 4, 5, 6, 7, 8, 9 });
    }
    What makes this possible is initializer lists, allowing you to pass an unrestricted number of argumments.
    The for loop is called a range-based for loop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    ty Elysia however it wasnt the answer i need!
    i change the code and now it seems to work
    the problem was in line 81 to 85 i thought making last item in string ' \0' could resize the string... !
    here is mynew code and it still it need some changes like using initializer_list...
    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=-1,arg_num=0,T_str=-1;
      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 */
              {
                int number=va_arg(listptr,int);
                if(number<10)
                 {
                   a[T_str]= number+'0';
                   size=a.size();//because the size change after put string
                   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];
                   a.resize(size-1);
                 }
                 else  //for number between 10 and 100 
                 {
    
    
                   int second_digit=number%10;
                   int first_digit=number/10;
                   a[T_str]=first_digit+'0';
                   a[T_str+1]=second_digit+'0';
                 }
              }
    
    
             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 given string at the end of a and get the input size*/
                  input=va_arg(listptr,char*);
                  size_input=strlen(input.c_str());
                  if(input[0]=='\0')// example "%s%s%d" ,"","dsd",3
                  {
    
    
                      int start=T_str;
                      int end=a.size();
                      while(start<end)
                      {
                        a[sh]=a[sh+2];
                        start++;
                      }
                      a.resize(start-2);
                      T_str=T_str-2;;
                  }
                  else
                   {
                      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*******/
                   int first_null=size_a-size_input;
                   char last_one_back=a[first_null-1];
                   a.resize (first_null-1);
                   a[first_null-1]=last_one_back;
     
    
    
    /***************move  one character left example %fd d-->fd d**********/
                  for(i=T_str;i<size_a;i++)
                    a[i]=a[i+1];
                 }//end of else
               }//end of if
          }//end of while
      va_end(listptr );
    
    
      return a;
    
    
    }
    int main()
    {
      string a="%s%d";
      string b=string_print(a,"dsd",32);
      cout<<b<<"X";
    
    
        return 0;
    }//12 1
    Last edited by king_zart; 11-27-2012 at 03:07 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by king_zart View Post
    ty Elysia however it wasnt the answer i need!
    Perhaps, but it makes the code much easier to read and less error prone. And safer. Safer is always good in today's world.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    I highly recommend you don't try to code a function as complex as sprintf() in one go. It's too hard, and you should be more mistrusting of your own code. You should be calling other functions to handle each kind of conversion you intend to support. That way you can individually test them before you throw them in a huge sprintf() function. A function that finds the next conversion in the format string might be helpful too, depending on how closely you want to mirror the standard. I think with sufficient planning you can do an awful lot with a loop containing a switch case statement for a whole sprintf() function. While it may well be as complicated as your first crack at it, it does not need to be written this messily.
    iMalc likes this.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Quote Originally Posted by Elysia View Post
    If you are going to be using C++, then it is better to do is properly. Here is an example using the latest standard:

    Code:
    #include <iostream>
    #include <initializer_list>
    
    void foo(std::initializer_list<int> test)
    {
        for (int n : test)
            std::cout << n << std::endl;
    }
    
    int main()
    {
        foo({1, 2, 3, 4, 5, 6, 7, 8, 9 });
    }
    What makes this possible is initializer lists, allowing you to pass an unrestricted number of argumments.
    The for loop is called a range-based for loop.
    is there any way to use intializer list without {}?
    for example use this
    foo(1, 2, 3, 4, 5, 6, 7, 8, 9 );
    instead of
    foo({1, 2, 3, 4, 5, 6, 7, 8, 9 });
    Last edited by king_zart; 12-11-2012 at 05:28 AM.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,545
    if you need to handle multiple types, instead of just strings, as you will with a sprintf()-type function, you might need to do something like this:

    Code:
    template<typename... Args>
    void SomeFunction(const std::string& format, Args... args)
    {
      // since we can't access the args by index, we must write
      // some recursive code here to descend through the list
    }
    this is a very advanced technique, and not for the faint of heart, but I do something like this in one of my utility functions, and now I basically can't live without it.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by king_zart View Post
    is there any way to use intializer list without {}?
    for example use this
    Code:
     foo(1, 2, 3, 4, 5, 6, 7, 8, 9 );

    instead of
    Code:
     foo({1, 2, 3, 4, 5, 6, 7, 8, 9 });
    You can do it using variadic templates as shown by Elkvis. But why do you reject the bracer syntax so much?
    Last edited by Elysia; 12-11-2012 at 03:11 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,545
    Quote Originally Posted by Elysia View Post
    But who do you reject the bracer syntax so much?
    I'm guessing you meant "why."

    I don't reject it. I just don't think it's the right tool for the job in a sprintf() function, particularly when the OP clearly shows it being used for string and numeric types.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by Elkvis View Post
    I'm guessing you meant "why."

    I don't reject it. I just don't think it's the right tool for the job in a sprintf() function, particularly when the OP clearly shows it being used for string and numeric types.
    I mean why
    Also, I was aiming for the OP, not you. You clearly know when initializer list is good for the job, and when it isn't. But the OP seem to just reject it purely out of a syntactical view.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    My guess is that this comes down to ignorance about C++11 features and that any syntax preferences being assigned to the OP are wrong. You can bring a horse to water, but you can't make him drink.
    Last edited by whiteflags; 12-11-2012 at 09:22 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, 07:31 AM
  2. pass string into main
    By chico1st in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2007, 12:06 AM
  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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21