Solved

do while

Posted on 2008-10-06
12
184 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
Comment Utility
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
Comment Utility
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
Comment Utility
               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 Comment

by:mikeregas
Comment Utility
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
Comment Utility
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
Comment Utility
               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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:mikeregas
Comment Utility
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
Comment Utility
>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
Comment Utility
           scanf(" %c", &choice);
        }while(choice !='y');
        getchar();
is this what I want?
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
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
Comment Utility
\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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now