Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

i have a palimdone

Posted on 2008-10-02
15
Medium Priority
?
209 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 1200 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 1200 total points
ID: 22624257
== is used for comparison ... = denotes assignment
char constants are enclosed in single quotes
choice == 'y'
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 1200 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 1200 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 1200 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 1200 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 1200 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 300 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction On a scale of 1 to 10, how would you rate our Product? Many of us have answered that question time and time again. But only a few of us have had the pleasure of receiving a stack of the filled out surveys and being asked to do somethi…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

722 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