0
I’m here again😂 Pls help me with my homework, guessing number( basic and not completed)the yellow part of code doesn’t work :|
https://code.sololearn.com/cgSaAj3ZDaYB/?ref=app For example , when u enter 2345 but the random number is 2357 The output must be : 2 green ( the digit is right and in right place) 3 green (the digit is right and in right place) 5 yellow (the digit is right but not in right place) 4 red. (the digit is not correct )
3 odpowiedzi
+ 2
You have to consider situation like 2355 guessed as 5375.
0
Gordon anyway it doesnt work!
0
The bugs never come alone, isn't it?
BUG 1: line 51: enteredNumberStr=std::to_string(enteredNumberInt);
but enteredNumberInt has been broken by the "while" loop at line 21
BUG 2: numbers tagged as red: must be removed from enteredNumberStr, else they will be presented again at third loop (the one detecting yellows) and will be tagged as yellow too.
I suggest to initialize "enteredNumberStr" at the beginning, and to remove the already tagged digits also in the first loop, not only in the second.
BUG 3: line 59
enteredNumberStr.erase(enteredNumberStr.begin() + counter2);
but enteredNumberStr becomes shorter at every loop, so the position (+counter2) is wrong and can furthermore produce traps.
I suggest to avoid of changing the string size but just replace the numbers with spaces: enteredNumberStr[counter2] = ' ';
here, when detecting greens, and in the previous loop which detects reds. At the end of both loops you must compact the spaces:
enteredNumberStr.erase(std::remove(enteredNumberStr.begin(), enteredNumberStr.end(), ' '),enteredNumberStr.end());
And you must return if all the 4 numbers have been replaced (else following code will trap):
if (enteredNumberStr.size() == 0) return 0;
BUG 4: all the '4' in the loops (lines 54 and 75) must be replaced with
enteredNumberStr.size(), because the string becomes shorter during the code.
BUG 5: enteredNumberStr, which contains the remaining numbers to check, must not modified at lines 35 and 77: use a temporary std::string instead.
OPTIMIZATION: the loop at line 54, the one wich detects the greens, requires just one counter, not two!
Happy homework!