C++ special characters displays as "?" - c++

I want to print a box using special characters like this
cout << "╔═══╗" << endl;
cout << "║ ║" << endl;
cout << "║ ║" << endl;
cout << "╚═══╝" << endl;
but it displays like this
?????
? ?
? ?
?????
How can I fix this?

You could try this one:
cout << (char)201 << (char)205 << (char)187 << endl;
cout << (char)186 << " " << (char)186 << endl;
cout << (char)186 << " " << (char)186 << endl;
cout << (char)200 << (char)205 << (char)188 << endl;
I tested it and prints what you want

Find out what character set the terminal you are viewing the program output on is using, then use escape codes to put those characters in your strings
There are several types and editor you are programming with may be using a different kind the program displays with.
https://en.wikipedia.org/wiki/Box-drawing_character

Related

Use cout to print a number in a certain format (left align & right align)

If I write code like this:
int a = 123456;
cout << setw(20) << setiosflags(ios::right) << a << endl;
cout << setiosflags(ios::left) << setw(20) << a << '*' << endl;
On the 3rd line, I set the alignment as left align, so my expected output is
123456
123456 *
but the REAL output is
123456
123456*
Why did that happen?
The IDE I use is DevCpp.
std::setiosflags() sets new flags without clearing any existing flags. So on the 3rd line, you are enabling the ios::left flag without disabling the ios::right flag. It does not make sense to have both flags enabled at the same time, and it seems the stream prefers the ios::right flag if it is enabled.
Use std::left and std::right instead. They reset the ios::internal, ios::left, and ios::right flags before setting the new alignment.
int a = 123456;
cout << setw(20) << right << a << endl;
cout << left << setw(20) << a << '*' << endl;
Live demo
If you remove the line with setiosflags(ios::right), it works as expected, so it seems all common compilers evaluate right before left and short circuit the program flow. Try either manually unsetting ios::right, or better yet, just use std::left like so:
cout << left << setw(20) << a << '\n';
This, as so many standard library functions, takes care of pesky details.
int a = 123456;
cout.setf(ios::right, ios::adjustfield);
cout << setw(20) << a << endl;
cout.setf(ios::left, ios::adjustfield);
cout << setw(20) << a << '*' << endl;
If I recall, you need to reset the alignment.
The setiosflags sets format flag for the output stream (here it's cout), not for this sentence only. Since the ios::right has priority over ios::left, the second line will be aligned right. So you need to clear the previous format flag and then set the new one.
int a = 123456;
cout << setw(20) << setiosflags(ios::right) << a << endl;
cout << resetiosflags(ios::right) << setiosflags(ios::left) << setw(20) << a << '*' << endl;
But the simplest way is to use std::left and std::right
int a = 123456;
cout << setw(20) << right << a << endl;
cout << left << setw(20) << a << '*' << endl;

C++ aligning characters to display neatly with any input implemented

I am having trouble keeping the ":" and the "$" aligned with whichever input the user implements through the cin command which is "numTShirts". It stays aligned if implemented 10 and under but discount goes out of place with any other input.
![implemented with 10] http://prntscr.com/gzms3m
![implemented with other] http://prntscr.com/gzmsjx
cout << "\n" << endl;
cout << fixed;
cout << "Thank you for your purchase.\n" << endl;
cout << "You bought " << numTShirts << " T-shirts\n" << endl;
cout << "Subtotal Total" << setw(5) << ": $ " << right << setprecision(2) << subTotal << "\n" << endl;
cout << setprecision(0) << "Discount(" << discountPCT << "%)" << setw(7) << ": $ " << right << showpoint << setprecision(2) << discount << "\n" << endl;
cout << setfill('-') << setw(35) << "-\n" << endl;
cout << setfill(' ');
cout << "Total" << setw(14) << ": $ " << right << showpoint << setprecision(2) << totalPrice << endl;
This is happening because the amount of discount is variable and according to it the space increases.
As you show in the example when the discount is of one digit the aligning is the desired one, otherwise it is different.
As the discoutn can be maximum of three digits (100%) then I'd suggest you two make three formatting of the output.
one for 1 digit discount, 2 digit and 3 digit.
by using a conditional statement you can show the respective in the output.

Strange pointer behaviour with chars

I was playing around with structures and their pointers when I observed a strange behaviour. In the following code I made a structure containing: id, last_name_inital, full_name and first_name_inital. Looking at the memory addresses of the int and string works fine but looking at the chars gives broken output. I added to chars outside the structure which show the same behaviour. Looking at just a char output and it's pointer output gives the same char back so no address.
#include <iostream>
#include <string>
using namespace std;
struct entry
{
int id = 1;
char last_name_inital = 'S';
string full_name = "August Smith";
char first_name_inital = 'A';
};
int main()
{
entry aug;
entry *p_aug = &aug;
char alpha ='A';
char *p_alpha = α
char sirra ='S';
char *p_sirra = &sirra;
cout << "Printing structure address" << endl;
cout << p_aug << endl;
cout << "\n";
cout << "Printing id and it's address" << endl;
cout << p_aug->id << " " << aug.id << endl;
cout << &p_aug->id << " " << &aug.id << endl;
cout << "\n";
cout << "Printing last_name_inital and it's address" << endl;
cout << p_aug->last_name_inital <<" "<< aug.last_name_inital << endl;
cout <<&p_aug->last_name_inital<<" "<< &aug.last_name_inital << endl;
cout << "\n";
cout << "Printing full_name and it's address" << endl;
cout << p_aug->full_name << " " << aug.full_name << endl;
cout << &p_aug->full_name << " " << &aug.full_name << endl;
cout << "\n";
cout << "Printing first_name_inital and it's address" << endl;
cout << p_aug->first_name_inital <<" "<<aug.first_name_inital<< endl;
cout <<&p_aug->first_name_inital<<" "<<&aug.first_name_inital<< endl;
cout << "\n";
cout << "Printing alpha and it's address" << endl;
cout << *p_alpha << " " << alpha << endl;
cout << p_alpha << " " << &alpha << endl;
cout << "\n";
cout << "Printing sirra and it's address" << endl;
cout << *p_sirra << " " << sirra << endl;
cout << p_sirra << " " << &sirra << endl;
cout << "\n";
}
which gives the following output (compiled and written with geany, g++, linux):
Printing structure address
0x7fff01772360
Printing id and it's address
1 1
0x7fff01772360 0x7fff01772360
Printing full_name and it's address
S S
S S
Printing last_name_inital and it's address
August Smith August Smith
0x7fff01772368 0x7fff01772368
Printing last_name_inital and it's address
A A
A# A#
Printing alpha and it's address
A A
AS#w� AS#w�
Printing alpha and it's address
S S
S#w� S#w�
on every run the last output changes, so obviously something is changing.
An online search gave only this question, which sort of touches the same background but not really or I don't get it. character pointers in C++
Basically my main questions are what is happening there and is it possible to output a char pointer, so where it is stored?
Thank you
EDIT:
The quick responses solve the question of how to print out the pointer address of a char. By using:(void*)(&char_name)
However it leaves me wondering why the cout values of the normal chars change but the values of the chars inside the structure stay the same on rerun. Especially also because the behaviour for the char 'S' in the structure with cout is the mentioned operator-overloaded-just-give-back-the-char-itself but not for 'A' and also not for the normal chars outside the structure.
Can anyone explain those differences? Or even does this not occur for you?
You could cast the char * to a void *. The problem is, that the operator<< is overloaded to read char * as c_string.
cout << "Printing last_name_inital and it's address" << endl;
cout << p_aug->last_name_inital <<" "<< aug.last_name_inital << endl;
cout << (void*)(&p_aug->last_name_inital) <<" "<< (void*)(&aug.last_name_inital) << endl;
cout << "\n";
ostream& operator<< (ostream& os, const char* s); is to write sequence of char not address, cast to void* to get address.
Typically, addresses vary between program executions because the operating system happens to have different memory locations available or deliberately shuffles around data for security reasons.
Inspecting pointer values is only useful for comparing pointers in the same program instance: for example, p_aug and &aug have the same value.

Program won't Print a string array C++

I have been working on this program for awhile, but it refuses to cooperate on this last little stretch. The point of the program is to sift a data file into three arrays, sort the arrays, then print them out into a table. The problem I'm having appears to be with the table. The program is divided into four functions, and when I attempt to debug, it won't show the productName array in the function.
The malfunctioning segment of code looks like this:
void printReport (string productName[], int numberinStock[], float price[], int number_of_products)
{
float totalPrice;
cout << setw(18) << " " << "Friendly Grocer Store Inventory" << setw(17) << " " << endl;
cout << setw(18) << "Inventory Item" << setw(16) << "Number in Stock" << setw(16) << "Unit Price" << setw(16) << "Total Sales" << endl;
for (int count=0; count <number_of_products-1; count++)
{
cout << setw(18) << productName[count] << setw(16) << numberinStock[count] << setw(16) << std::setprecision(2) << price[count] << setw(16) << std::fixed << std::setprecision(2) << price[count]*numberinStock[count] << endl;
}
cout << "Total Price: " << totalPrice;
}
It will print everything else, but not the productName.
Some debugging statements outside of the for loop like cout << productName[1] will print out the proper productName but it's completely blank on the actual report.
After some debugging it seems like after printing the productName in the for loop every item after that overwrites the product name.
For example just leaving cout << setw(18) << productName[count] << setw(16) << numberinStock[count] << endl;
will produce
" 3s"
" 10h"
" 2a"
The product names there are Mangoes, Sandwich, and pizza.
I'm at a loss. Where did I mess up?
You might have screwed up passing the data into the function. If you set up test arrays in the function it should be printing correctly.
To pass arrays in C++ use the arrayname.
eg
int main ()
{
string productName[] = {"mango"};
...
printReport(productName, numofProduct);
return 0;
}

C++ - How to reset the output stream manipulator flags [duplicate]

This question already has answers here:
Restore the state of std::cout after manipulating it
(9 answers)
Closed 4 years ago.
I've got a line of code that sets the fill value to a '-' character in my output, but need to reset the setfill flag to its default whitespace character. How do I do that?
cout << setw(14) << " CHARGE/ROOM" << endl;
cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl;
I thought this might work:
cout.unsetf(ios::manipulatorname) // Howerver I dont see a manipulator called setfill
Am I on the wrong track?
Have a look at the Boost.IO_State_Savers, providing RAII-style scope guards for the flags of an iostream.
Example:
#include <boost/io/ios_state.hpp>
{
boost::io::ios_all_saver guard(cout); // Saves current flags and format
cout << setw(14) << " CHARGE/ROOM" << endl;
cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl;
// dtor of guard here restores flags and formats
}
More specialized guards (for only fill, or width, or precision, etc... are also in the library. See the docs for details.
You can use copyfmt to save cout's initial formatting. Once finished with formatted output you can use it again to restore the default settings (including fill character).
{
// save default formatting
ios init(NULL);
init.copyfmt(cout);
// change formatting...
cout << setfill('-') << setw(11) << '-' << " ";
cout << setw(15) << '-' << " ";
cout << setw(11) << '-' << endl;
// restore default formatting
cout.copyfmt(init);
}
You can use the ios::fill() function to set and restore the fill character instead.
http://www.cplusplus.com/reference/iostream/ios/fill/
#include <iostream>
using namespace std;
int main () {
char prev;
cout.width (10);
cout << 40 << endl;
prev = cout.fill ('x');
cout.width (10);
cout << 40 << endl;
cout.fill(prev);
return 0;
}
You can manually change the setfill flag to whatever you need it to be:
float number = 4.5;
cout << setfill('-');
cout << setw(11) << number << endl; // --------4.5
cout << setfill(' ');
cout << setw(11) << number << endl; // 4.5
The null character will reset it back to the original state:
setfill('\0')