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: - #include <iostream>
-
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
//function prototype
-
bool ignoreCaseCompare (char first, char second);
-
-
int main ()
-
{
-
char first = ' ';
-
char second = ' ';
-
-
cout << "Enter a letter: ";
-
cin >> first;
-
cout << "Enter another letter: ";
-
cin >> second;
-
-
first = toupper(first);
-
second = toupper(second);
-
-
cout << "Are the letters the same? " <<
-
-
return 0;
-
}
-
//******function defintions*****
-
bool ignoreCaseCompare (char one, char two)
-
-
{if (one == two)
-
return true;
-
else
-
return false;
-
}
-
-
//end main function
This programs is much easier without the value-return function. Can anyone offer any advice or direction?
33 10740
move toupper inside ignoreCaseCompare
Your main function doesn't call ignoreCaseCompare().
You want to call ignoreCaseCompare() on the original characters, not the toupper equivalents. Move the toupper and the comparison parts into the function itself.
I am getting error messages still, but here are the changes I made. -
#include <iostream>
-
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
//function prototype
-
bool ignoreCaseCompare (char first, char second);
-
-
int main ()
-
{
-
char first = ' ';
-
char second = ' ';
-
char answer = ' ';
-
-
cout << "Enter a letter: ";
-
cin >> first;
-
cout << "Enter another letter: ";
-
cin >> second;
-
-
answer = bool ignoreCaseCompare (char first, char second)
-
cout << "The letters the same: " answer << endl;
-
-
return 0;
-
}
-
//******function defintions*****
-
bool ignoreCaseCompare (char one, char two)
-
-
one = toupper(one);
-
two = toupper(two);
-
-
-
{if (one == two)
-
return true;
-
else
-
return false;
-
}
-
-
//end main function
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: -
one = toupper(one);
-
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: -
bool ignoreCaseCompare (char one, char two)
-
{
-
//code in here belongs to the ignoreCaseCompare method.
-
}
-Frinny
I moved -
one = toupper(one);
-
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! -
#include <iostream>
-
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
//function prototype
-
bool ignoreCaseCompare (char first, char second);
-
-
int main ()
-
{
-
char first = ' ';
-
char second = ' ';
-
char answer = ' ';
-
-
cout << "Enter a letter: ";
-
cin >> first;
-
cout << "Enter another letter: ";
-
cin >> second;
-
-
answer = bool ignoreCaseCompare (char first, char second)
-
cout << "The letters the same: " answer << endl;
-
-
return 0;
-
}
-
//******function defintions*****
-
bool ignoreCaseCompare (char one, char two)
-
-
one = toupper(one);
-
two = toupper(two);
-
-
-
{if (one == two)
-
return true;
-
else
-
return false;
-
}
-
-
//end main function
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: -
void myMethod()
-
{
-
bool a = true;
-
bool b = false;
-
}
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: -
void myMethod()
-
a = true;
-
{
-
-
bool b = false;
-
}
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
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.
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.
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
Ok...I got it to go through with no error messages, but it's not displaying true or false. -
#include <iostream>
-
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
//function prototype
-
bool ignoreCaseCompare (char, char);
-
-
int main ()
-
{
-
char first = ' ';
-
char second = ' ';
-
char answer = ' ';
-
-
cout << "Enter a letter: ";
-
cin >> first;
-
cout << "Enter a letter: ";
-
cin >> second;
-
-
answer = ignoreCaseCompare (first, second);
-
cout << "The letters are the same: " << answer << endl;
-
-
return 0;
-
}
-
//******function definitions**********
-
bool ignoreCaseCompare (char one, char two)
-
{
-
one = toupper(one);
-
two = toupper(two);
-
-
if (one == two)
-
return true;
-
else
-
return false;
-
}
-
//end main function
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.
It may not be pretty but here it is: -
#include <iostream>
-
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
//function prototype
-
bool ignoreCaseCompare (char, char);
-
-
int main ()
-
{
-
char first = ' ';
-
char second = ' ';
-
char answer = ' ';
-
-
cout << "Enter a letter: ";
-
cin >> first;
-
cout << "Enter a letter: ";
-
cin >> second;
-
-
cout << "The letters are the same: ";
-
answer = ignoreCaseCompare (first, second);
-
cout << endl;
-
-
return 0;
-
}
-
//******function definitions**********
-
bool ignoreCaseCompare (char one, char two)
-
{
-
one = toupper(one);
-
two = toupper(two);
-
-
-
if (one == two)
-
cout << "true";
-
else
-
cout << "false";
-
return 0;
-
}
-
//end main function
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: -
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:
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
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: -
if (one == two)
-
return true;
-
else
-
return false;
But no answer appeared.
Because you output the result as '0' or '1' ( cout interprets bool in integer, apparently). Use something like cout << answer?"true":"false";
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: -
bool ignoreCaseCompare (char one, char two)
-
{
-
//declare a variable to keep track of what to return
-
bool isSameCharacter = false;
-
-
// set the variable according to whether or not the characters match
-
if (toupper(one)== toupper(two)){
-
isSameCharacter = true;
-
}else{
-
isSameCharacter = false;
-
}
-
-
//return whether or not the characters are the same
-
-
return isSameCharacter;
-
}
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
Banfa 9,065
Expert Mod 8TB
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.
Banfa 9,065
Expert Mod 8TB
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.
Banfa, what is a "iomanipiulator"????
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.
Banfa 9,065
Expert Mod 8TB
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 Banfa 9,065
Expert Mod 8TB
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.
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 :)
Banfa 9,065
Expert Mod 8TB
The link is working for me.
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: -
isSameCharacter = ignoreCaseCompare (first, second);
-
cout << "The two letters are the same: " << isSameCharacter?"true":"false" << endl;
-
return 0;
-
// end of main function
-
}
-
//****function defintions*****
-
bool ignoreCaseCompare (char a, char b)
-
{
-
bool isSameCharacter = false;
-
{
-
if (toupper (a)== toupper (b))
-
isSameCharacter = true;
-
else
-
isSameCharacter = false;
-
}
-
-
return isSameCharacter;
-
-
}
Error messages:
error C2563: mismatch in formal parameter list
error C2568: '<<' : unable to resolve function overload
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.
- #include <iostream>
-
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
//function prototype
-
bool ignoreCaseCompare(char, char);
-
-
int main ()
-
{
-
char first = ' ';
-
char second = ' ';
-
char ansTrueFalse = ' ';
-
-
cout << "Enter a letter: ";
-
cin >> first;
-
cout << "Enter a second letter: ";
-
cin >> second;
-
-
ansTrueFalse = ignoreCaseCompare (first, second);
-
cout << "The two letters are the same: " << ansTrueFalse?"true":"false" << endl;
-
return 0;
-
// end of main function
-
}
-
//****function defintions*****
-
bool ignoreCaseCompare (char a, char b)
-
{
-
bool ansTrueFalse = false;
-
{
-
if (toupper (a)== toupper (b))
-
ansTrueFalse = true;
-
else
-
ansTrueFalse = false;
-
}
-
-
return ansTrueFalse;
-
-
}
-
-
//end of isSameCharacter
errors are referencing line 22
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?
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 - #include <iostream>
-
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
//function prototype
-
bool ignoreCaseCompare(char, char);
-
-
int main ()
-
{
-
char first = ' ';
-
char second = ' ';
-
bool ansTrueFalse = false;
-
-
cout << "Enter a letter: ";
-
cin >> first;
-
cout << "Enter a second letter: ";
-
cin >> second;
-
-
ansTrueFalse = ignoreCaseCompare (first, second);
-
cout << "The two letters are the same: " << ansTrueFalse << endl;
-
return 0;
-
// end of main function
-
}
-
//****function defintions*****
-
bool ignoreCaseCompare (char a, char b)
-
-
{
-
if (toupper (a)== toupper (b))
-
return true;
-
else
-
return false;
-
-
}
-
-
-
-
//end of isSameCharacter
I think I've got it!!! - #include <iostream>
-
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
//function prototype
-
bool ignoreCaseCompare(char, char);
-
-
int main ()
-
{
-
char first = ' ';
-
char second = ' ';
-
bool ansTrueFalse = false;
-
-
cout << "Enter a letter: ";
-
cin >> first;
-
cout << "Enter a second letter: ";
-
cin >> second;
-
-
ansTrueFalse = ignoreCaseCompare (first, second);
-
cout << "The two letters are the same: ";
-
{
-
if (ansTrueFalse = true)
-
cout << "true" << endl;
-
else
-
cout << "false" << endl;
-
}
-
return 0;
-
// end of main function
-
}
-
//****function defintions*****
-
bool ignoreCaseCompare (char a, char b)
-
-
{
-
if (toupper (a)== toupper (b))
-
return true;
-
else
-
return false;
-
-
}
-
-
-
-
//end of isSameCharacter
Banfa 9,065
Expert Mod 8TB - 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 - 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 - cout << "The two letters are the same: " << boolalpha << isSameCharacter << endl;
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.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by G Kannan |
last post: by
|
4 posts
views
Thread by Dave |
last post: by
|
16 posts
views
Thread by cwizard |
last post: by
|
13 posts
views
Thread by Clevo |
last post: by
|
21 posts
views
Thread by Michael Bierman |
last post: by
|
9 posts
views
Thread by ckerns |
last post: by
|
3 posts
views
Thread by Allerdyce.John |
last post: by
|
7 posts
views
Thread by turtle |
last post: by
|
7 posts
views
Thread by Terry Olsen |
last post: by
| | | | | | | | | | | |