Console Output - Random Number Appearing - c++

I'm using OpenGL/Glut/GLM to import and render a model to my screen.
When I'm outputting to the console when the model is loaded or a key is pressed, a number follows the output... I've noticed it a few times now...
The format I use is: std::cout << "\nW key pressed\n" << std::cout; etc...
I hope this isn't a stupid question, but what is that number? And why is it appearing? Can I stop it?

That's the address of std::cout. It's showing up being you are inserting std::cout into std::cout.
Get rid of the std::cout at the end of your statement:
std::cout << "\nW key pressed\n";

Related

How to format output of a SQL query in C++?

Im working on a school assignment in which i need to format a query of some dummy data. The specific query gathers data on a few fields in a set of two tables, and prints it out to the console in C++.
While this works as expected, the only issue is formatting output so that it appears correctly. In our example output in the assignment document, this is shown as being the proper output
<ID> <name> <email> <phone> <ext> <manager>
not the exact spacing but more or less what needs to be done. The initial output i get in the console is similar to this, and looks like:
1056 Tim Tomphson ttomph#fakeEmailService.com +1 800 555 1580 x5122 Tom Timsphon
Until about a few entries down when suddenly the data becomes completely unformatted.
Now im confused, cause I thought i had the formatting right, and dont know why its fallen to hell around that point specifically since everything else up to that was basically perfect. and i dont know how to format it right so that it can return to a proper tabular output.
the code im using for this output is
cout << left << setw(10) << temp.empNum << "\t" << temp.firstName << " " << temp.lastName << "\t\t" << temp.email << "\t\t" << temp.phone << "\t\t" << temp.extension << "\t\t" << temp.reportsTo << endl;
I've also tried using printf statements, but that had less luck and was nowhere as close to successful as this command itself...does anyone have any ideas that could help me out?

How to disable cout output in the runtime?

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 cout outputs, but I still want to see 1 specific output (let's say the final output of the program) in the terminal.
Is it possible to use an ""other way"" of printing to the terminal for showing the program output, and then when suppressing cout still see things that are printed using this ""other way""?
Sure, you can (example here):
int main() {
std::cout << "First message" << std::endl;
std::cout.setstate(std::ios_base::failbit);
std::cout << "Second message" << std::endl;
std::cout.clear();
std::cout << "Last message" << std::endl;
return 0;
}
Outputs:
First message
Last message
This is because putting the stream in fail state will make it silently discard any output, until the failbit is cleared.
To supress output, you can disconnect the underlying buffer from cout.
#include <iostream>
using namespace std;
int main(){
// get underlying buffer
streambuf* orig_buf = cout.rdbuf();
// set null
cout.rdbuf(NULL);
cout << "this will not be displayed." << endl;
// restore buffer
cout.rdbuf(orig_buf);
cout << "this will be dispalyed." << endl;
return 0;
}
Don't use cout for debugging purposes, but define a different object (or function, or macro) that calls through to it, then you can disable that function or macro in one single place.
You can user cerr - standard output stream for errors for your debug purposes.
Also, there is clog - standard output stream for logging.
Typically, they both behave like a cout.
Example:
cerr << 74 << endl;
Details: http://www.cplusplus.com/reference/iostream/cerr/
http://www.cplusplus.com/reference/iostream/clog/
If you include files which involve cout you may want to write the code at the start (outside of main), which can be done like this:
struct Clearer {
Clearer() { std::cout.setstate(std::ios::failbit); }
} output_clearer;
It seems you print debug messages. You could use TRACE within Visual C++/MFC or you just might want to create a Debug() function which takes care of it. You can implement it to turn on only if a distinct flag is set. A lot of programs use a command line parameter called verbose or -v for instance, to control the behavior of their log and debug messages.

Console output in cmd.exe, and powershell.exe through C++

I know a simple way for correct displaying of localized chars on Cmd.exe. But how can I do same for Powershell.exe?
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
SetConsoleCP(GetACP());
SetConsoleOutputCP(GetACP());
// valid output in cmd.exe,
// but invalid output in powershell.exe
cout << "Привет мир (1)!" << endl;
// invalid output in both variants: cmd.exe,
// and powershell.exe
wcout << L"Привет мир (2)!" << endl;
return 0;
}
What is the output? Is your font supporting those glyphs?
perhaps the following link may be useful (about cmd.exe):
http://www.watchingthenet.com/how-to-add-and-change-fonts-in-windows-command-prompt.html
You could also try to redirect output from powershell to a file and check that. If the file is correct, you have a console-font not supporting your characters.
there is also a blog from ms, showing how to customize the font (about ps)
http://blogs.msdn.com/b/powershell/archive/2006/10/16/windows-powershell-font-customization.aspx
I think that's there's a shortcut to enable proper wcout handling in Windows. Try to add that as a first line to your main:
_setmode(_fileno(stdout), _O_U16TEXT);
After that wcout << L"Привет мир (2)!" << endl; will work like a charm independently of current locale and single-byte encoding chosen by user (as it should).

How to update a output field in terminal without output a new line? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to update a printed message in terminal without reprinting (Linux)
I have c++ code, performing some simulations.
I want to show the percentage of my simulation, but I don't want to output a new line every step, like
%1
%2
%3
...
Is there a way, in c++ or in shell scripts to show the progress without creating new lines?
Thanks
Edit 1
Anyone know how to update a number on my personal webpage without refreshing the whole page?
Thanks
Edit 2
double N=0;
forAll (internalIDs_, i) {
N++;
double percent = 100*N/internalIDs_.size();
// Info<< "\rProgress: " << percent << "%" << endl;
printf("\r[%6.4f%%]",percent);}
The terminal cursor keeps blinking cyclically through the numbers, very annoything, how to get rid of this?
The trick used for this is to return to the first position in the current line instead of progressing to the next line.
This is done by writing the \r character (carriage return) to the terminal/stdout.
cout << "\r%1";
cout << "\r%2";
cout << "\r%3";
...
\r - move at the begin of line;
but!
if :
cout << "\rsomelongmessage";
cout << "\rshort";
then you get at out:
shortongmessage
because of:
somelongmessage
^^^^^
short
but you can:
cout << "\rsomelongmessage";
cout << "\rshort ";
then you get finally:
short

Showing progress in command-line application

Ok, I am a bit embarrassed to ask such a simple thing but still.
I have command line utility application and need to show progress to the user.
I could write progress into cout, like this:
std::cout << "10%\n";
...
std::cout << "20%\n";
...
std::cout << "30%\n";
... but as a result user will see:
some line printed before
10%
20%
30%
...
... but what i really need is that percentage got updated, like this at the beginning:
some line printed before
10%
...
... and after update:
some line printed before
20%
...
... and after second update:
some line printed before
30%
...
How should I achieve that?
Instead of using '\n', use '\r':
std::cout << "\r10%" << std::flush;
Print newline ('\n') when done.
It's important to use std::flush so the stream contents really is output.
Use a carriage return.
std::cout << "\r10%";
std::cout << "\r20%";
...
Goes to the beginning of the line.