calling function

I am really confused with function calling in C++.  With JavaScript you can simply type in functionName() and it will call the function, but I cannot get this to work. I know there is something simple that I am missing here, but I have no earthly idea what it is.  Can someone tell me what I need here to call this function.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// reverseit function goes here
void reverseIt(char textArray[],int no)
     {
     char text;
     int len, x, mid = 0;
            
       if(no==0)
     {
         for(len=0,x=0; textArray[x] !='\x0';len++,x++);
     }
       else
           {
            len = 0; len = no;
         }

       mid = (len-1)/2;

       for(int i=0,j=len-1; i <=mid; i++,j--)
         {
             text = textArray[i];
               textArray[i] = textArray[j];
               textArray[j] = text;
         }
         
      cout << endl << textArray;
}

//main() funtion goes here
int main()
    {
            char str[50];
            cout << "Enter string to be reversed: ";
            cin.get(str, 50);
            cout << endl;
            //call to reverseIt funcion
            cout << "Reversed string is: " << str << endl;

      return 0;
          
system("pause");
}
MSAJMAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Just use it like

nt main()
    {
          char str[50];
          cout << "Enter string to be reversed: ";
          cin.get(str, 50);
          cout << endl;
          //call to reverseIt funcion
          reverseIt(str,strlen(str));
          cout << "Reversed string is: " << str << endl;

     return 0;
         
system("pause");
}
0
 
jkrCommented:
BTW, to elaborate - you need to supply the parameters correctly. Since the function is declared as

void reverseIt(char textArray[],int no)

the 1st argument is the char buffer, the second is the number of characters to reverse. In

        reverseIt(str,strlen(str));

they're both supplied, with the length being calculated using 'strlen()'. You'll need to add

#include <string.h> for that.

As a side note, that could be easier by not mixing C and C++, but that would require some biggr changes to the above code.
0
 
MSAJMAuthor Commented:
Thanks for your help and the additional info!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.