neversaint
neversaint

Reputation: 63994

Howto Construct Char Arrays

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

Upvotes: 1

Views: 497

Answers (3)

user36457
user36457

Reputation:

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;
}

Upvotes: 2

grieve
grieve

Reputation: 13508

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

Upvotes: 3

Miles
Miles

Reputation: 32468

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)

Upvotes: 5

Related Questions