473,803 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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 ignoreCaseCompa re() 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 ignoreCaseCompa re().

Additional instructions:

Here is the function heading:
bool ignoreCaseCompa re(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
33 11093
Frinavale
9,735 Recognized Expert Moderator Expert
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....tha t one could return void.

-Frinny
Mar 30 '10 #11
zamaam0728
13 New Member
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
zamaam0728
13 New Member
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
zamaam0728
13 New Member
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
9,735 Recognized Expert Moderator Expert
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 ignoreCaseCompa re 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 ignoreCaseCompa re 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
zamaam0728
13 New Member
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
newb16
687 Contributor
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
9,735 Recognized Expert Moderator Expert
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 ignoreCaseCompa re 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
9,065 Recognized Expert Moderator Expert
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
9,065 Recognized Expert Moderator Expert
Unless you're talking about the main function....tha t 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

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

Similar topics

1
14158
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of uninitialized value in concatenation (.) at register.pl line 38, <STDIN> line 10." The PERL code is as follows:
4
16298
by: Dave | last post by:
Hi, I tried something with 'return value' of a function and i got two different behaviours. My question is: why does method 1 not work? Thanks Dave method 1: here, whatever i choose (ok or cancel), i go to 'webpage.htm' <body>
16
11501
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not an object... the function is like so: function calc_total() { var x,i,base,margin,total,newmargin,newtotal; base = document.frmKitAmount.txtTotalKitValue.value; margin = document.frmKitAmount.margin.value/100;
13
6208
by: Clevo | last post by:
Hello, I want to check if user select one from the radiobox group. How can I get the radiobox actual value in javascript. In the value field I see the default value, but how can I know if it really was selected? Thanks! <input name="menu_order" id = "menu_order1" type="radio" value="1">one<br> <input name="menu_order" id = "menu_order2" type="radio" value="2">two<br>
21
3995
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
9
2937
by: ckerns | last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0). If value is null or non-numeric I want to assign the value of "0". rowString = "L411" //conrol name if (isNaN(eval ("document.forms."+rowString+".value")) == true ) { //this alert works if the value is a letter,i.e,"a" alert("You have entered an non-numeric value.\nEnter a number in the appropriate box.");
3
4211
by: Allerdyce.John | last post by:
Hi, In my code, I have a function which has a return value in the declaration: bool myFunction( int a) { // my implmentation }
7
3248
by: turtle | last post by:
I want to find out the max value of a field on a report if the field is not hidden. I have formatting on the report and if the field doesn't meet a certain criteria then it is hidden. I want to get a max of the field for the ones that are not hidden. is this possible? TIA, KO
7
10329
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function IsLvItemCheckedDelegate(ByVal ClientID As Integer) As Boolean Private Function IsLvItemChecked(ByVal ClientID As Integer) As Boolean If lvServers.InvokeRequired = True Then lvServers.Invoke(New IsLvItemCheckedDelegate(AddressOf IsLvItemChecked),...
2
4680
by: mndprasad | last post by:
Hi friends, Am new to AJAX coding, In my program am going to have two texbox which going to implent AJAX from same table. One box is going to retrieve the value of other and vice versa. I have implemented successfully for one text box, but it's done for the other field, since am getting script errors. <script> var queryField; var lookupURL; var divName;
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.