C++ (g++) special characters encoding ('\a', '\b', etc.) - c++

I am using g++ version 4:4.8.2-1ubuntu6 with Eclipse 3.8 on Linux Mint.
Following example from my C++ book does not work as expected:
//bondini.cpp -- using escape sequences
#include <iostream>
int main()
{
using namespace std;
cout << "\aOperation \"HyperHype\" is activated\n";
cout << "enter sercret code:________\b\b\b\b\b\b\b\b";
long code;
cin >> code;
cout << "\aYou entered: " << code << "...\n";
cout << "\aCode OK! Commencing Z3!\n";
return 0;
}
I get following result when running the program:
In Eclipse and directory I am using UTF-8 encoding. Why does not '\a' play sound as it should and '\b' does not move the cursor one space back, while '\n' works properly.
edit: As I understand it, it is the compiler that makes the mess of it. --> I was wrong, in terminal it works fine, but eclipse 'terminal' does not work.

Wherever you are sending your output. What the destination does with it is entirely in its own hands. So while eclipse might not support these special characters your terminal should.

Related

cout/cin not working properly in VS code only

I'm having a problem specifically with VS code where cout and cin are not working in c++ as expected.
The program is somehow terminating after the first cout and the debugger shows "paused on exception" and then "segmentation fault".
Since the same program is running perfectly in Apache Netbeans and other online compilers, I don't think cin.ignore() or endl or anything like that is required.
C programs run perfectly fine though (in VS).
#include <iostream>
using namespace std;
int main() {
int l,b;
cout << "\n\nENTER L and B : ";
cin >> l >> b;
cout << "L and B entered are : " << l << "\t" << b;
}
code + output image
Here is the debugger screen image
Even simple one variable cin statement isn't running and the program is terminating automatically. cin ain't executing
cout with any variable or constant ain't running correctly either.
simple cout image
My guess is something's wrong with memory allocation by VS code.
There seems to ba a problem with your Compiler installation.
Consider checking the compiler installation steps from the VS code documentation.

VS Code exits without displaying any output when a character array is taken as input

I have tried updating my g++ installation but there has been no solution.
Here is the code.
#include <iostream>
using namespace std;
#include<string>
#include <cstring>
int main()
{
char str[100];
cout << "Enter a string: ";
cin>>str;
cout << "You entered: " << str << endl;
cout << "\nEnter another string: ";
cin>>str;
cout << "You entered: "<<str<<endl;
return 0;
}
The output that this code shows is:
Check the terminal
Please give me a solution or at least a reason for this. I am new to Stack Overflow so please free to correct me if I made any mistake in the post. [This problem only happens in vs code but works in online gdb compiler.]
Edit:After I tried executing this in the cmd line this what it shows
cmd line execution
The output you're seeing is from some kind of makefile. You didn't say how you're trying to build the file so it's hard to say what caused the issue.
However, you can simply compile your file directly by typing g++ hgg.cpp -o hgg in the command shell
After writing the code, switch to Command Prompt (Cntrl + x) move to the directory, in which the C++ program is present.
Then run the following:
g++ -Wall filename.cpp -o filename
If nothing is printed on the console, then your code is syntactically correct. Then do the following:
filename.exe
It should work now.

unexpected % symbol after running cpp program in vscode terminal

tried running code but I'm facing an unexpected "%" symbol at end of the output.
#include < iostream >
using namespace std;
int main()
{
cout << " abc " ;
}
please help!!!
That is most likely your shell telling you that the output did not place a newline. zsh and fish do this by default, I believe. The screenshot indicates macOS, which does use zsh as of Catalina.
If you don't want to see that, change your code to output a newline.
cout << " abc \n";

Print (and store) high ASCII character (╔) in C++ in console

I'm making a small program in C++ and I would like to have this character stored in a variable: ╔. However, I can only do it in a string, and if I use the ' notation it just shows this: �.
Is there anything I can do?
BTW, I use:
Linux (Mint)
Visual Studio Code (integrated terminal)
The console shows the characters correctly if I use the " notation, so probably it's not a problem with the console itself.
You can use the hex notation:
char border = '\xcd';
Small program:
#include <iostream>
using std::cout;
using std::cin;
int main()
{
cout << "border: \xcd\n";
const char corner = '\xc9';
cout << "Upper left corner: " << corner << "\n";
cout << "Paused. Press ENTER to continue.\n";
cin.ignore(100000, '\n');
return 0;
}
There are many charts that show an extending ASCII encoding. Use the hexadecimal value for the character that you need.
Here's a chart from Wikipedia about DOS extended ASCII table.

Stroustrup swan book Vector Problem

I'm using Stroustrup's swan book. I have run into a problem getting output from a
vector. I followed the text example from sec. 4.6.3 on page 121. I
managed to get the source compiled and am able to execute it. After
typing in a list of whitespace separated words, the program hangs and
does not list the elements of the vector as it should. I realize not
every element will be outputted if it is repeated, but i receive no
output at all. I have compiled and run this using the g++ 4.3.2
compiler on Linux and using the Visual C++ express 2008 compiler on
windows. Both produce the same result. Thank you for taking time to
read this. Here is my source:
#include "Supporting_files/std_lib_facilities.h"
int main()
{
vector<string> words;
string temp;
cout << "Enter a list of words: ";
while(cin>>temp)
words.push_back(temp);
cout << "Number of words: " << words.size() << endl;
sort(words.begin(),words.end());
for(int i=0;i<words.size();++i)
if(i==0||words[i-1]!=words[i])
cout << words[i] << "\n";
}
while(cin>>temp) only ends when it hits an end of file. Use control-D to send an end of file into the terminal.