Tech Off Thread

9 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

C++.NET Express and Learning C++ Problem

Back to Forum: Tech Off
  • User profile image
    darklotus

    Disclaimer: I am a total newbie, please go easy on this question.

    I recently started teaching myself C++. I started with a book and a linux shell with g++ as a c++ compiler. I had no problems. I recently got a copy of Visual C++ Express Edition Beta 1 and decided to write some of my code with the shiny new editor. However, when it comes to compiling it cant find cout and cin and endl. I can only assume these are standard C++ functions or whatever they are called. Below is part of the error file C++.NET Express generated. Any help would be greatly appreciated. Do cout and cin and stuff exist in  .NET 2005?
    -dL

    [BuildLog.htm]
    Compiling...
    hello.cpp
    c:\<shortened>\hello.cpp(23) : error C2065: 'cout' : undeclared identifier
    c:\<shortened>\hello.cpp(24) : error C2065: 'cin' : undeclared identifier
    c:\<shortened>\hello.cpp(47) : error C2065: 'endl' : undeclared identifier


  • User profile image
    JustinSyc

    try add

    #include <iostream>

    using namespace std;

    before your code.

  • User profile image
    darklotus

    I tried that last night as well, i got the same errors.

    [CODE]
    #include <iostream>
    #include "stdafx.h"
    #include "math.h"
    #include "time.h"
    #include "stdlib.h"

    int rand_0toN1(int n);
    void draw_a_card();
    int select_next_available(int n);

    char *suits[4] = {"hearts","diamonds","spades","clubs"};
    char *ranks[13] = {"ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"};

    int card_drawn[52];
    int cards_remaining = 52;

    int main()
    {
        int n, i;
       
        while(1)
        {
            cout << "Enter number of cards to draw (0 to exit): ";
            cin >> n;

            if(n == 0)
                break;

            for(i = 1; i <= n; i++)
                draw_a_card();
        }
        return 0;
    }

    void draw_a_card()
    {
        int r;
        int s;
        int n, card;

        n = rand_0toN1(cards_remaining--);
        card = select_next_available(n);

        r = card % 13;
        s = card / 13;

        cout << ranks[r] << " of " << suits[s] << endl;
    }

    int select_next_available(int n)
    {
        int i = 0;
       
        while(card_drawn[i])
            i++;

        while(n-- > 0)
        {
            i++;
            while(card_drawn[i])
                i++;
        }

        card_drawn[i] = true;
        return i;
    }

    int rand_0toN1(int n)
    {
        return rand() % n;
    }
    [/CODE]

    [ERROR]

    Build Log
      
    Build started: Project: Dealer, Configuration: Debug|Win32
    Command Lines
      
    Creating temporary file "c:\Documents and Settings\Dark Lotus\My Documents\Visual Studio\Projects\NoFear\Dealer\Debug\RSP00001D70327220.rsp" with contents
    [
    /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Gm /EHsc /RTC1 /MTd /Yu"stdafx.h" /Fp"Debug/Dealer.pch" /Fo"Debug/" /Fd"Debug/vc80.pdb" /W3 /c /Wp64 /ZI /TP

    ".\Dealer.cpp"
    ]
    Creating command line "cl.exe @"c:\Documents and Settings\Dark Lotus\My Documents\Visual Studio\Projects\NoFear\Dealer\Debug\RSP00001D70327220.rsp" /nologo"
    Output Window
      
    Compiling...
    Dealer.cpp
    c:\Documents and Settings\Dark Lotus\My Documents\Visual Studio\Projects\NoFear\Dealer\Dealer.cpp(23) : error C2065: 'cout' : undeclared identifier
    c:\Documents and Settings\Dark Lotus\My Documents\Visual Studio\Projects\NoFear\Dealer\Dealer.cpp(24) : error C2065: 'cin' : undeclared identifier
    c:\Documents and Settings\Dark Lotus\My Documents\Visual Studio\Projects\NoFear\Dealer\Dealer.cpp(47) : error C2065: 'endl' : undeclared identifier
    Results
      
    Build log was saved at "file://c:\Documents and Settings\Dark Lotus\My Documents\Visual Studio\Projects\NoFear\Dealer\Debug\BuildLog.htm"
    Dealer - 3 error(s), 0 warning(s)


    [/ERROR]

  • User profile image
    Manip

    #include <iostream>
    #include "stdafx.h"
    #include "math.h"
    #include "time.h"
    #include "stdlib.h"

    becomes:

    #include <iostream>
    #include "stdafx.h"
    #include "math.h"
    #include "time.h"
    #include "stdlib.h"

    using namespace std;



    Or add 'std.' before count, cin etc - std.cout

  • User profile image
    darklotus

     I thought i tried that too, but i will definatly try it again.
    Thanks!
    -dL

  • User profile image
    Mike Dimmick

    Your book may predate the C++ standard. In standard C++, using the <iostream> header, cout, cin and endl are members of the std namespace.

    The simplest way to solve the errors, but not recommended practice, is simply to add the line:

    using namespace std;

    at file scope before the declaration of any routines.

    This brings everything in the std namespace into the global namespace, so you don't have a lot of control. Alternatively, you can add the three lines:

    using std::cout;
    using std::cin;
    using std::endl;

    or qualify each use of the objects with std:: (i.e. replace 'cout' with 'std::cout').

  • User profile image
    MarkusBB

    you have to do the #include "stdafx.h" first. every line before the stdafx include is discarded by the compiler. actually you can write even "slkajsdf9iä###w9ßp" before the precompiled header include :=)

  • User profile image
    darklotus

    Well i'll be darned. I cant believe that fixed it. I moved the #include <iostream> below the #include "stdafx.h" and it worked!!!

    Thankx!!!

    I will continue to use this IDE as my tester....

    Thanks Again!!!

    -dL

  • User profile image
    Sven Groot

    I would recommend avoiding pre-compiled headers for now. They are not essential to the language, and using them at this point may cloud your understanding of the language by focussing to much on a mechanism that is supplied purely by the tool you're working with.

    Pre-compiled headers are meant only to increase compile times. If your application includes a certain set of big headers that do not change (often), like <windows.h> it can take the compiler a lot of time if it has to parse those headers for each and every source file it compiles. Instead, the compiler generates a pre-compiled header file, which basically works by compiling the designated header file (typically called stdafx.h), and storing the compiler state after it finishes. Then, when compiling another source file, it will look for the pre-compiled header directive (#include "stdafx.h"), restore the state, and continue compiling from there. Obviously, putting stuff before the pre-compiled header directive would change the initial state before starting to compile stdafx.h, and therefore also change the resulting state, making the pre-compiled state useless. So you cannot put anything before it, because the restored state doesn't include that information.

    If you want to create a console application that doesn't use pre-compiled header files you should choose to create an empty project when creating a new console application project. That way there will be no stdafx.h, and you don't need to worry about what it does.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.