[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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");
}
0
MSAJM
Asked:
MSAJM
  • 2
1 Solution
 
jkrCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now