I'm trying to make a C++ program to read a password.
I made the program to cout *, not the characters I write but my problem is when I want to delete character because they're wrong.
Example:
My constant password is 12345
If I enter 1235 the program will show **** and I have to delete the last character. It's simple to remove it from the string but I want the last * to disappear from the console just as it happens when you introduce you windows password.
Is it possible? If it is, can someone explain how?
Outputting the backspace character '\b' may help to move the output point back.
Specifically, outputting the string "\b \b" should blank out the last character output.
printf("\b ");
This statement surely works because after the cursor goes one character back and space given in above printf statement will overwrite the printed character on console output.
When I am writting to console using putch function ( from conio.h ) to simulate backspace key
simple
std::cout << '\b';
or
printf("\b ");
not works
I have to write:
cout << '\b' << " " << '\b';
or
putch('\b');
putch(' ');
putch('\b');
Try backspace \b or erase the whole line and print it again.
Sipmly write the '\b' character to stdout std::cout<<"\b". If you are using cpp or printf("\b") for pure C
I faced the same thing recently, using the \b was** moving the cursor backward** but it was not deleting the char (or * ). I realize after that the next character you enter after \b is gonna replace the (character to be deleted). So, I came up with something like this
printf(“\b \b”);
using the space key after \b will replace the character to be deleted and again using the \b will take you one position back again (to the position where was incorrect character was, but this time the character will not be there) This way your output will look smooth. hope it will be helpful to you
Related
I have this project where we have to convert a made up simple coding language given through a txt into a c++ language and currently I am having a problem with checking my work through linux (where it will be tested on). On the other hand Visual Studio where I wrote my code works how it should be.
From what I observed something weird is going on when I am taking each line in the text, separating each word and placing it in a vector. How I do this is by starting with an empty string, looking at each character of the line, and when I see a space or a tab or any kind of character that signifies a separator then I push back that string to a vector then make the string empty again for the next characters. So something line "STR man = bloo" turns into <"STR", "man", "=", "bloo"> and so on.
In visual studio it worked like this, but it seems like in linux.. somehow the second element seems to add a weird character which would be "man ". Its not a space or an empty line and when I tried to look at size with a format std::cout << name << ":" << size << std::endl, it couts not the right format or result.
enter image description here
enter image description here
Looking at visual Studio.
first line: "size" how many elements in that vector.
second line: is a name that I want to check if its already in a vector along with the length of that name
third line: shows all the elements in that vector (which should only show 1 since the size is 1) along with its length as well.
Looking at the linux result
-first line: "size" equals 1. Which is right
second line: the name im checking and its length, which is right
third line: is where the problem occurs. The name is 1 character greater than what it is suppose to be and instead of adding onto whats printed out, it replaces the first 3 letters instead?
fourth line: should not even happen because the size is only 1 and its a basic for loop of (int i = 0; i < size; i++)
Ive tried to use brute force and just pop back the last character of the second element causing my visual studio code to fail the tests but that still fails the conditional of if the name is = to the name in the vector (which should be true) in linux. Whatever character I add on the second element when I cout that value, such as << name << "." << endl; it seems to always go and replaces first letters of that string.
Pls help
Sorry for the delay, the problem was that I was not handling invisible characters such as \r.
Sorry, the wording for the actual question is probably wrong. I have a program that reads in a line from a .txt file and then puts the string into an object to compare it to a string entered by the user. I haven't been able to get it to match, and when I've tried to see what is entered, I don't see much. Maybe there's an invisible character denoting the end of the line? I've tried code like this:
std::cout << "...." << table[row][col]->get() << "...." <<std::endl;
And got
....a
as the result. When reading the file I used std::getline() if that makes a difference.
I didn't find a true fix, although I did see that the length of the read-in string was one int longer than the actual word. I was able to use a substring to cut the end off of the string.
So I have a code in Qt that goes joins the Strings “john جونسون” and “(جيد جداً), but when i add them up I get the answer in a wrong order ex.:
john (جيد جداً) جونسون’
This has nothing to do with Qt. It is a unicode thing. Qt just adds the characters.
The problem arises because the string starts of in LTR(left to right), with 'jhon' because that is in latin alphabet, but then when you add an arabic word to that, the first letter of that word, should be on the right, because arabic is a RTL script. This means that the last letter (represented by the last bytes) is on the left. So the place where the second string got added is - in memory - the end of the string.
You add an Arabic string to the string, because Arabic also uses '(', and thus, you stay in RTL mode.
So you need to explicitly mark the switchover back to LTR:
QString ltr{"\u200e"};
QString a {"john جونسون"};
QString b {"(جيد جداً)"};
std::cout << (a+ltr+b).toStdString()<< std::endl;
This will add a Left to right zero width character in between, which tells, whatever is displaying your string, from that point onwards, the end of the string is on the right again. (Until it reaches the arabic characters again.)
I was trying to test my classes when I encountered a weird problem in the input of test cases.
I tried to simplify the input to see what went wrong so I created the program below.
#include <iostream>
#include <string>
int main()
{
std::string number;
while (std::getline(std::cin, number))
{
std::cout << std::string(number) << " ";
}
}
Basically, I am getting each line of text and storing it in a string variable using getline(). Then I display each string using std::cout and append a single space character.
My input file contains this:
one six
one seven
The expected output should be like this:
one six one seven
But instead, I get this:
one seven
That is a space character followed by the second line of the input. It disregards the first line of input. I know for a fact that each line are being read properly because they were correctly displayed when I replaced the code with this:
std::cout << std::string(number) << std::endl;
This error is quite new to me. What's happening here? Can anybody explain? TIA!
Ok, its clear.
Your input file must be : one six\r\ntwo seven\r\n with normal Windows EOL.
When you read it under cygwin, you get in first read one six\r, only the \n being eaten by getline, and same one seven\r on the second line.
So you write : one six\r one seven\r (with an ending blank). But the \r alone put the curson back in first column of same line and second line erases first.
And normally the problem is not visible if you replace the ending blank by a std::eol that puts the cursor on a new line. The tab (\t) if really a special case : it put the cursor on eighth column exactly where you expect it, but by pure chance. If you invert the two lines it would be more apparent because you would see the remaining of first line at end of second.
You can confirm it by writing the output to a file and editing it.
I could reproduce it under Linux with a Windows EOL. The reason for that is that Cygwin closely mimics Unix-Linux and use Unix EOL convention of only \n.
I am a beginner for C++ so I'm sorry if this question sounds stupid..
I made this little program to help me get familiar with the properties of cin:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string next;
cout<<"Enter your input.\n";
cin>>next;
cout<<next;
return 0;
}
When I typed in \n from the keyboard input, I was returned \n.
Also, when I changed the variable next from a string to a character and gave it the same input as above, I was returned only a \.
My question is: Why am I not returned with a new line instead? Doesn't cin recognize \n type in from keyboard as a newline character? Or is it just applicable to cout?
\n is an escape sequence in C++; when it appears in a character constant or a string literal, the two character sequence is replaced by the single character representing a new line in the default basic encoding (almost always 0x0A in modern systems). C++ defines a number of such escape sequences, all starting with a \.
Input is mapped differently, and in many cases, depending on the device. When reading from the keyboard, most systems will buffer a full line, and only return characters from it when the Enter key has been pressed; what he Enter key sends to a C++ program may vary, and whether the file has been opened in text mode or binary mode can make a difference as well—in text mode, the C++ library should negotiate with the OS to ensure that the enter key always results in the single character represented by \n. (std::cin is always opened in text mode.) Whether the keyboard driver does something special with \ or not depends on the driver, but most don't. C++ never does anything special with \ when inputting from a keyboard (and \n has no special meaning in C++ source code outside of string literals and character constants).
If you need your program to recognize \n as a new line character at input you can check this out:
https://stackoverflow.com/a/2508814/815812
What Michael say is perfectly correct.
You can try out in similar way.
Technically speaking, this depends on things outside your program, but assuming your terminal simply passes the individual bytes corresponding to the '\' and 'n' characters (which I think any sane one will), then the behavior you're seeing is expected.
"\n" is nothing more than a shortcut added to the programming language and environment to let you more easily represent the notion of the ASCII return key. It's not a character itself, just a command to tell the program to generate a non-printable character that corresponds to pressing the Enter key.
Let's say you're in Notepad or whatever and you press the Tab key. It tabs over a spot. Typing "\t" just enters the literal characters "\" and "t". Internally, whoever wrote Notepad had to say what it should do when the user pressed Tab, and he did so by using the mnemonic like
if(key == '\t') {
// tab over
}