0

I am just trying to familiarise myself with the basics of C++ moving from Java. I just wrote this functionality abstinent program and am coming across an errortest.cpp:15: error: expected primary-expression before ‘<<’ token and I am not sure why.

Anybody care to explain why endl is not working with constants? The code is below.

//Includes to provide functionality.
#include <iostream>

//Uses the standard namespace.
using namespace std;

//Define constants.
#define STRING "C++ is working on this machine usig the GCC/G++ compiler";

//Main function.
int main()
{
  string enteredString;

  cout << STRING << endl;
  cout << "Please enter a String:" << endl;
  cin >> enteredString;
  cout << "Your String was:" << endl;
  cout << enteredString << endl;

  return(0);
}
Jack H
  • 2,440
  • 4
  • 40
  • 63

8 Answers8

8

Your #define has a semicolon at the end. That becomes part of the macro, so the pre-processed code looks like this:

cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;

Remove the semicolon and you should be fine.


PS: It's usually a better idea to use real constants for this rather than relying on the preprocessor:

const char *STRING = "C++ is working on this machine usig the GCC/G++ compiler";
hammar
  • 138,522
  • 17
  • 304
  • 385
6

You have a ; in your preprocessor definition. Note that #DEFINE STRING x just copies the whole x-statement (including the ;) into the place where it's referenced.

Also, a preprocessor constant isn't a language constant. You should use const string STRING("C++ is working on this machine usig the GCC/G++ compiler");

KillianDS
  • 16,936
  • 4
  • 61
  • 70
  • Ah ok, that makes sense, rookie error. Oh, would you happen to know why only the first word of my string is printed? Thanks. – Jack H Jul 11 '11 at 20:50
  • 1
    @VisionIncision: `cin >> enteredString` only reads one word. To read the whole line use `getline(cin, enteredString)`. – hammar Jul 11 '11 at 20:52
2

You've got a secmi-colon at the end of your #define - this will be substituted into your code, giving.

cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;

Node
  • 3,443
  • 16
  • 18
1

Because you have a semi colon after STRING. Remove it and give it a try...

Leandro T. C. Melo
  • 3,974
  • 22
  • 22
1

Remove the ; in the STRINGS definition

littleadv
  • 20,100
  • 2
  • 36
  • 50
1

Remove ; from

#define STRING "C++ is working on this machine usig the GCC/G++ compiler"
Bruno Alano
  • 643
  • 1
  • 11
  • 21
1

Remove the ; at the end of #define STRING and try again.

yasouser
  • 5,113
  • 2
  • 27
  • 41
0

define is a preprocessor directive. This replaces everything that follows the defined macro, in this case STRING. So, remove the last semicolon (;) that puts an end-of-statement marker while being expanded in the offending line.

Community
  • 1
  • 1
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69