I am trying to code the below program using overloaded function which give me output is the answer in correct or not for the three different question, the answers are already provided in the program all I have to check the answer and give the output statement if the answer is correct or incorrect, I am able to run the below mentioned code but the output is weired, please look at it and help me with this.

#include <cstdlib>
#include <iostream>

using namespace std;
void checkQuestion(int, string, bool);

int main(int argc, char *argv[])
{
    int intAnswer = 3;
    string stringAnswer = "Abraham Lincoln";
    bool boolAnswer = true;
    string result[]={"correct", "incorrect"};
    
    void checkQuestion (int intAnswer);
              
              if (intAnswer == 3)
    {
         cout << "This answer is " << result <<endl;
    }
        
   void checkQuestion (string stringAnswer);
   
             if (stringAnswer == "Abrahim Lincoln")
   {
        cout << "This answer is " << result << endl;
   }
   
  void checkQuestion (bool boolAnswer);
  
           if (boolAnswer == true)
  {
       cout << "This answer is " << result << endl;
  }

system("PAUSE");
return EXIT_SUCCESS;
}

Voila! Syntax-highlighting!

Why did you put your functions inside main() ?, I think you should move them out. (I'm not an expert, so maybe I'm wrong)
Result is an array of strings, when you use cout << "This answer is " << result << endl; you must be getting something like:
This answer is Array or... something similar.

You need to tell the program which array will it show: cout << "This answer is " << result[0] << endl;

#include <cstdlib>
#include <iostream>

using namespace std;

// The function checkQuestion is being overloaded
// Overloading a function means having the same function name
// but multiple definitions with different parameter types exist
// so here there is basically an int, string and bool version 
// of checkQuestion
void checkQuestion(int intAnswer, string result[]);
void checkQuestion(string stringAnswer, string result[]);
void checkQuestion(bool boolAnswer, string result[]);

int main(int argc, char *argv[])
{
    int intAnswer = 3;
    string stringAnswer = "Abraham Lincoln";
    bool boolAnswer = true;
    string result[]={"correct", "incorrect"};
    
    // Call the 3 different functions using the above variables
    // Note: the result argument can be removed and simplely just
    // replace the cout << result in the functions with
    // either cout << "correct"; or cout << "incorrect"; 
    checkQuestion(intAnswer, result);
    checkQuestion(stringAnswer, result);
    checkQuestion(boolAnswer, result);



system("PAUSE");
return EXIT_SUCCESS;
}

void checkQuestion (int intAnswer, string result[]){
    cout << "This intAnswer answer is ";

    if (intAnswer == 3)    
         cout << result[0];
    else
        cout << result[1];

    cout << endl;
}   
void checkQuestion (string stringAnswer, string result[]){
    cout << "This stringAnswer answer is ";

    if (stringAnswer == "Abrahim Lincoln")
         cout << result[0];
    else
        cout << result[1];

    cout << endl;
}   

void checkQuestion (bool boolAnswer, string result[]){
    cout << "This boolAnswer answer is ";

    if (boolAnswer == true)  
         cout << result[0];
    else
        cout << result[1];

    cout << endl;
}

There ya go bud hope you understand what function overloading is now. well cyaz.

Thanks for the help, but what i did is bit different form you, please have a check, the code is below, it runs fine but, though i am new to C++ just to have an opinion that I am doing right stuff or not?

#include <cstdlib>
#include <iostream>

using namespace std;
void checkQuestion(int, string, bool);      //Function dacleration
int main(int argc, char *argv[])

{
    int intAnswer = 3;
    string stringAnswer = "Abraham Lincoln";
    bool boolAnswer = true;
    string result[]={"correct", "incorrect"};
    int i=0;
    
void checkQuestion (int intAnswer);       //Calling function

if (intAnswer == 3)
     {
 cout << "1. This answer is " << result[0]<<endl;
        }
else if (intAnswer != 3)
       {
cout <<"1. This answer is " <<result[1] <<endl;
       }

void checkQuestion (string stringAnswer);   //Calling function
   
if (stringAnswer == "Abraham Lincoln")
      {
cout << "\n2. This answer is "<< result[0] << endl;
      }
else if (stringAnswer != "Abraham Lincoln")
      {
cout << "\n2. This answer is "<<result[1]<<endl;
      }

void checkQuestion (bool boolAnswer);    //calling function

if (boolAnswer == true)
      {
cout << "\n3. This answer is " << result[0] << endl;
      }
else if (boolAnswer != true)
     {
cout <<"\n3. This answer is "<< result[1]<< endl;
     }

cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Voila! Syntax-highlighting!

Thanks for the help, but what i did is bit different form you, please have a check, the code is below, it runs fine but, though i am new to C++ just to have an opinion that I am doing right stuff or not?

#include <cstdlib>
#include <iostream>

using namespace std;
void checkQuestion(int, string, bool);
int main(int argc, char *argv[])

{
    int intAnswer = 3;
    string stringAnswer = "Abraham Lincoln";
    bool boolAnswer = true;
    string result[]={"correct", "incorrect"};
    int i=0;
    
void checkQuestion (int intAnswer);

     if (intAnswer == 3)
     {
        cout << "1. This answer is " << result[0]<<endl;
        }
             else if (intAnswer != 3)
             {
                  cout <<"1. This answer is " <<result[1] <<endl;
                  }
void checkQuestion (string stringAnswer);
   
                   if (stringAnswer == "Abraham Lincoln")
                   {
                      cout << "\n2. This answer is "<< result[0] << endl;
                      }
                           else if (stringAnswer != "Abraham Lincoln")
                           {
                                cout << "\n2. This answer is "<<result[1]<<endl;
                                }
void checkQuestion (bool boolAnswer);
  
                                     if (boolAnswer == true)
                                     {
                                        cout << "\n3. This answer is " << result[0] << endl;
                                        }
                                             else if (boolAnswer != true)
                                             {
                                                  cout <<"\n3. This answer is "<< result[1]<< endl;
                                                  }
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Voila! Syntax-highlighting!

The syntax highlighting is fine. The indentation boggles the mind. This is much more readable:

if (a == b)
{
    // do stuff
}
else if (a == c)
{
    // do other stuff
}
else
{
    // do something else
}

What's up with lines 15, 25, and 35? What are these lines? Function declarations? Function calls? The functions themselves? They're in the middle of main ().

i think think is better but not perfect: http://clipboard.it/v/ibb/

This function:

bool checkQuestion (bool boolAnswer)
{
    if (boolAnswer)
     return true;
    
    return false;
}

can be reduced to this:

bool checkQuestion (bool boolAnswer)
{
   return boolAnswer;
}

So it's a function that simply returns its parameter.

To the OP, you should probably define explicitly what the program is supposed to do and also explain explicitly what each function is supposed to do.

ok, but what if I want to "call checkQuestion function" in the main () and then "write the checkQuestion function" outside of the main(), I hope you understand what I mean...if yes then how to call the function in the main() and write the function outside of main?


#include <cstdlib>
#include <iostream>

using namespace std;

// The function checkQuestion is being overloaded
// Overloading a function means having the same function name
// but multiple definitions with different parameter types exist
// so here there is basically an int, string and bool version 
// of checkQuestion
void checkQuestion(int intAnswer, string result[]);
void checkQuestion(string stringAnswer, string result[]);
void checkQuestion(bool boolAnswer, string result[]);

int main(int argc, char *argv[])
{
    int intAnswer = 3;
    string stringAnswer = "Abraham Lincoln";
    bool boolAnswer = true;
    string result[]={"correct", "incorrect"};
    
    // Call the 3 different functions using the above variables
    // Note: the result argument can be removed and simplely just
    // replace the cout << result in the functions with
    // either cout << "correct"; or cout << "incorrect"; 
    checkQuestion(intAnswer, result);
    checkQuestion(stringAnswer, result);
    checkQuestion(boolAnswer, result);



system("PAUSE");
return EXIT_SUCCESS;
}

void checkQuestion (int intAnswer, string result[]){
    cout << "This intAnswer answer is ";

    if (intAnswer == 3)    
         cout << result[0];
    else
        cout << result[1];

    cout << endl;
}   
void checkQuestion (string stringAnswer, string result[]){
    cout << "This stringAnswer answer is ";

    if (stringAnswer == "Abrahim Lincoln")
         cout << result[0];
    else
        cout << result[1];

    cout << endl;
}   

void checkQuestion (bool boolAnswer, string result[]){
    cout << "This boolAnswer answer is ";

    if (boolAnswer == true)  
         cout << result[0];
    else
        cout << result[1];

    cout << endl;
}

There ya go bud hope you understand what function overloading is now. well cyaz.

Scott has written the functions outside of main. Remember main is in and of itself a function (method really).
In the code, you close off main() with } and then give the function _definition_ with return type, parameters, etc.
Your _declarations_ (yes you do need all the ones Scott put there for the overloaded function as the definition of overloaded is having a different signature (the types and count of parameters) from other "same name" functions) at the top tell the compiler "ok, these functions are somewhere and they take these parameter types, so look for them where you usually do."
This is how you can include a header file (which usually contains _declarations_ among other things) but not have the functions in your code.
Not sure if that clarifies it or makes it worse. I'm hopeful for the former.

scott has written the functions outside of main. Remember main is in and of itself a function (method really).
In the code, you close off main() with } and then give the function _definition_ with return type, parameters, etc.
Your _declarations_ (yes you do need all the ones scott put there for the overloaded function as the definition of overloaded is having a different signature (the types and count of parameters) from other "same name" functions) at the top tell the compiler "ok, these functions are somewhere and they take these parameter types, so look for them where you usually do."
this is how you can include a header file (which usually contains _declarations_ among other things) but not have the functions in your code.
Not sure if that clarifies it or makes it worse. I'm hopeful for the former.

i am still so confused, how can you call a function with return statement? Can you explain with some example..

Here's a skeleton code:

int multbytwo(int);
int main()
{
       int myreturnedvalue;
       int myinputvalue = 3;
       myreturnedvalue = multbytwo(myinputvalue);
                         (so this has returned an answer that we can 
                           now  assign to a fresh variable)
       since it does this returning we could also do
       std::cout << multbytwo(myinputvalue);
       return 0;
}

int multbytwo(int numberIwantmultipliedbytwo)
{
     either   
            int multipliedanswer;
            multipliedanswer=2*numberIwantmultipliedbytwo;
            return multipliedanswer;
   
     or (more succinctly)
            
             return 2*numberIwantmultipliedbytwo;

}

(and a void returns nothing so either you have to pass in something (or multiple parameters) by reference or output what's going on in your function)

Here's the normal layout I use (changed as needed, but pretty standard):

// #include statements go here
// "using namespace std" (if desired) here
// #define statements here
// global variables here
// function declarations here

int main ()
{
  // code, including function CALLS from main.
  return 0;
}

// functions go here

Function declarations end with a semicolon. Functions don't:

An example...

#include <iostream>
using namespace std;

int Sum (int array[], int numElements);

int main ()
{
    int someNumbers[3];
    someNumbers[0] = 3;
    someNumbers[1] = 2;
    someNumbers[2] = 4;
    int theSum = Sum(someNumbers, 3);
    cout << "The sum is " << sum;
    return 0;
}

int Sum (int array[], int numElements)
{
  int sum = 0;

  for (int i = 0; i < numElements; i++)
  {
    sum += array[i];
  }

  return sum;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.