• Status: Solved
• Priority: Medium
• Security: Public
• Views: 199

# 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;
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;
}
``````
0
mikeregas
• 7
• 5
1 Solution

Commented:
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

Author 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

Commented:
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

Author 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;

}
``````
0

Commented:
Thats because you are not reading the input string in the loop .... the fgets() should be inside the loop.
0

Commented:
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

Author 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
{
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;
}
``````
0

Commented:
>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

Author Commented:
scanf(" %c", &choice);
}while(choice !='y');
getchar();
is this what I want?
0

Commented:
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

Author 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

Commented:
\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.

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.