4

I'm currently trying to work out some issues I am experiencing with this code, can't really figure out why I am getting these 2 errors. I tried to see if something was not closed, but this not does seem to be the case, can be be cause of the distance between the ": "? I'm just grasping for straws by now..

main.cpp:30:38: error: expected ‘;’ before ‘generationString’
cout << "Generation " << x << ": " generationString << endl;

main.cpp:54:40: error: expected ‘;’ before ‘generationString’
cout << "Generation " << x++ << ": " generationString << endl;

When trying to compile this code:

#include <iostream>

using namespace std;

string
initString ()
{
}

int
calculateScore (string guess, string target)
{
}

string
mutate (string mutationString)
{
}


int
main ()
{
  string targetString = "METHINKS IT IS LIKE A WEASEL";
  string generationString = initString ();
  string currentString = generationString;

  int score = calculateScore (currentString, targetString);
  int x = 0;
  cout << "Generation " << x << ": " generationString << endl;
  do
    {
      for (int i = 0; i < 100; i++)
    {
      string newCopy = generationString;
      newCopy = mutate (newCopy);
      int copyScore = calculateScore (newCopy, targetString);

      if (copyScore > score)
        {
      currentString = newCopy;
      score = copyScore;
      if (copyScore == targetString.length ())
    {
      break;
    }

    }
}
  generationString = currentString;

     }
  while (score < targetString.length ());
  cout << "Generation " << x++ << ": " generationString << endl;
  return 0;
  }
Blaze
  • 16,736
  • 2
  • 25
  • 44
MSL
  • 65
  • 6

2 Answers2

3

You're missing a <<.

cout << "Generation " << x << ": " generationString << endl;

Should be

cout << "Generation " << x << ": " << generationString << endl;

Likewise for

cout << "Generation " << x++ << ": " generationString << endl;

That should be

cout << "Generation " << x++ << ": " << generationString << endl;
Blaze
  • 16,736
  • 2
  • 25
  • 44
3

You are probably missing a << in the line

cout << "Generation " << x << ": " generationString << endl;

Here you have

": " generationString

which should be

": " << generationString

C++ can concatenate literal strings, but it cannot concatenate literal string with anything else (like std::strings). So for instance this would work

cout << "Generation " << x << ": " "METHINKS IT IS LIKE A WEASEL" << endl;
Paul Floyd
  • 5,530
  • 5
  • 29
  • 43