tags:

views:

178

answers:

3

Dear all,

I have the following code that tries to enumerate strings.

#include <string>
#include <iostream>

using namespace std;

string base = "000";
char values[] = {'0', '1', '2', '3' }; // Error Here

for (int i = 0; i < base.length(); ++i)
{
   for (int j = 0; j < countof(values); ++j)
   {
      if (base[i] != values[j])
      {
          string copy = base;
          copy[i] = values[j];
          cout << copy << endl;

          for (int k = i+1; k < base.length(); ++k)
          {
              for (int l = 0; l < countof(values); ++l)
              {
                   if (copy[k] != values[l])
                   {
                       string copy2 = copy;
                       copy[k] = values[l];
                       cout << copy2 << endl;
                   }
              }
          }
      }
   }
}

But how come upon compilation it gave error:

test.cc:9: error: expected unqualified-id before 'for'
test.cc:9: error: expected constructor, destructor, or type conversion before '<' token
test.cc:9: error: expected unqualified-id before '++' token
+5  A: 

The error is actually in the following line, at the for loop: your code needs to be contained in a function of some sort, most likely int main(void)

Miles
+3  A: 

You are missing a main.

Try:

#include <string>
#include <iostream>

using namespace std;

string base = "000";
char values[] = {'0', '1', '2', '3' }; // Error Here

int main() // Added
{ // Added

   for (int i = 0; i < base.length(); ++i)
   {
      for (int j = 0; j < countof(values); ++j)
      {
         if (base[i] != values[j])
         {
             string copy = base;
             copy[i] = values[j];
             cout << copy << endl;

             for (int k = i+1; k < base.length(); ++k)
             {
                 for (int l = 0; l < countof(values); ++l)
                 {
                      if (copy[k] != values[l])
                      {
                          string copy2 = copy;
                          copy[k] = values[l];
                          cout << copy2 << endl;
                      }
                 }
             }
         }
      }
   }

   return 0; // Added
} // Added
grieve
+2  A: 

I see 2 main problems right off the bat.

1) You have no main() and no return code for it, rightfully so.

2) countof() does not exist, you are probably looking for sizeof().

#include <string>
#include <iostream>
#define countof( array ) ( sizeof( array )/sizeof( array[0] ) )

using namespace std;

int main(int argc, char *argv[]) {

string base = "000";
char values[] = {'0', '1', '2', '3' }; // Error Here

    for (int i = 0; i < base.length(); ++i)
    {
       for (int j = 0; j < countof(values); ++j)
       {
       if (base[i] != values[j])
       {
        string copy = base;
        copy[i] = values[j];
        cout << copy << endl;

        for (int k = i+1; k < base.length(); ++k)
        {
         for (int l = 0; l < countof(values); ++l)
         {
           if (copy[k] != values[l])
           {
            string copy2 = copy;
            copy[k] = values[l];
            cout << copy2 << endl;
           }
         }
        }
       }
       }
    } 
return 0;
}
John T
Maybe he is using this: http://blogs.msdn.com/the1/archive/2004/05/07/128242.aspx
grieve
Also sizeof would give the wrong answer there. You would want "sizeof(values)/sizeof(values[0])" minimally.
grieve
Ah i didn't even notice he was checking size of the array, I'll add in the macro.
John T