Permutations of chars in string

This is a discussion on Permutations of chars in string within the C++ Programming forums, part of the General Programming Boards category; Hello I found this code on the internet Code: #include <iostream> #include <string> using namespace std; int main() { std::string ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    61

    Permutations of chars in string

    Hello

    I found this code on the internet
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	std::string default = "12345";
    
    	int perm=1, digits=default.size();
    	for (int i=1;i<=digits;perm*=i++);
    	for (int a=0;a<perm;a++)
    	{
    		std::string avail=default;
    		for (int b=digits,div=perm;b>0; b--)
    		{
    			div/=b;
    			int index = (a/div)%b;
    			printf("%c", avail[index] );
    			//avail[index]=avail[b-1]; // fast but not lexigraphic order
    			avail.erase(index,1) ; // lexigraphic correct order
    		}
    		printf("\n");
    	}
    	printf("permutations:%d\n",perm);
    	return 0;
    }
    But I am getting an error:

    Code:
    perm.cpp: In function ‘int main()’:
    perm.cpp:8: error: expected unqualified-id before ‘default’
    perm.cpp:10: error: expected primary-expression before ‘default’
    perm.cpp:10: error: expected ‘,’ or ‘;’ before ‘default’
    perm.cpp:14: error: expected primary-expression before ‘default’
    perm.cpp:14: error: expected ‘,’ or ‘;’ before ‘default’
    make[1]: *** [perm] Error 1
    make: *** [run] Error 2
    What is missing? Thanks a lot

    Serge

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Posts
    2,597
    This is exactly why you should try to write code instead of ^C + ^V.
    'default' is a reserved word.
    && Try not to mix C and C++ io.
    (btw...where is stdio.h ?)
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    1
    Obviously this is C++ code..
    Try to use C++ compiler. I copied ur code to my new project, it passed.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    Quote Originally Posted by M1cKeY
    Obviously this is C++ code..
    Try to use C++ compiler. I copied ur code to my new project, it passed.
    Here are the error messages from the Comeau online C++ compiler:
    Code:
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 8: error: expected an identifier
      	std::string default = "12345";
      	            ^
    
    "ComeauTest.c", line 10: error: expected an expression
      	int perm=1, digits=default.size();
      	                   ^
    
    "ComeauTest.c", line 14: error: expected an expression
      		std::string avail=default;
      		                  ^
    
    3 errors detected in the compilation of "ComeauTest.c".
    Here are the error messages from g++ 4.4.5:
    Code:
    test.cpp: In function ‘int main()’:
    test.cpp:8: error: expected unqualified-id before ‘default’
    test.cpp:10: error: expected primary-expression before ‘default’
    test.cpp:10: error: expected ‘,’ or ‘;’ before ‘default’
    test.cpp:14: error: expected primary-expression before ‘default’
    test.cpp:14: error: expected ‘,’ or ‘;’ before ‘default’
    test.cpp:19: error: ‘printf’ was not declared in this scope
    test.cpp:23: error: ‘printf’ was not declared in this scope
    test.cpp:25: error: ‘printf’ was not declared in this scope
    So yes, it is intended to be C++ code, but it isn't valid C++. Your compiler might allow the printf despite the missing #include <cstdio> because <cstdio> could have been included via <iostream>, but that does not mean that failing to #include <cstdio> is okay, as the error messages for g++ show. The use of default as a variable name is clearly an error, and allowing it could well be considered a bug with your compiler (or a non-standard "feature").
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Permutations
    By mrkrinkle in forum C Programming
    Replies: 2
    Last Post: 03-10-2011, 05:43 PM
  2. casting a string of chars as a string of ints
    By otz111 in forum C Programming
    Replies: 5
    Last Post: 09-25-2009, 05:53 PM
  3. Take out chars in string
    By MKirstensen in forum C Programming
    Replies: 13
    Last Post: 07-05-2008, 11:16 PM
  4. permutations of a string
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 05-23-2006, 08:52 AM
  5. String permutations help
    By nickk in forum C Programming
    Replies: 4
    Last Post: 05-15-2004, 01:55 PM

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