error C2143: syntax error : missing ';' before '

This is my second program that I have made and I can't figure out this error my code is:

#include "stdafx.h"
#include

int a,b,c;

int main()
{
using namespace std;

using std::cout << "Enter first number";
using std::cin >> a;

using std::cout << "Enter second number";
using std::cin >> b;

c = a + b;
using std::cout << "Sum is" << c <<std:endl;

return 0;
}




I keep getting this error on the << or >> after cin or cout

error C2143: syntax error : missing ';' before '<<'


I tried to put a semicolon after int main() and the red lines under the << and >> went away but there was a red line under the {

Comments

  • [code]
    #include "stdafx.h"
    #include

    int a,b,c;

    int main()
    {
    using namespace std;

    using std::cout << "Enter first number";
    using std::cin >> a;

    using std::cout << "Enter second number";
    using std::cin >> b;

    c = a + b;
    using std::cout << "Sum is" << c <<std:endl;

    return 0;
    }
    [/code]

    It's because you are not using the [b]using[/b] directive properly.

    using namespace std; -- This brings the entire namespace into the the current scope. Using this your code should look like:
    [code]
    #include "stdafx.h"
    #include <iostream>

    int a,b,c;

    int main()
    {
    using namespace std;

    cout << "Enter first number";
    cin >> a;

    cout << "Enter second number";
    cin >> b;

    c = a + b;
    cout << "Sum is" << c << endl;

    return 0;
    }
    [/code]

    The using std::cout; style of using will bring that specific object or function into the current context in which case your could could look like this:
    [code]
    #include "stdafx.h"
    #include <iostream>

    int a,b,c;

    int main()
    {
    using std::cout;
    using std::cin;
    using std::endl;

    cout << "Enter first number";
    cin >> a;

    cout << "Enter second number";
    cin >> b;

    c = a + b;
    cout << "Sum is" << c << endl;

    return 0;
    }
    [/code]

    The using namespace X is usually considered bad programming due to the fact that it pollutes the current scope with everything from another namespace. Then what happens when you have something named the same in both? Usually you don't get the behavior you want.

    The using X is slightly better in that it makes a specific pull in of an item from a given namespace and is limited to the scope in which it is defined, but having a bunch of usings at the top of ever function usually turns into having them once at the top of the file in the global scope which leads to the problem of "using namespace X" but in a more subtle manner.

    Probably the best way to use code from other namespaces is to be explicit about it like the following:
    [code]
    #include "stdafx.h"
    #include <iostream>

    int a,b,c;

    int main()
    {
    std::cout << "Enter first number";
    std::cin >> a;

    std::cout << "Enter second number";
    std::cin >> b;

    c = a + b;
    std::cout << "Sum is" << c << std::endl;

    return 0;
    }
    [/code]
    Here the code will do exactly as you expect regardless of the circumstances.
  • Thanks, it works now.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories