+ 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
18 ответов
+ 5
What do u need ?
+ 1
Farry but this doesn‘t work right!
+ 1
brin ~ what do u mean? did u read the que body?🙁
+ 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.
+ 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
+ 1
just type 2, it will show "2 green"
+ 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 !
+ 1
Bilbo Baggins can u plz check this code ?
+ 1
alright deleted it
+ 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.
+ 1
Dennis thanks for your advises 😻💜 and can help me to fix this code ? to run right?
+ 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
+ 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)
+ 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.
0
Dennis it is totally confusing I can’t understand any statement of your code
0
I need some help please
0
I'm new
0
Bandije how can I help u?