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: - #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?
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
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
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: -
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 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
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";
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: -
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 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
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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:
|
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>
|
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;
|
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>
|
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
| |
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.");
|
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
}
|
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
|
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),...
|
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;
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |