2

I was working on a problem which was fairly simple but I came across an issue and was uncertain why the program was not working the way I wanted it to. The problem asked us to make a Palindrome test with and use functions. I was able to do all of that but in the question it also asked us to make sure the user input was a 5 digit number. This is what I did in my code:

 do {
   printf("Please Enter a positive 5-digit number: "); //Ask & receive input
   scanf("%d", &number);
   countdigs = countDigits(number); //
   }while((countdigs != 5) && (number <0) );//keep going throught the loop while the number is less than 0 and is not equal to 5 digits    
 palindrome(number); //palindrome

this is my palindrome function

void palindrome (int number) //create palindrome procedure
{ 
 int remainder,reversedNumber=0,original; //initialize variables
 original = number; //set original number to a new var
 while (number != 0) //while the number is not 0
 {
  remainder = number %10; //get remainder of original number
  reversedNumber = (reversedNumber*10) + remainder; //construct reversed 
  number
  number = number / 10; //go to the next digit
 }
 //printf("OG = %d",original); //check
 //printf("rev = %d", reversedNumber); //check
 if (original == reversedNumber) //if the original number and reversed 
 number are the same
 {
  printf("The number %d is a palindrome.\n",original);  //print solution
 }
 else //if not the same
 {
  printf("The number %d is not a palindrome.\n",original); //print solution
 }
}

and this is my countDigits function

int countDigits(int number) //setup recursive function
{
 int number_length=0; //initialize number length
 while(number != 0) //while the number is not 0
 {
  number = number/10; //divide number by 10 to get to next digit
  number_length = number_length + 1; //increase counter by 1
 }  
 return number_length; //return the counter
 printf("length %d",number_length);  
}

The issue I am having is in this code (countdigs != 5) which is the check to make sure the number is a positive (works fine), and makes sure the number is of length 5. However, I could not get it to work.

The problem said not to use any string function (length function)

comp
  • 41
  • 6

1 Answers1

3

I guess the issue is at the very beginning:

do {
    printf("Please Enter a positive 5-digit number: ");
    scanf("%d", &number);
    countdigs = countDigits(number);
} while((countdigs != 5) && (number < 0) );

We should ask user for an input again if countdigs != 5 OR number is negative, i.e.:

do {
    printf("Please Enter a positive 5-digit number: ");
    scanf("%d", &number);
    countdigs = countDigits(number);
} while((countdigs != 5) || (number < 0) );
Andriy Berestovskyy
  • 8,059
  • 3
  • 17
  • 33
  • This worked thank you for the answer, I thought that if we used an OR operator that if one of the conditions was met then the program would continue. I think I learned that in a python class last year and it stuck in my head. Does C have it differently? or am I just lost haha – comp Feb 23 '18 at 15:49
  • @comp we can read it as `while` smth is true `OR` smth is true `do` { operators } – Andriy Berestovskyy Feb 23 '18 at 16:00