locked
Beginner Example Program Error RRS feed

Answers

  • Hi Hugo Andrés,

    I have checked your code and you need to have a change.

    #include "iostream"
    
    
    int _tmain()
    {
       int x=5;
       int y=7;
       std::cout<<"\n";
       std::cout<<x+y<<" "<<x*y;
       std::cout<<"\n";
       return 0;
    }

    Please read "main: Program Startup" and "<iostream>" .

    If you want to use "cout", add "using namespace std" or use "std::cout". 

    Such as:

    using namespace std;
    int main()
    {
      cout<<"hello";
      return 0;
    }

    or:

    int main()
    {
     std::cout<<"hello";
     return 0;
    }

    Best regards,

    Sunny


    • Edited by Anna Cc Monday, January 13, 2014 6:15 AM
    • Marked as answer by Anna Cc Friday, January 17, 2014 1:20 PM
    Monday, January 13, 2014 6:08 AM
  • Hi Hugo Andrés,

    >>why "tmain" instead of "main"?

    _tmain does not exist in C++. main does.

    _tmain is a Microsoft extension.

    main is, according to the C++ standard, the program's entry point. It has one of these two signatures:

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

    Please take a look at this.

    >>Why "std::" before "cout"?

    We need to know the std namespace.

    The ANSI/ISO C++ standard requires you to explicitly declare the namespace in the standard library. For example, when using iostream, you must specify the namespace of cout in one of the following ways:

    1. std::cout (explicitly)                  
    • 2.using std::cout (using declaration)

    • 3.using namespace std (using directive)

    Best regards,

    Sunny

    • Marked as answer by Anna Cc Friday, January 17, 2014 1:20 PM
    Tuesday, January 14, 2014 1:43 AM

All replies

  • I see that your build failed. it needs to succeed to run the app

    Ahmed is an independent consultant in Ottawa, Canada specialized in .NET, Biztalk, WCF, WPF, TFS and other Microsoft technologies.

    Blog: http://lajak.wordpress.com

    Twitter: ahmedalasaad

    • Proposed as answer by Ramya SJ Sunday, January 12, 2014 7:21 AM
    Sunday, January 12, 2014 5:14 AM
  • Hi Hugo Andrés,

    I have checked your code and you need to have a change.

    #include "iostream"
    
    
    int _tmain()
    {
       int x=5;
       int y=7;
       std::cout<<"\n";
       std::cout<<x+y<<" "<<x*y;
       std::cout<<"\n";
       return 0;
    }

    Please read "main: Program Startup" and "<iostream>" .

    If you want to use "cout", add "using namespace std" or use "std::cout". 

    Such as:

    using namespace std;
    int main()
    {
      cout<<"hello";
      return 0;
    }

    or:

    int main()
    {
     std::cout<<"hello";
     return 0;
    }

    Best regards,

    Sunny


    • Edited by Anna Cc Monday, January 13, 2014 6:15 AM
    • Marked as answer by Anna Cc Friday, January 17, 2014 1:20 PM
    Monday, January 13, 2014 6:08 AM
  • Great,

    Now, why "tmain" instead of "main"?

    Why "std::" before "cout"?

    Monday, January 13, 2014 2:29 PM
  • Hi Hugo Andrés,

    I have checked your code and you need to have a change.

    #include "iostream"
    
    
    int _tmain()
    {
       int x=5;
       int y=7;
       std::cout<<"\n";
       std::cout<<x+y<<" "<<x*y;
       std::cout<<"\n";
       return 0;
    }

    Sunny, I've tried your alternative but I get the following error:

    Monday, January 13, 2014 2:37 PM
  • Hi Hugo Andrés,

    I'm sorry that I can not see the error you provide above. In fact, I create a win32 console application and #include "stdafx.h" has exists in the .cpp file.

    Best regards,

    Sunny

    Tuesday, January 14, 2014 1:28 AM
  • Hi Hugo Andrés,

    >>why "tmain" instead of "main"?

    _tmain does not exist in C++. main does.

    _tmain is a Microsoft extension.

    main is, according to the C++ standard, the program's entry point. It has one of these two signatures:

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

    Please take a look at this.

    >>Why "std::" before "cout"?

    We need to know the std namespace.

    The ANSI/ISO C++ standard requires you to explicitly declare the namespace in the standard library. For example, when using iostream, you must specify the namespace of cout in one of the following ways:

    1. std::cout (explicitly)                  
    • 2.using std::cout (using declaration)

    • 3.using namespace std (using directive)

    Best regards,

    Sunny

    • Marked as answer by Anna Cc Friday, January 17, 2014 1:20 PM
    Tuesday, January 14, 2014 1:43 AM
  • Ok, I have included "#include "stdafx.h" " and "using namespace std;" and now it worked. Thank you.

    Now, I would like to know, eventually, why these two lines must be included.
    I ask because I am using the example code from a book called "Teach Yourself C++ in 21 Days", and the code in the book doesnt include the two lines I mentioned above. The book has been tested in Visual C++ 6. So I suppose that the two lines have to do with this being a newer version of Visual C++.
    Monday, June 1, 2015 10:36 PM