Solved

do while

Posted on 2008-10-06
12
188 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
  • 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 

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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
Arduino EDI - Programming language 3 103
Assigning default value to structure in C for mutithread application 17 71
What is sub-make ? 2 71
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 opening and writing to files in the C programming language.

809 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