error help

This is a discussion on error help within the C++ Programming forums, part of the General Programming Boards category; hi all, I'm a beginner - got a book about C++, there's a example of a program but when I ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    4

    error help

    hi all, I'm a beginner - got a book about C++, there's a example of a program but when I type it, I get an error everywhere where "cout" is here's the code maybe you'll know what's wrong (all it says in the message is: `cout'

    Code:
    // booltest - compare variables input from the
    // keyboard and store the results off
    //into a logical variable
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    // set output formal for bool variables
    //to true and false instead
    //of 1 and 0
    cout.setf(cout.boolalpha); // HERE
    
    //initialize two arguments
    int nArg1;
    cout << "Input value 1: "; // THEN HERE etc
    cin >> nArg1;
    
    int nArg2;
    cout << "Input value 2: ";
    cin >> nArg2;
    
    bool b;
    b = nArg1 == nArg2;
    
    cout << "The statement, " << nArg1
         << " equals "        << nArg2
         << " is "            << b
         << endl;
         
          //wait until the user is ready before terminating the program
          system("PAUSE");
          return 0; 
    }

    ...I'm using Dev-C++ (came with the booK) if there's any other c++ editors that you'll recommend please let me know
    Last edited by cinek; 06-09-2007 at 10:29 AM.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    including the error message in ur post would be helpful

    change cout to std::cout and cin to std::cin

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    all it says in the message is: `cout'

    EDIT

    I included the std:: ....
    I still get an error

    Code:
    cout.setf(cout.boolalpha); // HERE
    then it shows an error:
    Code:
    << endl;
    the message is: `endl'

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    std::endl

    std::cout.setf(cout.boolalpha); // HERE

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    I don't get a error anymore on endl; but I still get it:

    Code:
    std::cout.setf(cout.boolalpha);

    errors on that line:
    `cout'
    (Each

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your problem is that everything in standard libraries is in a std namespace. To use the names you'll need to prefix every standard library name with "std::" (or use the "using" directive).

    Think, why do the issues get fixed when you prefix std:: and why do you get errors in places where you don't...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    7
    If you want to save the effort of directing every command through the standard namespace, just include

    using namespace std;

    right after your #include directives.. It is important that either you use the std:: or using namespace method, because otherwise it doesn't know where to find what you're trying to use.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    added
    Code:
    using namespace std;
    and I don't get any more errors thanks a lot for your help

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    For small beginner programs its not that big of a deal, but the better habit to get into is to use std:: in front of the standard names rather than a using directive.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21