Thread: I've been stuck at this for a while

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    I've been stuck at this for a while

    The following program works when valid input is entered. When invalid input is entered it goes into a loop and stops working. I have included the code along with how I want the code to behave when invalid input is entered.
    I've been stuck at this for a while-screenshot-50-png
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define MAX_QUERIES 12
    #define N_DIGITS 4
    
    int isValid(int N){
            //Create an array each index represent a digit 
            int digit[10]={0};
            int lastDigit = 0;
            //If Number is 4 digit 
            
            int lower = 1;
            int upper = 9;
            int x = 0;
            for(;x<(N_DIGITS-1);x++){
                    upper=upper*10+9;
                    lower=lower*10;
            }
            
            if(N>=lower && N<upper){
                            //Iterate through each digit in N
                            while(N>0){
                                    lastDigit = N%10;
                                    //if digit is 0 then N is not valid
                                    if(lastDigit==0)
                                            return 0;
                                    //if this digit is already seen then that means digit is repeated 
                                    if(digit[lastDigit]==1)
                                            return 0;
                                    //if not seen earlier then mark it as seen
                                    digit[lastDigit]=1;
                                    N=N/10;
                            }
                            return 1;
            }
            else
                    return 0;
    }
    
    //Choose a 4 digit valid number 
    int choose_N(){
            int N=0;
            while(1){
                    N = rand();
                    if(isValid(N))
                            return N;
            }
            return -1;
    }
    
    int matches(int N,int M){
            int dN=0;
            int dM=0;
            int m=0;
            while(N>0 && M>0){
                    dN=N%10;
                    dM=M%10;
                    //if both the last digit of N and M are same them increment the count of match
                    if(dN==dM)
                            m++;
                    N=N/10;
                    M=M/10;
            }
            return m;
    }
    
    int hits(int N,int M){
            //Array to track what all digits are there in N 
            int digitsN[10]={0};
            int lastDigit = 0;
            int i=1;
            int hit=0;
    
            while(N>0){
                    lastDigit=N%10;
                    //index at lastDigit in digitsN is seen so adding the index from right side e.g 4920 has index as 4321
                    digitsN[lastDigit]=i++;
                    N=N/10;
            }
            i=1;
            
            while(M>0){
                    lastDigit=M%10;
                    //Check if last digit of M is somewhere in the N but not at the same index
                    if(digitsN[lastDigit]!=0 && digitsN[lastDigit]!=i)
                            hit++;
                    M=M/10;
                    i++;
            }
            return hit;
    }
    
    int main(){
            srand(time(0));
            int N = choose_N();
            //printf("Number guessed = %d\n",N);
            printf("***Welcome to the MATCH and HIT game***\n");
            printf("The computer has selected a 4-digit number.\n");
            printf("Try to deduce it in 12 rounds of queries.\n");
            int round=1;
            int M=0;
            int numOfInputRead=0;
    
            while(round<=MAX_QUERIES){
                    printf("\nRound #%d\n",round);
                    while(numOfInputRead==0){
                            fflush(stdin);
                            printf("Please enter your query (4 digits):");
                            numOfInputRead = scanf("%d",&M);
                            if(numOfInputRead==1 && isValid(M)==1)
                                    break;
                            else{
                                    printf("Invalid number. Please try again!\n");
                                    numOfInputRead=0;
                            }
                    }
                    
                    numOfInputRead=0;
                    int m = matches(N,M);
                    int h = hits(N,M);
                    printf("\n-> %d match and %d hits ",m,h);
                    
                    if(m==N_DIGITS){
                            printf("\n**********************************\n");
                            printf("CONGRATULATIONS! You won the game!\n");
                            printf("**********************************\n");
                            return 0;
                    }
                    round++;
            }
            printf("\n**********************************\n");
            printf("Sorry, out of queries. Game over!\n");
            printf("**********************************\n");
            return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,419
    > fflush(stdin);
    This doesn't do what you think it does. Remove it.


    Code:
    if(numOfInputRead==1 && isValid(M)==1)
        break;
    else if (numOfInputRead == 0) {
        printf("Invalid number. Please try again!\n");
        // cleanup
        int ch;
        while ( (ch=getchar()) != EOF && ch != '\n' )
            ; // nothing to do
        numOfInputRead=0;
    } else {
        // user signalled EOF by pressing ctrl-z or ctrl-d
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    So, I did this and now it does not compile.


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define MAX_QUERIES 12
    #define N_DIGITS 4
    
    
    int isValid(int N){
            //Create an array each index represent a digit 
            int digit[10]={0};
            int lastDigit = 0;
            //If Number is 4 digit 
            
            int lower = 1;
            int upper = 9;
            int x = 0;
            for(;x<(N_DIGITS-1);x++){
                    upper=upper*10+9;
                    lower=lower*10;
            }
            
            if(N>=lower && N<upper){
                            //Iterate through each digit in N
                            while(N>0){
                                    lastDigit = N%10;
                                    //if digit is 0 then N is not valid
                                    if(lastDigit==0)
                                            return 0;
                                    //if this digit is already seen then that means digit is repeated 
                                    if(digit[lastDigit]==1)
                                            return 0;
                                    //if not seen earlier then mark it as seen
                                    digit[lastDigit]=1;
                                    N=N/10;
                            }
                            return 1;
            }
            else
                    return 0;
    }
    
    
    //Choose a 4 digit valid number 
    int choose_N(){
            int N=0;
            while(1){
                    N = rand();
                    if(isValid(N))
                            return N;
            }
            return -1;
    }
    
    
    int matches(int N,int M){
            int dN=0;
            int dM=0;
            int m=0;
            while(N>0 && M>0){
                    dN=N%10;
                    dM=M%10;
                    //if both the last digit of N and M are same them increment the count of match
                    if(dN==dM)
                            m++;
                    N=N/10;
                    M=M/10;
            }
            return m;
    }
    
    
    int hits(int N,int M){
            //Array to track what all digits are there in N 
            int digitsN[10]={0};
            int lastDigit = 0;
            int i=1;
            int hit=0;
    
    
            while(N>0){
                    lastDigit=N%10;
                    //index at lastDigit in digitsN is seen so adding the index from right side e.g 4920 has index as 4321
                    digitsN[lastDigit]=i++;
                    N=N/10;
            }
            i=1;
            
            while(M>0){
                    lastDigit=M%10;
                    //Check if last digit of M is somewhere in the N but not at the same index
                    if(digitsN[lastDigit]!=0 && digitsN[lastDigit]!=i)
                            hit++;
                    M=M/10;
                    i++;
            }
            return hit;
    }
    
    
    int main(){
            srand(time(0));
            int N = choose_N();
            //printf("Number guessed = %d\n",N);
            printf("***Welcome to the MATCH and HIT game***\n");
            printf("The computer has selected a 4-digit number.\n");
            printf("Try to deduce it in 12 rounds of queries.\n");
            int round=1;
            int M=0;
            int numOfInputRead=0;
    
    
            while(round<=MAX_QUERIES){
                    printf("\nRound #%d\n",round);
                    while(numOfInputRead==0){
                            printf("Please enter your query (4 digits):");
                            numOfInputRead = scanf("%d",&M);
                            if(numOfInputRead==1 && isValid(M)==1)
                                break;
                            else if (numOfInputRead == 0)
                            {
                                printf("Invalid number. Please try again!\n");
                                int ch;
                                while ( (ch=getchar()) != EOF && ch != '\n' )
                                ; // nothing to do
                                numOfInputRead=0;
                            }
                            else
                            {
                                
                            }
                            
                                    
                                    
                            }
                    }
                    } 
                    
                    numOfInputRead=0;
                    int m = matches(N,M);
                    int h = hits(N,M);
                    printf("\n-> %d match and %d hits ",m,h);
                    
                    if(m==N_DIGITS){
                            printf("\n**********************************\n");
                            printf("CONGRATULATIONS! You won the game!\n");
                            printf("**********************************\n");
                            return 0;
                    }
                    round++;
            }
            printf("\n**********************************\n");
            printf("Sorry, out of queries. Game over!\n");
            printf("**********************************\n");
            return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,419
    Well sloppy indentation means you lose track of where all the open / close braces are supposed to go.

    Your 'main' really ends at line 126.
    Figure out where your braces should be.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define MAX_QUERIES 12
    #define N_DIGITS 4
    
    int isValid(int N)
    {
      //Create an array each index represent a digit
      int digit[10] = { 0 };
      int lastDigit = 0;
      //If Number is 4 digit
    
      int lower = 1;
      int upper = 9;
      int x = 0;
      for (; x < (N_DIGITS - 1); x++) {
        upper = upper * 10 + 9;
        lower = lower * 10;
      }
    
      if (N >= lower && N < upper) {
        //Iterate through each digit in N
        while (N > 0) {
          lastDigit = N % 10;
          //if digit is 0 then N is not valid
          if (lastDigit == 0)
            return 0;
          //if this digit is already seen then that means digit is repeated
          if (digit[lastDigit] == 1)
            return 0;
          //if not seen earlier then mark it as seen
          digit[lastDigit] = 1;
          N = N / 10;
        }
        return 1;
      } else
        return 0;
    }
    
    //Choose a 4 digit valid number
    int choose_N()
    {
      int N = 0;
      while (1) {
        N = rand();
        if (isValid(N))
          return N;
      }
      return -1;
    }
    
    int matches(int N, int M)
    {
      int dN = 0;
      int dM = 0;
      int m = 0;
      while (N > 0 && M > 0) {
        dN = N % 10;
        dM = M % 10;
        //if both the last digit of N and M are same them increment the count of match
        if (dN == dM)
          m++;
        N = N / 10;
        M = M / 10;
      }
      return m;
    }
    
    int hits(int N, int M)
    {
      //Array to track what all digits are there in N
      int digitsN[10] = { 0 };
      int lastDigit = 0;
      int i = 1;
      int hit = 0;
    
      while (N > 0) {
        lastDigit = N % 10;
        //index at lastDigit in digitsN is seen so adding the index from right side e.g 4920 has index as 4321
        digitsN[lastDigit] = i++;
        N = N / 10;
      }
      i = 1;
    
      while (M > 0) {
        lastDigit = M % 10;
        //Check if last digit of M is somewhere in the N but not at the same index
        if (digitsN[lastDigit] != 0 && digitsN[lastDigit] != i)
          hit++;
        M = M / 10;
        i++;
      }
      return hit;
    }
    
    int main()
    {
      srand(time(0));
      int N = choose_N();
      //printf("Number guessed = %d\n",N);
      printf("***Welcome to the MATCH and HIT game***\n");
      printf("The computer has selected a 4-digit number.\n");
      printf("Try to deduce it in 12 rounds of queries.\n");
      int round = 1;
      int M = 0;
      int numOfInputRead = 0;
    
      while (round <= MAX_QUERIES) {
        printf("\nRound #%d\n", round);
        while (numOfInputRead == 0) {
          printf("Please enter your query (4 digits):");
          numOfInputRead = scanf("%d", &M);
          if (numOfInputRead == 1 && isValid(M) == 1)
            break;
          else if (numOfInputRead == 0) {
            printf("Invalid number. Please try again!\n");
            int ch;
            while ((ch = getchar()) != EOF && ch != '\n');  // nothing to do
            numOfInputRead = 0;
          } else {
    
          }
        }
      }
    }
    
    numOfInputRead = 0;
    int m = matches(N, M);
    int h = hits(N, M);
    printf("\n-> %d match and %d hits ", m, h);
    
    if (m == N_DIGITS) {
      printf("\n**********************************\n");
      printf("CONGRATULATIONS! You won the game!\n");
      printf("**********************************\n");
      return 0;
    }
    round++;
    }
    
    printf("\n**********************************\n");
    printf("Sorry, out of queries. Game over!\n");
    printf("**********************************\n");
    return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    I'm sorry but I just cannot seem to find the error.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,419
    > I'm sorry but I just cannot seem to find the error.
    You count each opening brace, and each closing brace.
    When one isn't in a place you expect, you've found a problem.

    You can do yourself a big favour by not trying to write so much code in main.
    Eg.
    Code:
    void introduction() {
      printf("***Welcome to the MATCH and HIT game***\n");
      printf("The computer has selected a 4-digit number.\n");
      printf("Try to deduce it in 12 rounds of queries.\n");
    }
    
    int choose_M() {
      int numOfInputRead, M;
      while (numOfInputRead == 0) {
        printf("Please enter your query (4 digits):");
        numOfInputRead = scanf("%d", &M);
        if (numOfInputRead == 1 && isValid(M) == 1)
          break;
        else if (numOfInputRead == 0) {
          printf("Invalid number. Please try again!\n");
          int ch;
          while ((ch = getchar()) != EOF && ch != '\n');  // nothing to do
          numOfInputRead = 0;
        } else {
    
        }
      }
      return M;
    }
    
    
    int main()
    {
      srand(time(0));
      int N = choose_N();
      //printf("Number guessed = %d\n",N);
      int round = 1;
      int M = 0;
      int numOfInputRead = 0;
    
      introduction();
      while (round <= MAX_QUERIES) {
        printf("\nRound #%d\n", round);
        M = choose_M();
      }
    }
    Hopefully, you might see now how to add the rest of your code to main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Got stuck --> need help
    By FrozenMind in forum C Programming
    Replies: 3
    Last Post: 10-26-2010, 02:53 AM
  2. Stuck and need help.
    By tvred in forum C++ Programming
    Replies: 4
    Last Post: 03-23-2010, 11:34 AM
  3. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  4. Stuck
    By goosematt in forum C Programming
    Replies: 1
    Last Post: 11-10-2003, 07:44 AM
  5. Stuck with '\b'
    By pratip in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2003, 07:49 AM

Tags for this Thread