FAQ Database Discussion Community


what does cout << “\n”[a==N]; do?

c++,cout,string-literals
In the following example: cout<<"\n"[a==N]; I have no clue about what the [] option does in cout, but it does not print a newline when the value of a is equal to N....

How to print array of LPCTSTR in c++?

c++,arrays,cout
How to print array of LPCTSTR example LPCTSTR pszValueNames[] = { L"partnerID", L"partnerSubID", L"transactionID" }; for (int i = 0; i < (sizeof(pszValueNames) / sizeof(LPCWSTR)); i++) { cout << (*pszValueNames[i]) << endl; } Above give some numbers which is not real lpctstr values. When i use wchar_t* and all other...

cout not executing outside a for loop

c++,arrays,for-loop,cout
I'm writing a program that outputs the sum of user-input, space-separated integers (no more than 100). I'm supposed to read the values into an array such that the input "1 2 3" produces "6". This is what I have so far: #include <iostream> using namespace std; int main() { int...

(Lower level of C++) When using “cout” on a piece of data, were does it go to before being displayed on screen?

c++,cout,cpu-registers
Specifically talking about the C++ part of the code here: [LINK] (intel x86, .cpp & .asm hybrid program.) From dealing with chars/strings' pointers in .asm I know it uses dl/dx registers for their storage-before-display (in case of 2h and 9h functions). How is it in the case when the data...

Getting different output for cout and printf while printing same variable

c++,printf,cout,sin
Using Devc++ for printf getting 0.00000 and for cout getting some weird output. #include<iostream> #include<cmath> #include<stdio.h> using namespace std; main() { float i,j=M_PI; i=sin(j); printf("%f \n",i); cout<<i; } ...

Flush cout manually only

c++,stream,cout,flush
As far as I know streaming std::endl into std::cout will flush it. I get that that behavior makes sense for most applications. My problem is that I have some output that uses multiple std::endl's and therefore flushes the output. This is really bad for the performance of my program and...

How to cout a vector of strings?

c++,string,vector,output,cout
what is the easiest way to print out a vector of strings on the console? I got something like this map < int, vector<string>> and i want to print the values to a user-given key. typemap::iterator p1; cin >> i for (pointer = map.begin(); pointer != map.end(); ++pointer) { if...

C++: Printing or cout a standard library container to console

c++,printing,cout,c++-standard-library
What's the C++ way to print or cout a C++ standard library container to the console, to view its contents? On a separate note, why doesn't the C++ library actually overload the << operator for you? Any history behind it?...

template specialization function c++

c++,templates,cout
This is my code: #include <iostream> using namespace std; template< typename T > T silnia( T w ) { cout << "not special" << endl; } template<> int silnia<int>( int x ) { cout << "special" << endl; } int main() { cout << silnia<double>(5) << endl; cout << silnia<int>(5)...

In C++, how can I cout a logo that features many special characters?

c++,escaping,cout
I have a logo that features many special characters, such as escape characters, and I want to print it to the terminal. How should I do this without the compiler throwing "unknown escape sequence" errors? Here is some example code (that features only one problematic character -- not, say, hundreds):...

CLion cout opens command prompt window instead of using CLion's terminal

c++,terminal,command-prompt,cout,clion
When I do cout << "test" in clion and then build it instead of writing it to IDE's terminal like IntelliJ, PHPStorm or any other of the Idea IDEs it opens a new command prompt window, writes it to that and them immediately closes the command prompt window so I...

Error C2679: binary '<<' : no operator found (vector iterators used)

c++,operator-overloading,operator-keyword,cout,ostream
The full error message reads: Error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator< std ::_Vector_val < std::_ Simple _ types < projects > > >' (or there is no acceptable conversion) Not the prettiest but here's my code up to the error,...

C++ cout not displaying anything

c++,cout
What may explain why the cout is not displaying anything in this code? I know it's related to the line v[0] = 1; but I can't find out why, does someone have an explanation for it? Edit: I also know that changing v[0] = 1; for v.push_back(1); would solve the...

Are two calls to cout less efficient than one?

c++,performance,c++11,cout
I have two questions: Is two calls to std::cout less efficient than one? If yes, does the compiler (usually) optimize it and generate equal assembly for the three cases shown below? For example: std::cout << "a" << "b"; or std::cout << "ab"; or std::cout << "a"; std::cout << "b"; I...

Numbered filenames in C++?

c++,compiler-errors,g++,cout
I have data that I would like to output to files named matrix_1.txt, matrix_2.txt, etc. The following code works in visual studio: //get ready to write to file std::ofstream myfile; //define filename std::ostringstream oss; oss << "output_matrix_" << sim_index << ".txt"; myfile.std::ofstream::open (oss.str()); //write to file myfile<<"stuff\n"; //close the file...

How to get rid of std::cout quickly

c++,debugging,cout
I have inherited a large codebase where std::cout is very frequently used with the purpose of printing out debugging information. There is no real debug/log functionality in the code and since this will be a short lived task (bugfix) implementing debug/log functionality for now is out of the question. It...

How to print “\a” in c++ using codelite?

c++,iostream,cout
I was trying to print \a but it shows nothing so I searched for this and found out it should have made a sound but it didn't either. I am using code lite on Windows 8. How to print \a? Where is that sound? This is my code: #include <iostream>...

how to disable cout output in the runtime?

c++,cout
I often use cout for debugging purpose in many different places in my code, and then I get frustrated and comment all of them manually. Is there a way to suppress cout output in the runtime? And more importantly, let's say I want to suppress all my cout outputs, but...

Why in C++ when i concat a string with '+' like it don't concat but remove first char of the string [duplicate]

c++,string,concatenation,cout
This question already has an answer here: Why in the code “456”+1, output is “56” [duplicate] 3 answers i was learning C++ after 6-7 years of java and i have encouter a problem, in this snippet that instead of "concat" it's just remove the n character as the input...

Typo on wikipedia input/output examples

c++,cout,flush
I am reading the following example from wikipedia. #include <iostream> int main() { std::cout << "Hello, world!\n"; return 0; } The article reads then as: This program would output "Hello, world!" followed by a newline and standard output stream buffer flush. I do not believe this is correct, since a...

undefined reference to 'std::cout'

c++,c++11,gcc,cout
Shall this be the example: #include <iostream> using namespace std; int main() { cout << "Hola, moondo.\n"; } It throws the error: gcc -c main.cpp gcc -o edit main.o main.o: In function `main': main.cpp:(.text+0xa): undefined reference to `std::cout' main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'...

C++ Program Weird int

c++,cout
i'm running a C++ program that classifies data into two different categories using a threshold to distinguish between them. But It wasn't working when i cout the int that holds the data it showed this -1.#IND. Has any one ever seen that??And if they have can you tell me how...

Switching to cout from printf - Complex format specifier patterns

c++,c,printf,cout
I have to rewrite a logging system in C++ as part of project requirements (everything has to be C++ instead of C now), and there are a number of ways in which we log things like mathematical data and pointer addresses. It is fairly common to see a log like:...

cout not working in c++ after taking line input (getline)

c++,string,cout
I am in midway of implementing fairplay algorithm, but I noticed that cout statements are not working Below is my code. Note that all required header files are included and there are no compilation errors (by far my knowledge). The program runs without errors and I get terminated command (again,...

Difference between 'cout<

c++,cout
I want to know actual difference between cout<<cout and cout<<&cout in c++? In my compiler cout<<cout returns 0x477864 & cout<<&cout returns 0x477860 at any time.It shows it has 1 digit of difference between them.What are the significance of these?

Printing out the hex vale of an unsigned char array in C++

c++,string,hex,std,cout
I want to print out the hex value of an unsigned char array using the cout function. The most obvious approach would be something like the following. unsigned char str[] = "foo bar baz\n"; for(unsigned short int i = 0; i < sizeof(str); i++){ std::cout << std::hex << str[i] <<...

Formatted output in C++

c++,formatting,cout
I am playing around with the setw and setfill functions but I'm absolutely stuck now. Here is my code: char separator = ' '; cout << "-" << setw(INET_ADDRSTRLEN*4 + INET6_ADDRSTRLEN) << setfill('-') << "\n"; cout << "| Name" << setw(INET_ADDRSTRLEN) << setfill(separator); cout << "| IPv4" << setw(INET_ADDRSTRLEN) <<...

Type of ternary expression

c++,ternary-operator,cout
Can anyone explain the output of the following program: #include <iostream> using namespace std; int main() { int test = 0; cout << "First character " << '1' << endl; cout << "Second character " << (test ? 3 : '1') << endl; return 0; } Output: First character 1...

Can printing cout << '\n' garble text output?

c++,newline,cout
I have a simple program to test a function I'm writing that detects if a text file for a maze is valid. The only allowed characters are '0', '1', ' ' (space) and '\n' (newline). However, when I execute the code with a sample text file, I get some strange...

How can I make the sign appear at the end of the fill when using std::cout and std::stringstream?

c++,std,cout,stringstream
I have the following code for formatting a printout to always be 4 digits with sign included: std::stringstream pitch; pitch.precision(0); pitch.width(4); pitch.fill('0'); pitch << std::showpos << (int)(m_values["Pitch_1"]); I would also like to show the sign ("+"/"-"), but I want it to precede the fill, as follows: +002 However, the code...

std::cout and printf array

c++,c,printf,cout,format-specifiers
int arrSize = 10; double* arr = new double [arrSize]; for(int i = 0; i < arrSize; i++){ arr[i] = i; } for(int i = 0; i < arrSize; i++){ printf("%d", arr[i]); //std::cout<<arr[i]; } Here printf() prints 0000000000. cout prints 0123456789. Why ?...

Unexpected Line Breaks in Output

c++,c++11,line-breaks,cout,extraction-operator
In this answer I have the following code: #include <iostream> #include <vector> #include <string> #include <sstream> #include <iterator> #include <limits> using namespace std; struct station{ string _stationName; int _studentPass; int _adultPass; }; std::istream& operator>>(std::istream& is, station& rhs){ getline(is, rhs._stationName, ';'); is >> rhs._studentPass >> rhs._adultPass; return is; } int main(){...

Why cout is producing no output on Code Blocks?

c++,codeblocks,iostream,cout
#include <iostream> using namespace std; int main(int argc, char** argv) { cout << "Whatever"; return 0; } Cout does not work, nor printf, nor puts, nor anything. Also, I've checked the project properties and both the debug and release are set to "Console Application" and to "Pause in the end"....