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?
33 11090
move toupper inside ignoreCaseCompa re
donbock 2,426
Recognized Expert Top Contributor
Your main function doesn't call ignoreCaseCompa re().
jkmyoung 2,057
Recognized Expert Top Contributor
You want to call ignoreCaseCompa re() 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
Frinavale 9,735
Recognized Expert Moderator Expert
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: 'ignoreCaseComp are' : 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
Frinavale 9,735
Recognized Expert Moderator Expert
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....remov e 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 ignoreCaseCompa re (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.
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: 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: 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,...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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...
| |