Can u help me with a part of guessing number project? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Can u help me with a part of guessing number project?

computer must give a random number and user Must enter a number and if any digit of the number that user have entered was correct , computer must print <<that digit and „green“ as a word>> but this code doesn’t work right :( https://code.sololearn.com/cQHGppJX6LSW/?ref=app

27th May 2019, 3:45 PM
SaraAdib
SaraAdib - avatar
18 Answers
+ 5
What do u need ?
27th May 2019, 4:02 PM
bb013
bb013 - avatar
+ 1
Farry but this doesn‘t work right!
27th May 2019, 4:14 PM
SaraAdib
SaraAdib - avatar
+ 1
brin ~ what do u mean? did u read the que body?🙁
27th May 2019, 4:15 PM
SaraAdib
SaraAdib - avatar
+ 1
SaraAdib what doesnt work right? it shows "green" when the user enters a matching digit. replace 20 with rand()%20 to generate random number < 20. i did 20 to check it works.
27th May 2019, 4:26 PM
Farry
Farry - avatar
+ 1
Farry no , it is not what I wanted , it shows green when all the digits are right! but I want it to shows green for every digits that is right! and demonstrates that digit
27th May 2019, 4:41 PM
SaraAdib
SaraAdib - avatar
+ 1
just type 2, it will show "2 green"
27th May 2019, 4:42 PM
Farry
Farry - avatar
+ 1
Farry this program is going to be a guessing number project in future when it is completed! so there are tow more colors that I didn‘t mention yet! red and yellow , yellow shows that this digit is right but not in the right place , and red shows that this digit isn‘t in the random number, ok? so this code is useless for that project , cause it shows green ! when the numbers are same but I want it to show green for every digit that is right and is in the right place !
27th May 2019, 4:46 PM
SaraAdib
SaraAdib - avatar
+ 1
Bilbo Baggins can u plz check this code ?
27th May 2019, 4:47 PM
SaraAdib
SaraAdib - avatar
+ 1
alright deleted it
27th May 2019, 4:50 PM
Farry
Farry - avatar
+ 1
Wow m8, I know you didn't ask for readability tips but please. - Indent your code. - Don't declare every variable at the beginning of the program, declare and define them when you need it. - Keep consistent variable names, why the random capital R in Random, why another random without a capital? And the random capital U in remainerOfEnteredNUmber. - d, a, f? Please use meaningful names. - "#include <bits/stdc++.h> //for size of array". It includes every header out there. In my case it even fails to compile because the <filesystem> requires an addition option. Why would you need the filesystem header here anyway? - *(&remainerOfEnteredNUmber + 1) - remainerOfEnteredNUmber. ??? Don't do that, please. That's where std::size is for. - Use a class or at least functions if you haven't learned a class yet. + You're using std:: That's good. :) You're only demotivating others from even reading it otherwise, not to mention yourself in a few days... or hours.
27th May 2019, 4:54 PM
Dennis
Dennis - avatar
+ 1
Dennis thanks for your advises 😻💜 and can help me to fix this code ? to run right?
27th May 2019, 5:06 PM
SaraAdib
SaraAdib - avatar
+ 1
I suggest erasing it, rethinking your method and trying the readability tips. Sometimes starting over is faster than fixing it. You're kinda overcomplicating things. Farry linked you a good example, that's gone now. As a tip: Use a string instead of an array of ints. It makes it much easier. And here something to get you started, as an example: #include <iostream> #include <algorithm> #include <string> /* Takes user input and returns a string with the length of its parameter prepended with '0's */ std::string getInput( int length ) { std::string input; bool errorFlag = false; do { if( errorFlag ) { std::cerr << "Incorrect input\n"; } std::cin >> input; input = input.substr( 0, length ); // Cuts string to x characters input.insert( 0, length - input.size(), '0' ); // Prepends '0's errorFlag = true; }while( std::count_if( input.begin(), input.end(), ::isdigit ) != length ); return input; } int main() { constexpr int Length = 4; std::string code = somerandomstring; std::string input = getInput( Length ); std::cout << input << std::endl; // Rest of the stuff } Also these links might be of use: https://en.cppreference.com/w/cpp/string/basic_string https://en.cppreference.com/w/cpp/algorithm
27th May 2019, 5:52 PM
Dennis
Dennis - avatar
+ 1
IMHO the best way to learn is try to understand why your code does not work. And the best way to understand it, is to put prints everywhere. Two bugs, just to start... (1) in the loop which gets the digits from the user, you do not initialize the loop variable j to 0 (2) it is not correct to loop from 0 to 3: in that case the digits are reversed (for example if the user entered 1234, the digits will be 4 3 2 1)
27th May 2019, 6:17 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
SaraAdib In due time, m8. :) Go line by line and just ask/google what it means. Dealing with code outside your 'vocabulary' is a great way to learn after all as is using the c++'s reference material. Using the "rubber duck" ( google it ) debugging approach, or just by pasting a bunch of couts everywhere is a good way to see what's going on.
28th May 2019, 2:05 PM
Dennis
Dennis - avatar
0
Dennis it is totally confusing I can’t understand any statement of your code
28th May 2019, 10:24 AM
SaraAdib
SaraAdib - avatar
0
I need some help please
28th May 2019, 8:46 PM
Bandije
0
I'm new
28th May 2019, 8:46 PM
Bandije
0
Bandije how can I help u?
29th May 2019, 5:09 AM
SaraAdib
SaraAdib - avatar