By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

C++ Value Return Function that produces a true/false

P: 13
I am at the end of my first semester of C++, and I'm not sure what I should do to make this program meet the requested specifications. The assignment is as follows:

Write a function called ignoreCaseCompare() that has two character (char) parameters. The function should return true if the two characters received represent the same letter, even if the case does not agree. Otherwise, the function should return false. Then, write a simple main() function that uses your ignoreCaseCompare().

Additional instructions:

Here is the function heading:
bool ignoreCaseCompare(char c1, char c2)

{

//Write the code to compare c1 with c2 and return true/false

}


Here is what I have:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. //function prototype
  8. bool ignoreCaseCompare (char first, char second);
  9.  
  10. int main ()
  11. {
  12.     char first = ' ';
  13.     char second = ' ';
  14.  
  15.     cout << "Enter a letter: ";
  16.     cin >> first;
  17.     cout << "Enter another letter: ";
  18.     cin >> second;
  19.  
  20.     first = toupper(first);
  21.     second = toupper(second);
  22.  
  23.     cout << "Are the letters the same? " <<
  24.  
  25.     return 0;
  26. }
  27. //******function defintions*****
  28. bool ignoreCaseCompare (char one, char two)
  29.  
  30. {if (one == two)
  31. return true;
  32. else
  33. return false;
  34. }
  35.  
  36.     //end main function

This programs is much easier without the value-return function. Can anyone offer any advice or direction?
Mar 29 '10 #1
Share this Question
Share on Google+
33 Replies


100+
P: 687
move toupper inside ignoreCaseCompare
Mar 29 '10 #2

Expert 100+
P: 2,404
Your main function doesn't call ignoreCaseCompare().
Mar 29 '10 #3

jkmyoung
Expert 100+
P: 2,057
You want to call ignoreCaseCompare() on the original characters, not the toupper equivalents. Move the toupper and the comparison parts into the function itself.
Mar 29 '10 #4

P: 13
I am getting error messages still, but here are the changes I made.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2.  
  3. using std::cout; 
  4. using std::cin; 
  5. using std::endl; 
  6.  
  7. //function prototype 
  8. bool ignoreCaseCompare (char first, char second); 
  9.  
  10. int main () 
  11.     char first = ' '; 
  12.     char second = ' ';
  13.     char answer = ' ';
  14.  
  15.     cout << "Enter a letter: "; 
  16.     cin >> first; 
  17.     cout << "Enter another letter: "; 
  18.     cin >> second; 
  19.  
  20.     answer = bool ignoreCaseCompare (char first, char second)
  21.     cout << "The letters the same: " answer << endl;
  22.  
  23.     return 0; 
  24. //******function defintions***** 
  25. bool ignoreCaseCompare (char one, char two) 
  26.  
  27. one = toupper(one); 
  28. two = toupper(two); 
  29.  
  30.  
  31. {if (one == two) 
  32. return true; 
  33. else 
  34. return false; 
  35.  
  36.     //end main function
Mar 29 '10 #5

Frinavale
Expert Mod 5K+
P: 9,731
You should really try to read and look at what the error message has to say. If you still can't make heads or tails of what it's telling you...you should post the error message so that we can see it and can help you further with your problem.

The error in this case is very obvious...the error(s) occurred on lines 29 and 30 of your above posted code....


You did not move the following lines inside a method:
Expand|Select|Wrap|Line Numbers
  1.  one = toupper(one); 
  2.  two = toupper(two); 
Please note that in order for code to be within a method it must be inside the method's opening and closing curly braces {}

For example:
Expand|Select|Wrap|Line Numbers
  1. bool ignoreCaseCompare (char one, char two) 
  2. {
  3.   //code in here belongs to the ignoreCaseCompare method.
  4. }
-Frinny
Mar 29 '10 #6

P: 13
I moved
Expand|Select|Wrap|Line Numbers
  1. one = toupper(one);  
  2. two = toupper(two);
inside the function.

Here are the error messages:

error C2062: type 'bool' unexpected
error C3646: 'one' : unknown override specifier
error C2072: 'ignoreCaseCompare' : initialization of a function
error C2065: 'one' : undeclared identifier
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2447: '{' : missing function header (old-style formal list?)

I have never had any difficulty with this until now. Ugh!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2.  
  3. using std::cout; 
  4. using std::cin; 
  5. using std::endl; 
  6.  
  7. //function prototype 
  8. bool ignoreCaseCompare (char first, char second); 
  9.  
  10. int main () 
  11.     char first = ' '; 
  12.     char second = ' ';
  13.     char answer = ' ';
  14.  
  15.     cout << "Enter a letter: "; 
  16.     cin >> first; 
  17.     cout << "Enter another letter: "; 
  18.     cin >> second; 
  19.  
  20.     answer = bool ignoreCaseCompare (char first, char second)
  21.     cout << "The letters the same: " answer << endl;
  22.  
  23.     return 0; 
  24. //******function defintions***** 
  25. bool ignoreCaseCompare (char one, char two) 
  26.  
  27. one = toupper(one); 
  28. two = toupper(two); 
  29.  
  30.  
  31. {if (one == two) 
  32. return true; 
  33. else 
  34. return false; 
  35.  
  36.     //end main function
Mar 29 '10 #7

Frinavale
Expert Mod 5K+
P: 9,731
Add an open curly brace "{" to line 28 in your above posted code.
Then delete the open curly brace on line 33 (that is in front of your if statement).

In C, C++, Java, JavaScript, C#...and may other programming languages...
The open curly braces "{" indicate the start of a block of code and the close curly brace "}" indicates the end of a block of code.

A function is a block of code and it requires an open curly brace "{" and a close curly brace "}" to indicate where the function stops and where it ends. Anything in side the curly braces is used by the method.

Your function has to look like:
Expand|Select|Wrap|Line Numbers
  1. void myMethod()
  2. {
  3.   bool a = true;
  4.   bool b = false;
  5. }
Notice how the bool a and the bool b are declared inside the method?
Notice how the open curly brace "{" comes before these variables?


If I had put the following it would not compile because it doesn't make sense to the compiler:
Expand|Select|Wrap|Line Numbers
  1. void myMethod()
  2.  a = true;
  3. {
  4.  
  5.   bool b = false;
  6. }
That is what you have going on right now in your code.


Once you've fixed that, you should address the problems that you are causing on line 21 in your above posted code. This is not how you call a method....remove the "bool" on that line or indicate that you are casting to a bool by placing the bool within open and close parentheses like this: (bool).....also you need to add a semicolon ";" to the end of line 21 to indicate that you are finished.

-Frinny
Mar 29 '10 #8

100+
P: 687
Calling your function as
answer = bool ignoreCaseCompare (char first, char second)
is wrong. I don't post the correct version - one semester of C/C++ should be enough to learn how to call C functions and how to read error messages.
Hint - toupper() is a function too.
Mar 29 '10 #9

P: 13
I thought about using the void, but we can not because that is inthe next chapter, and we can not include commands that haven't been covered yet.
Mar 29 '10 #10

Frinavale
Expert Mod 5K+
P: 9,731
If a function returns void then it doesn't return anything!

In your case your function should return a bool (true or false), so your function can't be void. Unless you're talking about the main function....that one could return void.

-Frinny
Mar 30 '10 #11

P: 13
Ok...I got it to go through with no error messages, but it's not displaying true or false.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. //function prototype
  8. bool ignoreCaseCompare (char, char);
  9.  
  10. int main ()
  11. {
  12.     char first = ' ';
  13.     char second = ' ';
  14.     char answer = ' ';
  15.  
  16.     cout << "Enter a letter: ";
  17.     cin >> first;
  18.     cout << "Enter a letter: ";
  19.     cin >> second;
  20.  
  21.     answer = ignoreCaseCompare (first, second);
  22.     cout << "The letters are the same: " << answer << endl;
  23.  
  24.     return 0;
  25. }
  26. //******function definitions**********
  27. bool ignoreCaseCompare (char one, char two)
  28. {
  29.     one = toupper(one);
  30.     two = toupper(two);
  31.  
  32.     if (one == two)
  33.         return true;
  34.     else
  35.         return false;
  36. }
  37. //end main function
Mar 30 '10 #12

P: 13
Thank you for all of your help. You helped me see the obvious mistakes, and explained the errors to me so I won't make the same mistakes again.
Mar 30 '10 #13

P: 13
It may not be pretty but here it is:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. //function prototype
  8. bool ignoreCaseCompare (char, char);
  9.  
  10. int main ()
  11. {
  12.     char first = ' ';
  13.     char second = ' ';
  14.     char answer = ' ';
  15.  
  16.     cout << "Enter a letter: ";
  17.     cin >> first;
  18.     cout << "Enter a letter: ";
  19.     cin >> second;
  20.  
  21.     cout << "The letters are the same: ";
  22.     answer = ignoreCaseCompare (first, second);
  23.     cout << endl;
  24.  
  25.     return 0;
  26. }
  27. //******function definitions**********
  28. bool ignoreCaseCompare (char one, char two)
  29. {
  30.     one = toupper(one);
  31.     two = toupper(two);
  32.  
  33.  
  34.     if (one == two)
  35.         cout << "true";
  36.     else
  37.         cout << "false";
  38.     return 0;
  39. }
  40. //end main function
Mar 30 '10 #14

Frinavale
Expert Mod 5K+
P: 9,731
I'm glad you were finally able to compile your project.


Whenever you are posting code for us to take a look at could you please use code tags? It makes it a lot easier for us to read your code and it gives us line numbers that help when we are trying to refer to a specific line of code.

Your ignoreCaseCompare function returns a type bool....see your function's signature:
Expand|Select|Wrap|Line Numbers
  1. bool ignoreCaseCompare (char one, char two)
Notice how you have "bool" in front of the function name?
This is the type that the function should be returning.

Now, take a look at the last line in your function:
Expand|Select|Wrap|Line Numbers
  1. return 0;
You are returning an int (0)....this is not a boolean value.
You should be returning true or false depending on whether or not the characters matched.

You could approach this a couple of ways but I think the best practice is to have 1 return for the function (just like what you're doing)....you are going to need to have a bool variable within your ignoreCaseCompare method that will keep track of whether or not the character is the same. You will return this bool at the end of the function.


Do you understand what I'm trying to say?

-Frinny
Mar 30 '10 #15

P: 13
Frinny,

Thanks for all of your help. I'm not sure if I understand the code tags. I did read the link.

If I understand you correctly, you are referring to the function, and the fact that it does not really serve its purpose.

I first attempted this:
Expand|Select|Wrap|Line Numbers
  1. if (one == two) 
  2.       return true;
  3. else
  4.       return false;
But no answer appeared.
Mar 30 '10 #16

100+
P: 687
Because you output the result as '0' or '1' ( cout interprets bool in integer, apparently). Use something like cout << answer?"true":"false";
Mar 30 '10 #17

Frinavale
Expert Mod 5K+
P: 9,731
What you had originally is what is expected of the function.
The function is supposed to return a value...not print the value.

What I was recommending is that you only have 1 return statement in your function. This is "good coding practice" because gives your function 1 entry point and 1 exit point. It makes it easier to understand by humans and compilers.

I was recommending that you do this:
Expand|Select|Wrap|Line Numbers
  1. bool ignoreCaseCompare (char one, char two)
  2. {
  3. //declare a variable to keep track of what to return
  4. bool isSameCharacter = false;
  5.  
  6. // set the variable according to whether or not  the characters match
  7.     if (toupper(one)== toupper(two)){
  8.        isSameCharacter = true;
  9.     }else{
  10.        isSameCharacter = false;
  11.     }
  12.  
  13. //return whether or not the characters are the same
  14.  
  15.     return isSameCharacter;
  16. }
Now, the reason the value is not being printed in this case is because the function is not supposed to print anything!

Your function is simply supposed check if it's the same character and then return true or false accordingly (right??)

It's not the responsibility of the ignoreCaseCompare to print anything....you should do this in your main method.


In your main method on line 22, you are storing the result of the method into "answer" (I don't see where you've defined/declared this variable)...but you are not printing this variable onto the screen...you should have a cout that prints the answer onto the screen

See newb16's post (just above this one)!

:)

-Frinny
Mar 30 '10 #18

Banfa
Expert Mod 5K+
P: 8,916
Normally cout does interpret bool as an integer (it is implemented as one after all) but you can get text output like this

cout << boolalpha << answer;

where boolalpha is an iomanipiulator.
Mar 30 '10 #19

Banfa
Expert Mod 5K+
P: 8,916
Unless you're talking about the main function....that one could return void.
No, no, no absolutely never ever ever never ever never ever (getting the picture?) should main return void.

main returns int, returning void is at best an extension and unnecessairly making you code non-portable and at worst undefined behaviour.

Always return int from main.
Mar 30 '10 #20

Frinavale
Expert Mod 5K+
P: 9,731
Banfa, what is a "iomanipiulator"????
Mar 30 '10 #21

Frinavale
Expert Mod 5K+
P: 9,731
No, no, no absolutely never ever ever never ever never ever (getting the picture?) should main return void.
In school I was fist taught that main should be void....I'm pretty sure of that. It wasn't until I started doing bash scripting did I see why the main function should return something and so I started implementing the main method so that it returned an int.
Mar 30 '10 #22

Banfa
Expert Mod 5K+
P: 8,916
Opps, I mean iomanipulator they are the things, like boolalpha that you can pass to cout to perform all the formating you used to do with the codes in the printf % formats. Defined in the header <iomanip>.

For instance
Set text or numerical display of bool
Set field widths with left or right alignment
Set precision
Set display format of floating points
Set base to display integers
Set the field padding character

Here's the reference
Mar 30 '10 #23

Banfa
Expert Mod 5K+
P: 8,916
In school I was fist taught that main should be void....
Yes it is unfortunate the some schools did, and still do, teach main returning void for C/C++ programming. It doesn't change the fact it is just about as wrong as you can get.
Mar 30 '10 #24

Frinavale
Expert Mod 5K+
P: 9,731
Thanks for the reference (link seems to be down though or I just can't access it due to network stuffs).


At least now I know what you're talking about :)
Mar 30 '10 #25

Banfa
Expert Mod 5K+
P: 8,916
The link is working for me.
Mar 30 '10 #26

P: 13
I'm still working on this. I feel like such a numbskull because it's still not right, and I have to make a change to this program for the next assignment.

Here are the changes I have made per your advice:
Expand|Select|Wrap|Line Numbers
  1. isSameCharacter = ignoreCaseCompare (first, second);
  2.     cout << "The two letters are the same: " << isSameCharacter?"true":"false" << endl;
  3.     return 0;
  4.     // end of main function
  5. }
  6. //****function defintions*****
  7. bool ignoreCaseCompare (char a, char b)
  8. {
  9.     bool isSameCharacter = false;
  10.     {
  11.         if (toupper (a)== toupper (b))
  12.         isSameCharacter = true;
  13.         else
  14.         isSameCharacter = false;
  15.     }
  16.  
  17.         return isSameCharacter;
  18.  
  19. }
Error messages:
error C2563: mismatch in formal parameter list
error C2568: '<<' : unable to resolve function overload
Apr 5 '10 #27

Expert 100+
P: 2,404
To use CODE tags ...
  • Select [highlight] the source code in your post.
  • Click on the "#" button on the message toolbar.

Can't tell from your snippet:
  • What type is variable isSameCharacter?
  • What type is variable first?
  • What type is variable second?
  • What is the function prototype for ignoreCaseCompare?

Usually error messages from the compiler identify the line of source code they are complaining about. That information is always helpful in interpreting the error message. Please tell us which source line the error messages refer to.
Apr 5 '10 #28

P: 13
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. //function prototype
  8. bool ignoreCaseCompare(char, char);
  9.  
  10. int main ()
  11. {
  12.     char first = ' ';
  13.     char second = ' ';
  14.     char ansTrueFalse = ' ';
  15.  
  16.     cout << "Enter a letter: ";
  17.     cin >> first;
  18.     cout << "Enter a second letter: ";
  19.     cin >> second;
  20.  
  21.     ansTrueFalse = ignoreCaseCompare (first, second);
  22.     cout << "The two letters are the same: " << ansTrueFalse?"true":"false" << endl;
  23.     return 0;
  24.     // end of main function
  25. }
  26. //****function defintions*****
  27. bool ignoreCaseCompare (char a, char b)
  28. {
  29.     bool ansTrueFalse = false;
  30.     {
  31.         if (toupper (a)== toupper (b))
  32.         ansTrueFalse = true;
  33.         else
  34.         ansTrueFalse = false;
  35.     }
  36.  
  37.         return ansTrueFalse;
  38.  
  39. }
  40.  
  41. //end of isSameCharacter 
errors are referencing line 22
Apr 5 '10 #29

Expert 100+
P: 2,404
Lines 8, 14, and 21 are inconsistent. What is the return type of ignoreCaseCompare and why aren't you using that type?

What do you expect line 22 to do?
There are three double-quotes in line 22 -- sounds unbalanced.
What is that question mark supposed to do in line 22?
Apr 5 '10 #30

P: 13
Okay. I made a couple of changes based on the last questions. The program is showing no errors, but the results are now displaying a 0 for false and a 1 for true. The last thing I need to figure out is how to get the display to read true/false instead of 1/0

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. //function prototype
  8. bool ignoreCaseCompare(char, char);
  9.  
  10. int main ()
  11. {
  12.     char first = ' ';
  13.     char second = ' ';
  14.     bool ansTrueFalse = false;
  15.  
  16.     cout << "Enter a letter: ";
  17.     cin >> first;
  18.     cout << "Enter a second letter: ";
  19.     cin >> second;
  20.  
  21.     ansTrueFalse = ignoreCaseCompare (first, second);
  22.     cout << "The two letters are the same: " << ansTrueFalse << endl;
  23.     return 0;
  24.     // end of main function
  25. }
  26. //****function defintions*****
  27. bool ignoreCaseCompare (char a, char b)
  28.  
  29.     {
  30.         if (toupper (a)== toupper (b))
  31.         return true;
  32.         else
  33.         return false;
  34.  
  35.     }
  36.  
  37.  
  38.  
  39. //end of isSameCharacter
Apr 5 '10 #31

P: 13
I think I've got it!!!

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2.  
  3. using std::cout; 
  4. using std::cin; 
  5. using std::endl; 
  6.  
  7. //function prototype 
  8. bool ignoreCaseCompare(char, char); 
  9.  
  10. int main () 
  11.     char first = ' '; 
  12.     char second = ' '; 
  13.     bool ansTrueFalse = false; 
  14.  
  15.     cout << "Enter a letter: "; 
  16.     cin >> first; 
  17.     cout << "Enter a second letter: "; 
  18.     cin >> second; 
  19.  
  20.     ansTrueFalse = ignoreCaseCompare (first, second); 
  21.     cout << "The two letters are the same: ";
  22.     {
  23.     if (ansTrueFalse = true)
  24.         cout << "true" << endl;
  25.     else
  26.         cout << "false" << endl;
  27.     }
  28.     return 0; 
  29.     // end of main function 
  30. //****function defintions***** 
  31. bool ignoreCaseCompare (char a, char b) 
  32.  
  33.     { 
  34.         if (toupper (a)== toupper (b)) 
  35.         return true; 
  36.         else 
  37.         return false; 
  38.  
  39.     } 
  40.  
  41.  
  42.  
  43. //end of isSameCharacter 
Apr 5 '10 #32

Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. cout << "The two letters are the same: " << isSameCharacter?"true":"false" << endl;
Doesn't work because of the relative operator precedences of << and ?: (look them up). This is not unusual because << has quite a high precedence so if you are trying to evaluate an expression in acout statement always enclose it in brackets, this should work
Expand|Select|Wrap|Line Numbers
  1. cout << "The two letters are the same: " << (isSameCharacter?"true":"false") << endl;
However the is an iomanipulator to do this for you, include <iomanip> and you can just put
Expand|Select|Wrap|Line Numbers
  1. cout << "The two letters are the same: " << boolalpha << isSameCharacter << endl;
Apr 5 '10 #33

100+
P: 687
cout << "The two letters are the same: " << ansTrueFalse?"true":"false" << endl;
It didn't work because priority of ternary (?:) operator is lower than shift (<<). Use parentheses.
Apr 5 '10 #34

Post your reply

Sign in to post your reply or Sign up for a free account.