When I paste or type a string that contains ~1000 characters into the console, ¿'s (upside down question marks) start appearing in the input. Once they start appearing, I can't delete the characters I've entered, and pressing enter yields even more ¿'s. The program only continues after hitting delete once and pressing enter again.
When I cout << the string obtained from the input , an amount of characters about equal to the number of ¿'s that appeared in the input are missing from the string. There were no missing characters in the input, just the strange extra ¿'s.
It's not an issue with unknown characters, as it happens even with simple letters and digits. This doesn't happen when the input is entered in pieces. Is there some sort of limit on how many characters can be entered into the console at once, or is this a different error? And can it be fixed? Thanks!
Related
I have an EditBox in a MFC-dialog. The user is supposed to enter a number. I'm trying to automatically add separators to the number while the user is inputting it:
When the number is more than 3 digits long, a separator is added between the hundreds and the thousands digit; a second one between the hundredthousands and the millions when it gets longer than 6 digits and so forth (so that 1234567 becomes 1,234,567 for example).
This is done in a function executed by ON_EN_CHANGE and basically works fine already. But the problem is that the caret position is set to the beginning of the EditBox once my function changes the length of the string in it, preventing continous typing.
I tried simulating the press of the end-key to send the caret to the end of the EditBox, which works as long as the user only enters a number from left to right. But it won't work when the user is trying to add, remove or edit digits in the middle of the number. I need the caret position at the exact spot of the number where it was before the user pressed a key.
I tried calculating the new caret position from the previous one (gotten with CEdit::GetSel()) and the previous length of the number:
OnEnChange()
{
int prevCursPos {HIWORD(m_editCtrl.GetSel())};
int prevStrLen {m_editCtrl.GetWindowTextLengthW()};
UpdateData(TRUE);
// Adding/Removing of separators as needed
UpdateData(FALSE);
int difference {m_editCtrl.GetWindowTextLengthW() - prevStrLen))};
if(difference > 0) // a separator has been added to the string
m_editCtrl.SetSel(-1, prevCursPos + 1);
}
However, the SetSel() function doesn't seem to have any effect. I also tried to send an EM_SETSEL message instead, but I couldn't figure out how to make that work either, the caret always resets itself to the beginning of the EditBox.
Does anyone have another idea on how to accomplish what I'm trying to do?
It is unclear how you are handling "/ Adding/Removing of separators as needed".
SetSel with the first parameter set to -1 will position the caret at the beginning of the string if the string changes after calling UpdateData(false)
Create CString type of the variable (m_csEdit for example) for this edit control and
int iLen = m_csDDx.GetLength();
m_editCtrl.SetSel(iLen, -1);
or
m_editCtrl.SetSel(iLen, iLen);
You can calculate the length differently from what I suggested.
Consider using masked edit control (maybe).
Forgot to mention. Use GetNumberFormatEx to format number with thousand delimiter.
I have a problem.
i want the user to enter a number in my c++ program, but during the input i want to prevent that he just presses enter without having made an input, thus creating a line break.
I have already solved the problem in another place where the user has to enter a character.
I have read the character with getchar, determined the position of the cursor with the ANSI escape sequences and provided the whole thing with a do while loop.
But since I want to read in a number between 0 and 250, getchar would not be suitable.
scanf and cin both wait for a valid input and cause these nasty line breaks.
I have already thought about using getchar anyway and storing the characters in a char array whose indices I then convert to the corresponding numbers which I then add up to the actual number which can then be stored again in an int variable.
But surely there is an easier alternative or?
Translated with www.DeepL.com/Translator (free version)
This line will flush the newline character
cin.ignore(256, '\n');
The programme is about getting the number of letters in words of your programme with their ratings to know which appeared most.example "I stay in Ohio" from the sentence we can see it is made up of a group of letters and we are to know the number times a letter appeared ahbd their ratings in percentage
You can create std::vector<std::pair> that contains the pair of information, first would be a letter and the second one would be the number how many they are in sentence. You can go through each char in sentence and check if it is equal 0x0 if not, then add the letter to first field in std::pair in std::vector and count how many the letter appears in sentence changing the letter with 0x0. After that go to the next letter until the sentence ends. There you will have a vector containing pairs of information: first - the letter, second - how many times it appeared.
This is the problem that I am attempting to solve:
Ask the user to enter a bunch of words, as many as they want, until they enter a 0. After
that, count how many times each letter appears across all the words, and print out a list of each letter and how many times it appears. Example:
Enter word> hello
Enter word> lemon
Enter word> goodbye
Enter word> 0
Letter: h appears 1 times
letter: e appears 3 times
...
So far I have to put all the words together, and have made comparisons. The problem lies in that, after all the words are put together, and 0 is input, I cannot count each invidual character within the combined string. I did some research, and I've read that to perform this you need vectors, but i do not understand how to use them.
I've been trying at it for a week to get it right, but to no avail. C++ is sort of different from all the other language I have learned (at least for me).
You can use an std::unordered_map, with the characters as key and the counter as value. For each string you read, just iterate over it and increase the value corresponding to the character in the map.
This way you don't actually need to store the words.
I am trying to create something (I'm thinking probably a loop?) that will allow me to have the user enter several numbers and then enter something like "done" to get all the numbers added up.
for example if I have a float (call it x for now) they could enter "7 enter 5 enter 9 enter done enter" and it would add those numbers and make x that value. Where I am coming into problems is that I need the user to be able to be able to enter as many numbers as they want (between 1 and 70 as an example) without specifying how many numbers they want to enter, just entering something in when they are done.
Thank you all
You'll need to use an infinite loop (while (true) or for (;;)) to read the next input into a string.
Check if the string is done. If so, break the loop.
Then try to parse that string into a double (don't use float) with the function std::stod.
If the parsing fails, optionally print an error message like "Bad input, try again" and restart the loop. If the parsing succeeds, add the number to the counter and restart the loop.