473,790 Members | 2,380 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 #1
33 11090
newb16
687 Contributor
move toupper inside ignoreCaseCompa re
Mar 29 '10 #2
donbock
2,426 Recognized Expert Top Contributor
Your main function doesn't call ignoreCaseCompa re().
Mar 29 '10 #3
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.
Mar 29 '10 #4
zamaam0728
13 New Member
I am getting error messages still, but here are the changes I made.
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.     char first = ' '; 
  12.     char second = ' ';
  13.     char answer = ' ';
  14.  
  15.     cout << "Enter a letter: "; 
  16.     cin >> first; 
  17.     cout << "Enter another letter: "; 
  18.     cin >> second; 
  19.  
  20.     answer = bool ignoreCaseCompare (char first, char second)
  21.     cout << "The letters the same: " answer << endl;
  22.  
  23.     return 0; 
  24. //******function defintions***** 
  25. bool ignoreCaseCompare (char one, char two) 
  26.  
  27. one = toupper(one); 
  28. two = toupper(two); 
  29.  
  30.  
  31. {if (one == two) 
  32. return true; 
  33. else 
  34. return false; 
  35.  
  36.     //end main function
Mar 29 '10 #5
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:
Expand|Select|Wrap|Line Numbers
  1.  one = toupper(one); 
  2.  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:
Expand|Select|Wrap|Line Numbers
  1. bool ignoreCaseCompare (char one, char two) 
  2. {
  3.   //code in here belongs to the ignoreCaseCompare method.
  4. }
-Frinny
Mar 29 '10 #6
zamaam0728
13 New Member
I moved
Expand|Select|Wrap|Line Numbers
  1. one = toupper(one);  
  2. 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!
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.     char first = ' '; 
  12.     char second = ' ';
  13.     char answer = ' ';
  14.  
  15.     cout << "Enter a letter: "; 
  16.     cin >> first; 
  17.     cout << "Enter another letter: "; 
  18.     cin >> second; 
  19.  
  20.     answer = bool ignoreCaseCompare (char first, char second)
  21.     cout << "The letters the same: " answer << endl;
  22.  
  23.     return 0; 
  24. //******function defintions***** 
  25. bool ignoreCaseCompare (char one, char two) 
  26.  
  27. one = toupper(one); 
  28. two = toupper(two); 
  29.  
  30.  
  31. {if (one == two) 
  32. return true; 
  33. else 
  34. return false; 
  35.  
  36.     //end main function
Mar 29 '10 #7
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:
Expand|Select|Wrap|Line Numbers
  1. void myMethod()
  2. {
  3.   bool a = true;
  4.   bool b = false;
  5. }
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:
Expand|Select|Wrap|Line Numbers
  1. void myMethod()
  2.  a = true;
  3. {
  4.  
  5.   bool b = false;
  6. }
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
Mar 29 '10 #8
newb16
687 Contributor
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.
Mar 29 '10 #9
zamaam0728
13 New Member
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.
Mar 29 '10 #10

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
16296
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
6206
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
3992
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
2935
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
4210
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
3241
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
10326
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
4678
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
9666
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
10200
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
10145
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
9986
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
9021
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...
1
7530
isladogs
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...
0
6769
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
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.