?
Solved

do while

Posted on 2008-10-06
12
Medium Priority
?
192 Views
Last Modified: 2012-05-05
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
Comment
Question by:mikeregas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22656320
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 Comment

by:mikeregas
ID: 22656339
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22656350
               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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:mikeregas
ID: 22656357
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22656372
Thats because you are not reading the input string in the loop .... the fgets() should be inside the loop.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22656380
               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 Comment

by:mikeregas
ID: 22656403
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22656418
>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 Comment

by:mikeregas
ID: 22656440
           scanf(" %c", &choice);
        }while(choice !='y');
        getchar();
is this what I want?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22656462
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 Comment

by:mikeregas
ID: 22656474
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 22656495
\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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question