error: 'cout' was not declared in this scope

I am trying to use a program to calculate BMI with modules. I have started with the following code (could be very wrong but I am a beginner). Can someone help me understand why I am getting an error of error: 'cout' was not declared in this scope?

#include <iostream>
double height, weight;
double bmiCal (double height, double weight);

void BMIcalc()


{
cout << "This program will calculate BMI" << endl;
std::cout << "This program will calculate BMI\n";

cout belongs to the std namespace, so one has to communicate that to the compiler. I put \n rather than endl because that can be a little inefficient - it flushes the buffer every time.

Please always use code tags: http://www.cplusplus.com/articles/z13hAqkS/
Hi ositamay11,

My first thought is based on the code you posted is that
cout
should be
std::cout

Or you could start your code
1
2
3
4
5
6
#include <iostream>

using namespace std;

double height, weight;
double bmiCal (double height, double weight);

That is the quick answer. I know that the
using namespace
dose save some typing. And I know there is a better explanation of how it all works maybe some one else can explain it better.
using namespace std; 0r
std::cout《"blah blah"
Either of then would work. The first one is not considered a good practice as apparentely it makes program slower but at this level, it is insignificant.
The problem with using namespace std; is that it can cause naming conflicts (it brings into the global namespace all of std that is in whatever header files one has included), and undermines the whole purpose of having namespaces - which is to help prevent naming conflicts. There are lots of ordinary English words that exist in std, such as left, right which are both defined in iostream. So if one had a variable or function named left, there is a conflict with std::left. It's worse the more includes one has - especially with the algorithm header file.

It might seem easy to have using namespace std; now, but it will bite you one day.

It is possible to do this:

1
2
3
using std::cout;
using std::cin;
using std::endl;


but that gets tiresome as well, it's easy to accumulate lots of them. The easiest thing to do in the end is to get put std:: before every std thing. That is what all the experienced coders do, so one might as well get ahead of the game now :+)

Also, using std:: is explicit - std::copy means just that, not boost::copy or some other copy from a different library.

Ideally one should put their own code into it's own namespaces.
Topic archived. No new replies allowed.