Solved

i have a palimdone

Posted on 2008-10-02
15
202 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
Comment Utility
do {
    //do your processing
    //ask if user wants to continue doing it
} while (user said yes);
0
 

Author Comment

by:mikeregas
Comment Utility
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
Comment Utility
== is used for comparison ... = denotes assignment
char constants are enclosed in single quotes
choice == 'y'
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
:) ... Extra \n in the input buffer
0
 

Author Comment

by:mikeregas
Comment Utility
I removed the \n and it is not running the while loop so it still says press any key to continue?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 45

Assisted Solution

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

Author Comment

by:mikeregas
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
       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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Complex Numbers are funny things.  Many people have a basic understanding of them, some a more advanced.  The confusion usually arises when that pesky i (or j for Electrical Engineers) appears and understanding the meaning of a square root of a nega…
Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

762 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

11 Experts available now in Live!

Get 1:1 Help Now