/*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);

}

Recommended Answers

All 3 Replies

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

commented: heh. +7

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 ();
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.