How to format output of a SQL query in C++? - 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?

Related

Is there a way to get regional time format with Boost?

as I tried to ask in my title, I am talking about regional time format. I have an application that can display the time/date, but from US users I get the question of why am I displaying the time like this: 21:30 instead of this: 9:30PM. Same thing for the date to which I display the date as 22.05.2018 instead of 05.22.2018.
So my questions is: Is there a way to achieve getting a timezone flag or location or even the time already formatted so that I can display the time/date different for different regions?
With Boost Locale
double now=time(0);
cout << as::datetime << as::local_time << "Local time is: "<< now << endl;
cout << as::gmt << "GMT Time is: "<< now <<endl;
cout << as::time_zone("EST") << "Eastern Standard Time is: "<< now <<endl;
Parse date from string back and add the time-zone

Making a text box in c++

I'm programming some very simple stuff and I want to make a neat little "text box" at the beginning of my program in the command prompt with some basic info. An example of what I mean:
cout << "Creators: J. Adams & T. Jefferson" << endl;
cout << "Nicknames: Johnnny & Tommy" << endl;
cout << "Age: 20 & 21" << endl;
cout << "This program is for ..... << endl;
This is just an example, and one of the things I'd like is a simple way to align these things (so that "Johnny" is directly below "J. Adams", or at least directly enough so you know they belong together). I know of setw but you have to include for that, and I'd like a method where you don't have to use that.
Other than that, I'd like a simple way to put an outline around the text (that's what I mean by text "box"). Nothing to fancy, just something you can do with the first 128 normal characters. I've googled around for something similar to no avail.

Mongodb Update using C++ API

I have written a C++ function to update a mongodb document. It is as below.
void
LocationDb::setServingRouter(string sensor, string oAp, string nAp) {
m_conn.update("location_db.ldb",
BSON("id" << sensor << "name" << "/temp/s/1" << "ap" << BSON("name" << oAp)),
BSON("id" << sensor << "name" << "/temp/s/1" << "ap" << BSON("name" << nAp)));
}
This update query is working; however, I believe it has some limitation i.e. every time I update, I need to pass it the old ap name (oAp).
I find this very annoying in the sense that I just want to update the ap name without bothering was old name is. As per this query implementation, it is first finds the matching record in the document, and then update it.
How can I just update by matching the query partially? Bottom line is that I don't want to remember what old record was. Any help?

Console Output - Random Number Appearing

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";

How to have the exact same formatting of numbers in C++and R?

I am writing a functional test in R for a program in C++. The typical output file has some columns as "string" and the other columns as "double". Then, I would like to use diff to compare the expected output file returned by R with the observed output file returned by C++.
In pseudo-C++, I simply do this:
stringstream ssTxt;
ssTxt.precision (7);
ssTxt.setf (ios::scientific);
for(i=0; i<10; ++i){
ssTxt << names[i] << " " << values[0][i] << " " << values[1][i] << " " << values[2][i] << endl;
// write in file and clear ssTxt
}
Here is a typical output:
item1 2.8200000e-01 500 4.1846912e-04
In R, I do that:
results <- data.frame(name="item1", val1=0.282, val2=500, val3=0.00041846912873)
write.table(x=format(results, digits=8, nsmall=7, scientific=TRUE), file=...)
Here is the output corresponding to the same data:
item1 2.82e-01 500 4.1846912e-04
As you can see, it almost works, but R doesn't add trailing 0 after "2.82". I prefer to change the R code rather than the C++ code. So how can I do that?
Have you tried
sprintf or formatC?
To be specific I think
sprintf("%e", 2.82e-01)
should do the trick but as mentioned above have a look at help(sprintf), which describes the various formatting capabilities of this function ...