-
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 ;)
-
including the error message in ur post would be helpful
change cout to std::cout and cin to std::cin
-
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:
the message is: `endl'
-
std::endl
std::cout.setf(cout.boolalpha); // HERE
-
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
-
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...
-
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.
-
added
Code:
using namespace std;
and I don't get any more errors :D thanks a lot for your help :)
-
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.