• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

do while

What am I doing wrong here, I need to ask if the user wants to enter another palindrome, the the user selects y or n. Obviously y=yes and n=no. I am having extreme difficulty with the loop can someone help me out here

#include <stdio.h>   
#include <stdlib.h>   
#include <string.h>
   
char palindrome(char *,int,int);
 
main()
{   
      char x[50],y[50]; /*I wrote it without using any functions I wasn't previously familiar with*/   
      int i=0,j,r=0,k,len; /*will try to use the functions when I find time*/
	  int choice;
      printf("Enter your string\n");   
      gets(x);  
      len=strlen(x);  
 
	  do 
	  {
		for (j=0;j<len;j++)
		{  
			if (x[j]>='A' && x[j]<='Z') /*turning caps into lowercase*/  
			y[r++]=x[j]-('A'-'a'); /*making new string stripped of everything but letters*/  
		
			if (x[j]>='a' && x[j]<='z') /*did this to simplify slightly, my task wasn't worded very clearly anyway*/  
			y[r++]=x[j];  
		 }  
      
		k=r-1;  
      
		if (palindrome (y,i,k)==1)  
		printf("You've got yourself a palindrome\n");  
      
		else if (palindrome (y,i,k)==0) printf ("Not a palindrome\n");  
		getchar();
 
		printf("Enter another palindrome? y or n\n");
		scanf("%d", &choice);
	  }while(choice !=1);
	  return 0;
 
}  
 
char palindrome(char a[],int i,int k)
{  
      if (i>=k) return 1;  
 
      else if (a[i]!=a[k]) return 0;  
 
      else if (a[i]==a[k]) return palindrome(a,i+1,k-1);  
 
      else return 0; 
}

Open in new window

0
mikeregas
Asked:
mikeregas
  • 7
  • 5
1 Solution
 
sunnycoderCommented:
scanf("%d", &choice);
          }while(choice !=1);

You are reading in an int!!! User enters y/n both of which are != 1 and your loops iterates again.

Do not use gets - use fgets instead

fgets(x, 50, stdin);
0
 
mikeregasAuthor Commented:
I am lost and I am closing in on my deadline very fast, so I am asking what am I doing wrong here because I dont know.
0
 
sunnycoderCommented:
               printf("Enter another palindrome? y or n\n");
                scanf("%d", &choice);
          }while(choice !=1);

User enters y ... You read it in as an int - which is a value > 1
User enters n ...  You read it in as an int - which is a value > 1

You check choice!=1 - which evaluates to true - so your loop executes again ...

Either you change your printf to ask user to enter 1 for exiting or you start reading in a char and compare it to 'y' or 'n' instead of 1.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mikeregasAuthor Commented:
this is what I have come up with so far and it ask the question and so long as I do not choose in the program doesnt end, but it doesnt loop back through

	  do 
	  {
		for (j=0;j<len;j++)
		{  
			if (x[j]>='A' && x[j]<='Z') /*turning caps into lowercase*/  
			y[r++]=x[j]-('A'-'a'); /*making new string stripped of everything but letters*/  
		
			if (x[j]>='a' && x[j]<='z') /*did this to simplify slightly, my task wasn't worded very clearly anyway*/  
			y[r++]=x[j];  
		 }  
      
		k=r-1;  
      
		if (palindrome (y,i,k)==1)  
		printf("You've got yourself a palindrome\n");  
      
		else if (palindrome (y,i,k)==0) printf ("Not a palindrome\n");  
		getchar();
 
		printf("Enter another palindrome? y or n\n");
		scanf(" %c", &choice);
	  }while(choice !='n');
	  //return 0;
 
}  

Open in new window

0
 
sunnycoderCommented:
Thats because you are not reading the input string in the loop .... the fgets() should be inside the loop.
0
 
sunnycoderCommented:
               else if (palindrome (y,i,k)==0) printf ("Not a palindrome\n");  
                getchar();  //-->> why do you have a getchar() here? This would make the user feel as if your program is stuck
 
                printf("Enter another palindrome? y or n\n");
                scanf(" %c", &choice);
//you would need a getchar() here to remove the extra \n in the buffer
0
 
mikeregasAuthor Commented:
I have have moved the do{ and got rid of the getchar() and it still will not execute the loop, I am totally lost I have been looking at do while loops online and they all look like mine and they work, I can not figure this one out.
#include <stdio.h>    
#include <string.h>
   
char palindrome(char *,int,int);
int main(void)
{   
     
	  char x[50],y[50]; /*I wrote it without using any functions I wasn't previously familiar with*/   
      int i=0,j,r=0,k,len; /*will try to use the functions when I find time*/
	  char choice;
 
	   do 
	  {
      printf("Enter your string\n");   
      fgets(x, 50, stdin);  
      len=strlen(x);  
 
	  
		for (j=0;j<len;j++)
		{  
			if (x[j]>='A' && x[j]<='Z') /*turning caps into lowercase*/  
			y[r++]=x[j]-('A'-'a'); /*making new string stripped of everything but letters*/  
		
			if (x[j]>='a' && x[j]<='z') /*did this to simplify slightly, my task wasn't worded very clearly anyway*/  
			y[r++]=x[j];  
		}  
      
		k=r-1;  
      
		if (palindrome (y,i,k)==1)  
		printf("You've got yourself a palindrome\n");  
      
		else if (palindrome (y,i,k)==0) printf ("Not a palindrome\n");  
		//getchar();
 
		printf("Enter another palindrome? y or n\n");
		scanf(" %c", &choice);
	  }while(choice !='y');
	  return 0;
 
}  
 
char palindrome(char a[],int i,int k)
{  
      if (i>=k) return 1;  
 
      else if (a[i]!=a[k]) return 0;  
 
      else if (a[i]==a[k]) return palindrome(a,i+1,k-1);  
 
      else return 0; 
}

Open in new window

0
 
sunnycoderCommented:
>while(choice !='y');
This is not what you want - check the condition again ...

from http:#22656380

                printf("Enter another palindrome? y or n\n");
                scanf(" %c", &choice);
//you would need a getchar() here to remove the extra \n in the buffer
0
 
mikeregasAuthor Commented:
           scanf(" %c", &choice);
        }while(choice !='y');
        getchar();
is this what I want?
0
 
sunnycoderCommented:
you tell me :) ...

Does having a getchar() AFTER the loop help remove the extra \n that came from reading in the choice? What happens if user chooses to enter another string - would the \n be removed in this case?

Also check your loop condition choice!=y ... do while loop executes as long as the condition in while is true. So choice != 'y' will make the loop execute as long as choice is not equal to 'y' and as soon as choice is 'y' - loop would exit.
0
 
mikeregasAuthor Commented:
no it is not running the loop, I dont know where or what I am looking for. The /n I am not sure what you are refering to. If I change the choice!='y' to choice!='n' doesnt seem to make a difference
0
 
sunnycoderCommented:
\n refers to newline character .... when you ask the user to input choice - he enters y or n followed by a newline ... you read the character but \n remains in the input buffer. When you try to read in the string for verification this \n is picked up from input buffer and handed over to your program - assuming that user simply pressed enter without entering a string at all.
This is the same problem that I had told you about in your previous question.

You have corrected the condition ... It should be != 'n' indeed. Once you have the getchar in place, your code should work fine. In case it doesnt post code here and I will verify
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now