Void function problems

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.

[code]

#include <iostream>
using namespace std;

void getInputChar (string, char &);
bool ignoreCaseCompare (char, char);

int main() {
char first = ' ';
char second = ' ';
string prompt = " ";
bool answer = " ";

getInputChar (prompt, first);
getInputChar (prompt, second);

answer = ignoreCaseCompare (first, second);
cout << answer << endl;

}
void getInputChar (string, char &) {
char answer = ' ';
cout << "enter a character ";
cin >> answer;
cout << endl;
}
bool ignoreCaseCompare (char one, char two) {
one = toupper(one);
two = toupper(two);
if (one == two)
cout << "true";
else {
cout << "false"; }
cout << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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
    const char a = get_input_char( "enter a character" ) ;

    // pass the input (prompt string) to the function, and save the returned result in b
    const char b = get_input_char( "enter another character" ) ;

    // pass the input (the two characters) to the function , and save the returned result in eq
    const bool 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) ; }
Last edited on
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.
1
2
3
4
5
6
7
8
# include <iostream>
# include <cctype>
int main() { 
  char a{}, b{};
  std::cin >> a >> b;
  std::cout << "'" << a << "' and '" <<  b << "' are equal: "
            << std::boolalpha << (std::toupper(a) == std::toupper(b)) << '\n';
}

Live demo:
http://coliru.stacked-crooked.com/a/16995c39235ff515

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream> 
#include <cctype>
// using namespace std;

void getInputChar(std::string const&, char &);
bool ignoreCaseCompare(char, char);

int main() {
    std::string const prompt = "enter a character: ";
    
    char first  = ' ';
    getInputChar(prompt, first);
    char second = ' ';
    getInputChar(prompt, second);
    
    std::cout << "a and be are equal: " << std::boolalpha 
              << ignoreCaseCompare(first, second) << '\n';
}

void getInputChar(std::string const& prompt, char& answer) {
    std::cout << prompt;
    std::cin >> answer;
}

bool ignoreCaseCompare(char const one, char const two) {
    return std::toupper(one) == std::toupper(two);
}

Demo:
http://coliru.stacked-crooked.com/a/a010ec7564cb7767
Last edited on
Topic archived. No new replies allowed.