Jessica Animationz
Jessica Animationz

Reputation: 1

C++ Expected Primary Expression before '<<' Endl

I keep getting an expected primary-expression before '<<' token compile error message when I try to compile the following code. I'm pretty sure it has something to do with endl; because when I remove '<< endl; ' from each part of the code it works fine

#include <cstdlib>
#include <iostream>

using namespace std;
int num1 = 0;

int funcNum1()
{
    cout << num1; << endl;
    int num1 = 2;
    cout << num1; << endl;
    return 0;
}

int main(int argc, char *argv[])
{
    cout << num1; << end1;
    int num1 = 1;
    funcNum1();
    cout << num1; << end1;
    system("PAUSE");
    return 0;
}

Upvotes: 0

Views: 769

Answers (3)

Swapnil
Swapnil

Reputation: 1568

You have made mistake in line cout << num1; << end1; change it to

cout << num1 << endl;

Chance this at both the places in your code.

Upvotes: 1

Nactus
Nactus

Reputation: 712

In main() your syntax is off:

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

    cout << num1 << endl; // watch the semicolons, use "endl" not "end1"
    int num1 = 1;
    funcNum1();
    cout << num1 << endl; // watch the semicolons, use "endl" not "end1"
    system("PAUSE"); // system needs #include <stdlib.h>
    return 0;
}

You need the "stdlib.h" which is embedded in the "cstdlib" header to access the system() method: take a look at the documentation

Upvotes: 0

golobitch
golobitch

Reputation: 1334

As you can see from this code, after the num1 you have semicolon. Remove this semicolon (you have also this problem in your function). Also, you have number 1 instead of l in endl. Fix this two error and it should work.

int main(int argc, char *argv[])
{
    cout << num1; << end1;
    int num1 = 1;
    funcNum1();
    cout << num1; << end1;
    system("PAUSE");
    return 0;
}

Upvotes: 0

Related Questions