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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zamaam0728
    New Member
    • Mar 2010
    • 13

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

    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:

    Code:
    #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?
    Last edited by Banfa; Mar 29 '10, 11:32 AM. Reason: Added [code]...[/code] tags
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    move toupper inside ignoreCaseCompa re

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Your main function doesn't call ignoreCaseCompa re().

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        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.

        Comment

        • zamaam0728
          New Member
          • Mar 2010
          • 13

          #5
          Still lost

          I am getting error messages still, but here are the changes I made.
          Code:
          #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
          Last edited by Frinavale; Mar 29 '10, 07:09 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            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:
            Code:
             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:
            Code:
            bool ignoreCaseCompare (char one, char two) 
            {
              //code in here belongs to the ignoreCaseCompare method.
            }
            -Frinny

            Comment

            • zamaam0728
              New Member
              • Mar 2010
              • 13

              #7
              I moved
              Code:
              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!
              Code:
              #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
              Last edited by Frinavale; Mar 29 '10, 07:48 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                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:
                Code:
                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:
                Code:
                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

                Comment

                • newb16
                  Contributor
                  • Jul 2008
                  • 687

                  #9
                  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.

                  Comment

                  • zamaam0728
                    New Member
                    • Mar 2010
                    • 13

                    #10
                    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.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      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

                      Comment

                      • zamaam0728
                        New Member
                        • Mar 2010
                        • 13

                        #12
                        Ok...I got it to go through with no error messages, but it's not displaying true or false.
                        Code:
                        #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
                        Last edited by Frinavale; Mar 30 '10, 01:08 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

                        Comment

                        • zamaam0728
                          New Member
                          • Mar 2010
                          • 13

                          #13
                          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.

                          Comment

                          • zamaam0728
                            New Member
                            • Mar 2010
                            • 13

                            #14
                            It may not be pretty but here it is:
                            Code:
                            #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
                            Last edited by Frinavale; Mar 30 '10, 01:09 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              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:
                              Code:
                              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:
                              Code:
                              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

                              Comment

                              Working...