954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get user character input convert to lower case if UpperCase...Quitting help

/*Hello, I am to create a program that gets a character and if it is uppercase, convert it to lowercase, if it is numeric print an error. I wrote the following code but my problem is that when I ask the user if he/she would like to quit, and I enter 'N' or 'n' the program does not get out of the while loop when it is suppose to. What am I doing wrong? Any help would be appreciated. Thanks in advanced.

#include<stdio.h>
#include<conio.h>
void toLowerCase(char);
char ch;
int main (void)
{
	char ans;
	do
	{
	char ch;
	char choice;
	printf("\nPlease input a character then press enter\n");
	ch = getche();

	if ( ch >='A' && ch <='Z' )
		choice = 'U';
	if ( ch >= '0' && ch <= '9' )
		choice = 'N';
	if ( ch >= 'a' && ch <='z' )
		choice = 'L';

	switch(choice)
	{
	case 'U':
		printf("\nYou inputted %c", ch);
		printf("\n!!!Upper Case character inputted!!!\n");
		printf("\n\nThe program will now convert this to lowercase\n");
		toLowerCase(ch);
		break;

	case 'N':
		printf("\nYou inputted %c", ch);
		printf("\n!!!Error: Numeric value inputted!!!\n");
		break;

	case 'L':
		printf("\nYou inputted %c", ch);
		break;
	}

/* Below is the problem that I am facing. It does not get out of the while loop if I enter 'n' or 'N'*/
	printf("\nWould you like to input another character? (y/n) : ");
	ans = getche();
	printf("\n");
}while ( ans != 'n' || ans != 'N' );
	
	return 0;
}

void toLowerCase(char ch)
{
	ch = ch+32;
	printf("Converted value is %c", ch);

}
mtech91
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Write a boolean truth table for
while ( ans != 'n' || ans != 'N' );

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

try this,

printf ("\n\n\nWould you like to input another character? (y/n): ");
ans=getche();
{
if (ans=='y' || ans=='Y')
main ();
}
{
if (ans=='n' || ans=='N')
exit ();
}
Xufyan
Posting Whiz
309 posts since Mar 2010
Reputation Points: 6
Solved Threads: 7
 

Even though you technically can, NEVER call main(), it is not intended to be a recursive function.

A loop, specifically a while loop in this case, is the correct way to do it. The issue is that his conditional is not written correctly, as WaltP pointed out.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: