Anyone know how to do this?

anyone know how to do this?
I'm having hard time in my class.(c++)

Modify the following program so that when run, it requires the user to enter
the digits in the order shown in main(). The user enters the digits one at a
time pressing Enter after they've entered a single digit. Write the
requireDigit function so that it prompts the user for a digit, and checks if it
is correct. If it is correct, the function should do nothing, otherwise it
should tell the user they messed up and to try again. The function should loop
until the user enters the correct number::

#include <iostream>
#include <string>
using namespace std;


int main()
{
const string SECRET_DATA = "0001101010100100101001";
requireDigit(9);
requireDigit(3);
requireDigit(4);
requireDigit(1);
cout << SECRET_DATA << endl;
}
Last edited on
Where is your implementation of the requireDigit function?

Maybe you want to have a look at this command and the example given:
http://www.cplusplus.com/reference/cstring/strcmp/

And when posting code, try to use the code tag. Just simply copy and past your code, then select the code and click the "<>" button next to the text field.
Since SECRET_DATA contains just 0's and 1's you'd also need to restrict the maximum incorrect entries a user gets, otherwise it would be too easy to hack:

1. store SECRET_DATA in a container like vector
2. enter the password one digit at a time, if the digit entered is not equal to the corresponding element of the vector, it's a mistake, so (a) increase the mistake count by 1 and (b) ask user to re-enter the particular password element again
3. if correct digit is entered, move on to the next password element
4. if in-correct digit is entered, repeat 2 until max_mistakes occur in which case exit program, or,
5. if all digits are entered correctly before reaching max_mistakes, declare Success, exit program (in this case) or go on to do something else if you wish:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<vector>
using namespace std;
constexpr auto max_mistakes = 4;// C++11, can use const int instead for previous compiler versions //

int main(){
vector<int>password {0,0,0,1,1,0,1,0,1,0,1,0,0,1,0,0,1,0,1,0,0,1};
int digit = 0;
int mistakes = 0;
int i ;

for (int i = 0; i < password.size(); i++){
    do{
        cout<<"Please enter password character "<<(i+1)<<endl;
        cin>> digit;
            if(digit != password[i]){
                mistakes++;
               }
        if (mistakes == max_mistakes){
                break; // quits do-while loop // 
            }
        }
	while (digit != password[i]);

    if(mistakes == max_mistakes){
        cout<<"\nSorry, you've reached the max no of unsuccessful attempts"<<endl;
        break; // quits for loop //
        }
   }
   if(mistakes != max_mistakes){
        cout<<"\nSuccess!!!"<<endl;
   }
}
Last edited on
Topic archived. No new replies allowed.