-4

This is the code, please help me if anything is wrong.

#include <iostream>

int main() {
    char ch = 'A';
    int num = ch;
    cout << "The ASCII code for " << ch << "is " << num << "\n";
    cout << "Adding 1 to the character code : \n";
    ch = ch + 1;
    num = ch;
    cout << "The ASCII code for " << ch << "is " << num << "\n";
    return (0);
}

I get the errors like

ex1.cpp: In function ‘int main()’:
ex1.cpp:6:5: error: ‘cout’ was not declared in this scope
ex1.cpp:6:5: note: suggested alternative:
/usr/include/c++/4.6/iostream:62:18: note:   ‘std::cout’

Guys please correct my mistakes.

David G
  • 94,763
  • 41
  • 167
  • 253
prabuksara
  • 21
  • 4

4 Answers4

3

The problem is that the iostream header provides these objects for you but only under the std namespace. Use a qualified name by prefixing them with std:::

std::cout << code;

It's commonly recommended that you do not use using namespace std because it introduces tokens into the global namespace. You're better off with using the namespace prefix as an alternative.

David G
  • 94,763
  • 41
  • 167
  • 253
3

The global cout object is defined in the std namespace (pretty much like all the stuff in the Standard Library, with only a few exceptions).

Thus, you can either fully qualify the name (and use std::cout):

std::cout << "The ASCII code for " << ch << "is " << num << "\n";
// ...

Or introduce a using declaration:

using std::cout;
cout << "The ASCII code for " << ch << "is " << num << "\n";
// ...

Avoid the bad, global using declaration:

using namespace std;

Which would import all symbols defined in the std namespace into the global namespace, thus leading to a high risk of name clashes. This is a bad programming practice and should be used only in limited situations.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • One more doubt, We have to declare everything like 'cout' 'cin' using 'using std::cout' and 'using std::cin' ? there are many declaration like these or these are the only two? – prabuksara Mar 09 '13 at 18:23
  • @prabuksara: It is a good programming practice to declare everything you need that way as long as this is not going to be too verbose. Of course, if there would be too many names from the `std` namespace that you are using, you could have a `using namespace std` declaration, but you should put that in a limited scope (i.e. inside functions), and not (for instance) at the global scope in a shared header. Otherwise, all the files that import that header will also import all the names from the `std` namespace. – Andy Prowl Mar 09 '13 at 18:28
  • +1 for mentioning `using std::cout;`. – Thomas Matthews Mar 09 '13 at 19:38
0

Use std::cout or add the std namespace. Put this on the top of your file:

using namespace std;
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • `using namespace std` is more often than not a bad idea. – juanchopanza Mar 09 '13 at 18:01
  • Okay, I get that, do you perhaps have some pointers that go a little more in depth? – Bart Friederichs Mar 09 '13 at 19:07
  • 1
    There is a good discussion of the issue in this question: [Why is 'using namespace std;' considered a bad practice in C++?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c) – Blastfurnace Mar 09 '13 at 19:26
-1

cout is a stream defined in the namespace std. So when you use it you either have to write std::cout or you need a line using std::cout in global scope before your first usage of cout.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176