i have a palimdone

how do I make the choices work so that when the user enters y it runs through the program again or n and it stops the program
#include <stdio.h>
#include <string.h>
 
void main()
{
	char strsrc[1064];
	char strtmp[1064];
	char choice;
	
	
	
	printf("Enter your string;\n");
	scanf("%s", &strsrc);
 
	strcpy(strtmp, strsrc);
	strrev(strsrc);
 
	if(strcmp(strsrc,strtmp)==0)
		printf("\nEntered string %s is a palindrome.\n", strsrc);
	else
		printf("\nEntered string %s is not a palindrome.\n",strsrc);
	
	printf("Enter another palindrome?\n y.\t or\t n.\n");
	scanf("%c", &choice);
	
	system("PAUSE");
	return 0;
}

Open in new window

mikeregasAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sunnycoderConnect With a Mentor Commented:
do {
    //do your processing
    //ask if user wants to continue doing it
} while (user said yes);
0
 
mikeregasAuthor Commented:
how do I get the choicce to work?
#include <stdio.h>
#include <string.h>
 
void main()
{
	char strsrc[1064];
	char strtmp[1064];
	int choice;
	char y = 1;
	char n = 0;
	
	
	do
	{
	printf("Enter your string;\n");
	scanf("%s", &strsrc);
 
	strcpy(strtmp, strsrc);
	strrev(strsrc);
 
	if(strcmp(strsrc,strtmp)==0)
		printf("\nEntered string %s is a palindrome.\n", strsrc);
	else
		printf("\nEntered string %s is not a palindrome.\n",strsrc);
	
	printf("Enter another palindrome?\n y.\t or\t n.\n");
	scanf("%c", &choice);
	}while(choice = y);
	system("PAUSE");
	return 0;
}

Open in new window

0
 
sunnycoderConnect With a Mentor Commented:
== is used for comparison ... = denotes assignment
char constants are enclosed in single quotes
choice == 'y'
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
sunnycoderConnect With a Mentor Commented:
You are also likely to face some weird input issues due to extra \n that is left in your input stream after reading the choice ... remember that user had entered y followed by a newline - this newline is still in your input buffer. If you find your scanf for reading string is not executing - it is reading the newline.
To get rid of this error, you can read a char after reading choice or read a line in case user had entered some more extra chars.
To make your program robust, preferred method is reading user input as string and then parsing, converting, and validating it before using it.
0
 
mikeregasAuthor Commented:
I must be missing something because it is not letting me choose y or n. It just say press any key to continue
0
 
sunnycoderConnect With a Mentor Commented:
:) ... Extra \n in the input buffer
0
 
mikeregasAuthor Commented:
I removed the \n and it is not running the while loop so it still says press any key to continue?
0
 
sunnycoderConnect With a Mentor Commented:
There would be a \n after your input string too. Post your latest code.
0
 
mikeregasAuthor Commented:
here is the code

        printf("Enter another palindrome?\n y. or n.");
      scanf("%c", &choice);

this part doesnt work
#include <stdio.h>
#include <string.h>
 
void main()
{
	char strsrc[1064];
	char strtmp[1064];
	int choice;
	char y = 1;
	char n = 0;
	
	
	do
	{
	printf("Enter your string;\n");
	scanf("%s", &strsrc);
 
	strcpy(strtmp, strsrc);
	strrev(strsrc);
 
	if(strcmp(strsrc,strtmp)==0)
		printf("\nEntered string %s is a palindrome.\n", strsrc);
	else
		printf("\nEntered string %s is not a palindrome.\n",strsrc);
	
	printf("Enter another palindrome?\n y. or n.");
	scanf("%c", &choice);
	}while(choice == 'y');
 
	system("PAUSE");
	return 0;
}

Open in new window

0
 
sunnycoderConnect With a Mentor Commented:
You havent removed any newlines!!
you getch()/getchar()/fgets() etc to remove it - from both the instances of scanf ... or you can also try matching it in the format string
scanf("%s \n", &strsrc);

0
 
mikeregasAuthor Commented:
I do not know of the getch() or the getchar() or fgets(), I also tried using the string format and then it crashed I commited it in the code sample below.
#include <stdio.h>
#include <string.h>
 
void main()
{
	char strsrc[1064];
	char strtmp[1064];
	int choice;
	char y = 1;
	char n = 0;
	
	
	do
	{
	printf("Enter your string;\n");
	scanf("%s", &strsrc);
 
	strcpy(strtmp, strsrc);
	strrev(strsrc);
 
	if(strcmp(strsrc,strtmp)==0)
		printf("\nEntered string %s is a palindrome.\n", strsrc);
	else
		printf("\nEntered string %s is not a palindrome.\n",strsrc);
	
	printf("Enter another palindrome?\n y. or n.");
	scanf("%c", &choice);
	//scanf("%s \n", &strsrc); I tried this one also the loop doesnt run
	}while(choice == 'y');
 
	system("PAUSE");
	return 0;
}

Open in new window

0
 
sunnycoderConnect With a Mentor Commented:
       printf("Enter your string;\n");
        scanf("%s\n", &strsrc);
 
http://linux.die.net/man/3/getchar

stdin refers to input stream
0
 
mikeregasAuthor Commented:
I do not know what I have missed, but when it runs the program the way it is coded. it does not give the user the option to enter 'y' or 'n', it just ends the program. any ideas or am I just completely missing it
0
 
tliottaConnect With a Mentor Commented:
You're getting confused by your code.

Create a totally different program that does nothing but loop and print 'OK' while the user enters 'y'. If the user enters something else, let the loop end and print 'not OK'.

That will let you focus on learning how to control the loop and process input.

Once you master those, take your current code and insert it in the loop.

There are some details about that last part to be complete, but at least you will have a very clear example of looping over input without having to be distracted by extra code.

Tom
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.

All Courses

From novice to tech pro — start learning today.