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
zamaam0728
13 New Member
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
zamaam0728
13 New Member
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
9,065 Recognized Expert Moderator Expert
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
newb16
687 Contributor
cout << "The two letters are the same: " << ansTrueFalse?"t rue":"false" << endl;
It didn't work because priority of ternary (?:) operator is lower than shift (<<). Use parentheses.
Apr 5 '10 #34

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
10555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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
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?
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.