I want to use cout to print out this sentence: "You can build piramid which floor is only odd. not even", but I want to do it more comfortably. Just like the way below. But, when I use this way, an error occurs. So, is there any way to use it like this?
cout << "You can build piramid which floor is only odd.
not even" << '\n';
Adjacent string literals will automatically be concatenated, even if they are on different lines. So you can write it this way instead:
std::cout << "You can only build pyramids whose floor is odd, "
"not even.\n";
To use multi-line strings in C++, you can use backslash.
cout << "You can build piramid which floor is only odd. \
not even" << '\n';
Related
I am attempting format the output of a file using setw with no success. I understand that my output is shifting based on the character length of the variable before it, however I do not know how to change my code so that each row will print independent of the row before it. (Please see image link)
Due to the word "Swimming" being longer than all other word types, this shifts the information for those columns, making the report look terrible. Additionally, it seems like the comma in the "Amount" Column is shifting the output as well.
fout << s.getID()
<< right << setw(11) << addCommas(s.getAmount()) << "\t"
<< left << setw(14) << s.getType() << "\t"
<< left << setw(14) << s.getLength() << "\t"
<< left << setw(10) << s.getDate() << " "
<< left << setw(15) << s.getFname() << "\t"
<< s.getLname() << endl;
lineCount++;
Current Program Output
(Currently messing with those setw values so please disregard that they don't line up with the header at the moment)
EDIT: Changed my font in VS to Helvetica because Consolas hurts my eyes, this also affected the output of my .dat, so essentially this was a non-issue. Yikes.
If you add std::internal to your std::cout statement, it'll pump the remaining spaces out, and you can do a new setw after that. This might work better than tabs for you, especially if you're not using a monospaced font. See an example in this answer. Make sure your setw is large enough to accommodate your largest words... and don't do using namespace std, it's poor practice. Shame on you! ;)
I am beginner at C++, my question is can I remove endl the one at the end of cout like :
cout<<" "<<endl;
I don't want return to new line . If I can't remove it, what should I do?
Elephant in the room: remove the endl from the cout.
But in case you don't own that code, you could try "\033[F", which, if your terminal supports it, moves you to the previous line.
Another possibility would be to redirect the cout buffer using rdbuf to an ostream that you control. You could (i) redirect, (ii) call the function that writes the errant cout with the new line and (iii) inspect your ostream and write to the original buffer, this time omitting the endl. Switch everything back once you're done.
Yes of course you do not need to use std::endl every time you use <<. As an example a simple way to print a vector with spaces between the elements would look like:
std::vector<int> foo = {1,2,3,4,5};
for (auto e : foo)
std::cout << e << " ";
Here we never use a endl. You cout also just use \n at the end of a string literal and that will put a newline in the buffer as well.
std::cout << "test\n";
std::cout << "this will be on a new line";
Notice that I don't put a newline in the last cout<< so if there is anymore output it will start off right after the "e" in "line".
Yes you can remove endl. It is an optional parameter to the << stream operator of which there are many. If you don't include it then a new Carriage Return/Line Feed character will not be output and therefore text will appear on the same line in the output (presumably console).
For example
cout << "Hello, World" << endl;
would become:
cout << "Hello, World";
or to make the point another way you could write:
cout << "Hello,";
cout << " World";
There are lots of other examples out there too, here's one for starters: http://www.cplusplus.com/doc/tutorial/basic_io/
cout<<"Your message here.";
It's as simple as that. Were you trying to do something like...
cout<<"Your message here."<<; ....? This is wrong as:
The << operator signifies that something comes after the "" part. You don't have any in this case.
This question is an extension to this one.
Eclipse Debug run and console run of the same program give different outputs
To give a bit of background...
While parsing the output of the NTP application I discovered something rather odd in my string outputs. The environment that I am using is eclipse with CDT in Debug mode, redirecting console output to a terminal (/dev/pts/0) with fish running as shell.
Where is what I found...
Strings with starting characters or charachter within like *, +, &, ^. such as when I try to cout like this.
cout << "+123" << endl; //does not get printed
cout << "*123" << endl; //does not get printed
cout << "&123" << endl; //Concatenates with string below (without the line feed)
cout << "|123" << endl; //^
cout << "^123" << endl; //Does not get printed
Also
cout << "abcdef" << endl << endl;
does not have the desired effect of two linefeeds; only one gets printed.
Whereas when I run the progam through the terminal...
~$ ./Project/Debug/Project
Everything is normal and works as expected.
Is this expected behaviour, something I am not aware of?
Or is this a bug, corruption or something if such nature?
Or am I doing something wrong?
P.S: I might have left some details out by accident, please read the question in the link provided about to catch up.
I'm working on a program and have a strange, cout related problem. Since the program is a bit big and the code talks best, I'll paste the relevant snippets.
First, I have an iterator, *it defined in a for as
for(vector<facet*>::iterator it=facets_to_dump->begin(); it<facets_to_dump->end(); it++)
In this for, if I use the expression
facet* facet_to_work_on = *it;
cout << facet_to_work_on->facet_id << "\t";
Nicely prints out integers.
But, if I use the notation
cout << (facet*)(*it)->facet_id << "\t";
This code prints out hex values. Hex values are equal to the integer values. Any idea why this is happening?
Thanks in advance.
The reason
cout << (facet*)(*it)->facet_id << "\t";
prints out a hex value is that -> binds harder than the (facet*) cast, that is it evaluates
(*it)->facet_id
and casts the result to a facet*. Pointers are output in hex.
Try including <iomanip> and:
cout << dec << (facet*)(*it)->facet_id << "\t";
To say you want numbers in decimal form.
printf("%-8s - %s\n", c->name ? c->name : "", c->help);
I think what you're looking for is
cout << left << setw(8) << (c->name ? c->name : "") << " - " << c->help << endl;
The left and setw(8) manipulators together have the same effect as the %-8s formatting specifier in printf. You will need to #include <iomanip> in order for this to work, though, because of the use of setw.
EDIT: As Matthieu M. pointed out, the above will permanently change cout so that any padding operations print out left-aligned. Note that this isn't as bad as it might seem; it only applies when you explicitly use setw to set up some padding. You have a few options for how to deal with this. First, you could just enforce the discipline that before using setw, you always use either the left or right manipulators to left- or right-align the text, respectively. Alternatively, you can use the flags and setf function to capture the current state of the flags in cout:
ios_base::fmtflags currFlags = cout.flags();
cout << left << setw(8) << (c->name ? c->name : "") << " - " << c->help << endl;
cout.setf(currFlags, ios_base::adjustfield);
This works in three steps. The first line reads the current formatting flags from cout, including how it's currently aligning padded output. The second line actually prints out the value. Finally, the last line resets cout's output flags for internal alignment to their old value.
Personally, I think the first option is more attractive, but it's definitely good to know that the second one exists since it's technically more correct.
If you've got the Boost libraries:
std::cout << boost::format("%-8s - %s\n") % (c->name ? c->name : "") % c->help;