Dev C%2b%2b Cout Does Not Name A Type

Reading time: 30 minutes | Coding time: 10 minutes

I was learning Adam Drozdek's book 'Data Structures and Algorithms in C', well, I typed the code in page 15 in my vim and compiled it in terminal of my Ubuntu 11.10. #include <iostream> #i. The first problem is that extern is only valid before the name of a type, so that's what the compiler is complaining about. The second is that cout is defined in the namespace std, so you need to tell the compiler to look there. The good thing is that the code doesn't say using namespace std.

C++ uses the concept of streams to perform I/O operations. A stream is a sequence of bytes in which character sequences are 'flown into' or 'flow out of'. Streams operate as the intermediate between the program and the I/O devices thus helping the programmers to achieve device independent I/O operations. The two types of streams in C++ are:

  • input stream
  • output stream

Headerfiles available in C++ for Input/Output Operations :


Hi lonelycloud, I have been having some problems with the XXXX does not name a type and I'm interested to see how did you fix yours. Im sorry I cant help you with your other problem. The cout object is ensured to be initialized during or before the first time an object of type iosbase::Init is constructed. After the cout object is constructed, it is tied to cin which means that any input operation on cin executes cout.flush. The 'c' in cout refers to 'character' and 'out' means 'output', hence cout means.

  1. iostream - iostream stands for standard input-output stream. This header file contains definitions to objects like cin, cout, cerr etc.
  2. iomanip - iomanip stands for input output manipulators. The methods declared in this files are used for manipulating streams. This file contains definitions of setw, setprecision etc.
  3. fstream - This header file describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.

The objects being discussed in this article are cin and cout. They are the most commonly used objects for taking inputs and printing outputs.

Standard output stream (cout)

cout is an object of the class ostream. The standard output device is the display screen. The characters are inserted into the output stream. The insertion operator (<<) is used along with cout to insert values into the stream so that the output device (monitor) can use them.

Usage


Multiple insertion operations (<<) can be chained in a single statement:

Output

Chaining multiple insertions is useful to mix literals and variables in a single statement:

Assuming the variable name is initialised with 'Alice' and age with 25, the output of the previous statement would be :

The cout operator does not add linebreaks add the end of an output. To add a linebreak, the newline character ('n') has to be used.

Output

The same thing can also be achieved using endl manipulator instead of the new-line character.

This produces the same output as above.

Note -While both the newline character and endl produce the same output, the key difference between the two is that endl causes a flushing of the output buffer every time it is called, whereas 'n' does not.

Output Formatting :


Formatting the output is an important part of any application, to improve the user interface and enhance the readability.

Formatting in C++ is done through the use of I/O manipulators. Most of these manipulators are available in iostream and iomanip.

The standard manipulators are :-

  • endl - Introduces a line break on the output stream similar to 'n'. The example was seen above .

  • setw() - The setw() manipulator sets the width of the field assigned for the output. It takes the size of the field (in number of characters) as argument.

Example :

Output

When chaining multiple insertion operations, the setw() manipulator does not stick from one operation to the next. To set the width for each operation, setw() has to be repeated.

Example :

Output :

  • setprecision() - The setprecision() manipulator sets the total number of digits to be displayed when floating-point numbers are printed.

Example :

Output

The setprecision() manipulator can also be used to set the number of decimal places to be displayed. To do this, you have to set an ios flag.

Output

Note - Unlike setw() , all the subsequent couts after setprecision() retain the precision set with the last setprecision(). That means setprecision() is 'sticky'.

  • Additional IOS flags :
flagmeaning
leftleft-justify the output
rightright-justify the output
showpointdisplays decimal point and trailing zeros for all
all floating point numbers, even if decimal places
are not required
showposdisplay a plus sign before positive values
scientificdisplay floating point numbers in scientific notation

* Standard input stream (cin) :


cin is an object of the class istream . The standard input device is they keyboard. When the user enters characters via the keyboard, the keycode is placed in the input stream. The extraction operator ( >> ) is used with cin to extract values from the stream.

Usage

Output

Like the insertion operator, extraction operator can also be chained in a single statement.

This is the equivalent of

In both cases, the user must provide two values separated by any blank separator (space, tab or newline).

C%2b%2b

Using cin with strings


cin can be used to read strings the same way it is used with other fundamental data types. However, cin always considers any white space character as terminating the value being extracted. So extracting a string with cin means extracting only a word and not a sentence.

To overcome this, a function called getline() can be used that takes the stream (cin) as first argument, and the string variable as second.

Example :

CoutDev c 2b 2b cout does not name a type 3

Output

Dev C%2b%2b Cout Does Not Name A Type

Another way to use getline with cin :

cin.getline(char buffer, int length) - Reads a stream of characters into the string buffer. It stops when it has read length-1 characters or when it finds an end-of-line character ('n').

Dev C 2b 2b Cout Does Not Name A Type 2

Example :

Output

Dev c 2b 2b cout does not name a type 3

Dev C 2b 2b Cout Does Not Name A Type 3

Apart from getline(), cin can also be used with other member functions. Some of them include :

Dev C 2b 2b Cout Does Not Name A Type B

funtionmeaning
cin.get(char &ch)Reads an input character and store it in ch
cin.read(char buffer, int n)Reads n bytes from the stream into the buffer
cin.ignore(int n):Ignores the next n characters from the stream
cin.eof():Returns a nonzero value if end of file is reached

Dev C 2b 2b Cout Does Not Name A Type 1

With this article at OpenGenus, you must have the complete idea of Cin and Cout in C++. Enjoy.

I try to restart codeblocks(I've read that does the trick) and this is what I'm greeted with.
|| Build: Debug in 1D Combat Simulator (compiler: GNU GCC Compiler) |
objDebugmain.o:crt1.c|| multiple definition of `mainCRTStartup'|
c:program files (x86)codeblocksmingwbin..libgccmingw324.7.1......crt2.o:crt1.c|| first defined here|
objDebugmain.o:crt1.c|| multiple definition of `WinMainCRTStartup'|
c:program files (x86)codeblocksmingwbin..libgccmingw324.7.1......crt2.o:crt1.c|| first defined here|
objDebugmain.o:crt1.c|| multiple definition of `atexit'|
c:program files (x86)codeblocksmingwbin..libgccmingw324.7.1......crt2.o:crt1.c|| first defined here|
objDebugmain.o:crt1.c|| multiple definition of `_onexit'|
c:program files (x86)codeblocksmingwbin..libgccmingw324.7.1......crt2.o:crt1.c|| first defined here|
objDebugmain.o:cygming-crtbegin.c|| multiple definition of `__gcc_register_frame'|
c:program files (x86)codeblocksmingwbin..libgccmingw324.7.1crtbegin.o:cygming-crtbegin.c|| first defined here|
objDebugmain.o:cygming-crtbegin.c|| multiple definition of `__gcc_deregister_frame'|
c:program files (x86)codeblocksmingwbin..libgccmingw324.7.1crtbegin.o:cygming-crtbegin.c|| first defined here|
c:program files (x86)codeblocksmingwbin..libgccmingw324.7.1crtbegin.o:cygming-crtbegin.c|| undefined reference to `_Jv_RegisterClasses'|
|| Build failed: 13 error(s), 0 warning(s) (0 minute(s), 0 second(s)) |
So the winmax@16 went away, but now I get 13 new errors. AWESOME!!!!