How to load projects with encoding in Visual Studio 2022 - c++

I have to print some ASCII characters (extended version, code page 437) in console application as an exercise. Now, instead of writing cout << char(196) << char(196) << char(196);, I'd like to write cout << "───" to make a line. I have found that I can save files with the correct encoding by clicking "Save as" and choosing "save with encoding" (in this example - OER United States - Codepage 437). After doing that, the following code compiles and runs without any problems:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "\n *** Multiplication Table ***\n\n";
for (int i = 1; i < 11; i++)
{
if (i == 2) cout << "────┼────────────────────────────────────\n";
for (int j = 1; j < 11; j++)
{
if (j == 2) cout << "│";
cout << setw(4) << i * j;
}
cout << "\n";
}
}
However, when I close Visual Studio and reopen the project, suddenly all special ASCII characters are changed. For example, this bit "────┼────────────────────────────────────\n"; becomes "ÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n"; in the code editor. However, the program still runs correctly and displays ────┼────────────────────────────────────.
If I try to save the project with encoding again, I am warned that there are characters that I cannot save with selected encoding and asked to use Unicode instead, which then messes up the characters again and also stops the program from showing the correct characters.
Question: How do I make Visual Studio 2022 use the correct encoding when opening projects?

Related

Why my code in c++ is taking too long to execute?

When I click to compile and run my code in c++, using dev c++, the code takes a while to run in the console, even though it's something very basic. The console screen opens and goes black, with the cursor blinking, the program only starts after a few seconds. How can I solve this problem? Can someone help me, please?
#include <iostream>
using namespace std;
int main() {
int valor[5] ;
int i ;
for(i = 0 ; i < 5 ; i++) {
cout << "digite valor[" << i << "]" << endl ;
cin >> valor[i] ;
}
for( i = 0 ; i < 5 ; i++) {
cout << "valor[" << i << "]: "<< valor[i] << endl ;
}
return 0;
}
Got a similar issue some years ago at work, with the imposed antivirus. All compiled executables, even the smaller one, took several seconds to really be launched.
We were forced to request some special rights to IT in order to be able to exclude from scanning our development folders. It solved the problem instantly.
You should be able to tell your antivirus to exclude your base development folder (let's say "C:\Users\malkmim\Projects" and all its subfolders) from scan, and then test again if you still have this issue.

How to print page by page with cout in C++?

Imagine I have this code:
for (int i=0; i<1000; i++) {
cout << i << endl;
}
So in the console, we see the result is printed once until 999. We can not see the first numbers anymore (let say from 0 to 10 or 20), even we scroll up.
How can I print output page by page? I mean, for example if there are 40 lines per page, then in the console we see 0->39, and when we press Enter, the next 40 numbers will be shown (40->79), and so on, until the last number.
While the previously answers are technically correct, you can only break output at a fixed number of lines, i.e. at least with standard C++ library only, you have no way to know how many lines you can print before filling the entire screen. You will need to resort to specific OS APIs to know that.
UNIX people may tell you that, in fact, you shouldn't bother about that in your own program. If you want to paginate, you should just pipe the output to commands such as less or more, or redirect it to a file and then look at the file with a text editor.
You could use the % (remainder) operator:
for (int i=0; i<1000; i++) {
std::cout << i << '\n';
if(i % 40 == 39) {
std::cout << "Press return>";
std::getchar();
}
}
This will print lines 0-39, then 40-79 etc.
If you need something that figures out how many lines the terminal has and adapts to that, you could use curses, ncurses or pdcurses. For windows, (I think) pdcurses is what you should go for.
#include <curses.h>
#include <iostream>
int main() {
initscr(); // init curses
int Lines = LINES; // fetch number of lines in the terminal
endwin(); // end curses
for(int i = 0; i < 1000; i++) {
std::cout << i << '\n';
if(i % Lines == Lines - 1) {
std::cout << "Press return>";
std::getchar();
}
}
}
This only initializes curses, fetches the number of lines in the terminal and then deinit curses. If you want, you could stay in curses-mode and use the curses functions to read and write to the terminal instead. That would give you control over the cursor position and attributes of what you print on screen, like colors and underlining etc.

endl, '\t' and '\n' dont work after 15 tabs

If you compile and run this code, the endl doesn't get executed. You will get 0hello when you pop the terminal into full screen.
#include <iostream>
int main() {
using namespace std;
for (int i = 0; i < 15; i++) {
cout << '\t';
}
cout << "0" << endl << "hello";
return 0;
}
However, if you use cout << "00" << endl << "hello"; then it works fine. I don't understand why this happens nor how to fix it.
I am assuming you are running this from visual studio where the default terminal width is 120 characters.
A tab is 8 characters. 8x15 = 120.
If you look at the output, there is a blank line before the 0. It is printing the tabs: just that you've reached the end of line so it has moved to the next line.
If you change the terminal width to 80 characters you might get a different result - a blank line and the 0 in the centre of the page.

Input to Visual Studio Console Window not showing

I've recently started to learn how to code, and started with C++. The IDE I'm using is Visual Studio Community 2019 on Windows 10 64-bit. I wrote a small code which I wanted to debug using breakpoints. When Visual Studio enters debug mode it gives the following message on the bottom-left corner: "loading symbols for kernel32.dll" (the message isn't restricted to kernel32.dll, but also ntdll.dll, KernelBase.dll, msvcp.dll, vcruntime140d.dll, ucrtbased.dll). I opened the module tab and automatically loaded all of them (I don't really know what these are, some clarity in this regard would also be appreciated), however, the main issue persists: when I enter a keyboard entry to the console it doesn't show the character entered. After pressing step-into it shows the value that has now been stored inside the declared variable and the entered value on the console, which may or may not have been entered correctly as the character input is invisible in the console window while typing.
Does this have something to do with Stack, Heap management, RAM etc.? Please explain what this issue means and what I can do to solve it as there is no point in debugging if i cannot see what is being printed or entered on the console.
The code is shown below, if it is required (The program takes an input from the user and compares it to a randomly generated number as shown):
#include <iostream>
#include <time.h>
#include <stdlib.h>
int main()
{
int Guess, RandomNumber,End=1;
srand(time(NULL));
RandomNumber = rand() % 100 + 1;
std::cout << "Enter a Number. (HINT: The number lies between 0 and 100!)";
while (End != 0) //this is where I added a breakpoint.
{
do
{
std::cin >> Guess;
if (Guess > RandomNumber)
{
std::cout << "You Guess is incorrect. The special number is smaller than your guess! ";
}
if (Guess < RandomNumber)
{
std::cout << "You Guess is incorrect. The special number is larger than your guess! ";
}
} while (Guess != RandomNumber);
std::cout << "You are CORRECT! The number was: " << Guess<<std::endl<<"Press 1 to play again. To exit press 0"<<std::endl;
std::cin >> End;
}
}

C++ Printing special ascii characters to the Windows console

After 2 hours of searching and trying various methods, I'm pulling my hair out trying to print special ascii characters to the console! (C++)
typedef unsigned char UCHAR;
int main()
{
UCHAR c = '¥';
cout << c;
return 0;
}
Why does this code print Ñ (209) instead of ¥ (165)???
I've tried:
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
but neither seems to do anything, no matter which values I pass to it.
Someone else suggested that the console's font needed to be changed through the registry. But that's ridiculous. I don't want my end users to have to start changing registry values simply to run my program...
the really odd thing is that if I print all the ascii characters to a file (using ofstream), they show up correctly both in notepad, and the visual studio editor (2012 professional).
ofstream file("ASCII.txt");;
if (file.is_open())
{
UCHAR c = 0;
for (int i = 0; i < 256; i++)
{
c++;
file << c << "\t|\t" << (int)c << endl;
}
}
file.close();
Any help is much appreciated.
Thanks!
Welcome to the pain of encoding :(
#include <iostream>
#include <windows>
int main() {
SetConsoleCP(437);
SetConsoleOutputCP(437);
std::cout << (char)157 << "\n";
}
Generates:
The problem is that your source file is not in CP437 and therefore the character has a different value than the one you are trying to print (as you noted, in your source value is is 165 which is a different character in CP437).
https://en.wikipedia.org/wiki/Code_page_437