Solved

i have a palimdone

Posted on 2008-10-02
15
204 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:mikeregas
  • 8
  • 6
15 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 400 total points
ID: 22624096
do {
    //do your processing
    //ask if user wants to continue doing it
} while (user said yes);
0
 

Author Comment

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

Assisted Solution

by:sunnycoder
sunnycoder earned 400 total points
ID: 22624257
== is used for comparison ... = denotes assignment
char constants are enclosed in single quotes
choice == 'y'
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 400 total points
ID: 22624353
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
 

Author Comment

by:mikeregas
ID: 22624350
I must be missing something because it is not letting me choose y or n. It just say press any key to continue
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 400 total points
ID: 22624363
:) ... Extra \n in the input buffer
0
 

Author Comment

by:mikeregas
ID: 22624432
I removed the \n and it is not running the while loop so it still says press any key to continue?
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 400 total points
ID: 22624469
There would be a \n after your input string too. Post your latest code.
0
 

Author Comment

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

Assisted Solution

by:sunnycoder
sunnycoder earned 400 total points
ID: 22625461
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
 

Author Comment

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

Assisted Solution

by:sunnycoder
sunnycoder earned 400 total points
ID: 22626179
       printf("Enter your string;\n");
        scanf("%s\n", &strsrc);
 
http://linux.die.net/man/3/getchar

stdin refers to input stream
0
 

Author Comment

by:mikeregas
ID: 22626395
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
 
LVL 27

Assisted Solution

by:tliotta
tliotta earned 100 total points
ID: 22626923
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22628808
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

We are taking giant steps in technological advances in the field of wireless telephony. At just 10 years since the advent of smartphones, it is crucial to examine the benefits and disadvantages that have been report to us.
Article by: Nicole
This is a research brief on the potential colonization of humans on Mars.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

777 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