Hi, all. I've been toying with this program for a couple of weeks, and haven't been able to find any solution anywhere so here I am. Basically I'm using two functions to do the bulk of the work for this program. The console needs to ask for two letter inputs and display true or false depending on whether they match or not. both functions seem to be working generally, but I'm getting "true" on any two inputs. Any help is appreciated ladies and gents.
EDIT: Thanks for the tips, everyone, helped me out a lot.
#include <iostream>
#include <string>
#include <cctype>
// input: prompt string result: character entered by the user
char get_input_char( std::string prompt ) ;
// input: characters to be compared for equality
// result: bool true if the characters compare equal when case is ignored
bool icase_eq( char a, char b ) ;
int main()
{
// pass the input (prompt string) to the function, and save the returned result in a
constchar a = get_input_char( "enter a character" ) ;
// pass the input (prompt string) to the function, and save the returned result in b
constchar b = get_input_char( "enter another character" ) ;
// pass the input (the two characters) to the function , and save the returned result in eq
constbool eq = icase_eq( a, b ) ;
// print the result in eq
std::cout << "the characters '" << a << "' and '" << b
<< "' compare equal ignoring case: " << std::boolalpha << eq << '\n' ;
}
// input: prompt string result: character entered by the user
char get_input_char( std::string prompt )
{
std::cout << prompt << ": " ;
char answer ;
std::cin >> answer ;
return answer ; // return the result
}
// input: characters to be compared for equality
// result: bool true if the characters compare equal when case is ignored
bool icase_eq( char a, char b ) { return std::toupper(a) == std::toupper(b) ; }
Ok, your have multiple problems. 1. bool is either true or false they can not be spaces. 2. your getInputChar function, you are passing it variables to use them it should look something more like:
1 2 3 4 5 6 7
void getInputChar(string prompt, char & answer)// added the variable that are passed
{
//removed char answer
cout << "enter a character ";
cin >> answer;
cout << endl;
}
Your second function is also a bool yet does not return anything.
Here's one way to modify what you posted. The problem is that you are not returning anything from your function, nor are your doing anything with the parameters, at least in getInputChar. Compile with all warnings on (-Wall -Wextra -pedantic-errors), so that your compiler will tell you: