Solved

i have a palimdone

Posted on 2008-10-02
15
207 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
[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
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

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.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month7 days, 3 hours left to enroll

622 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