0

Hey everyone,
My problem is that in my program below I made an indefinite loop for comparing strings and values, my problem is on line 60 & 61, the loop automatically accepts whichever if statement is on line 60 when I test for an uppercase and lowercase char, as soon as I remove the logical or from my if statements and remove the capital char they work fine.

Also if your wondering why I haven't used all the functions, its because I just wanted to test each out before doing all the complete coding but im just stuck on this if.

#include <iostream>
#include <cstring>

int equal(int x, int y)
{
	if(x == y)
		return true;
	else 
	{
		return false;
	}
}

int equal(char x, char y)
{
	if(x == y)
		return true;
	else return false;
}


int equal(double x, double y)
{
	if(x == y)
		return true;
	else return false;
}

int equal(char* x, char* y)
{
	if(strcmp(x, y) == 0)
		return true;
	else return false;
}

int main()
{
	for(;;)
	{
	char yn;
	std::cout<<"\n\nComparison of:\nInt\nChar\nDouble\nChar*\n";
	int a(0);
	int b(0);
	double c(0);
	double d(0);
	char* e = (nullptr);
	char* f = (nullptr);
	std::cout<<"\n\nI will compare two integers that you must now type in\nFirst: ";
	std::cin>>a;
	std::cout<<"Second: ";
	std::cin>>b;
	std::cout<<"Comparing...\n";

	if(equal(a, b))
		std::cout<<"They are equal in value\n";
	else 
		std::cout<<"They are unequal\n";
	std::cout<<"\nWould you like to continue?\nY or N: ";
	std::cin>>yn;
	if(yn == ('n' || 'N') ) break;
	if(yn == 'y' || 'Y') continue;
	}
	
	return 0;
}
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by L3gacy
0

You have to test for equality twice: yn == 'y' || yn == 'Y ' etc. You may find that you also only want to test the first character of yn to be sure your user didn't add an extraneous (or necessary) keystroke or several, such as "yes\n"

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.