Calling function doesnt work...

This doesnt work as it should...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void renameMonths(string month)
  {
  if (month == "jan")
    {
    month = "january";
    }
  }

int main()
  {
  string month;
  month = "jan";
  renameMonths(month);
  cout << month << endl;
  }


the output is just jan, when it should be january.

Thanks in advance.
You didn't pass month as a reference.
oh... how do you do that..?

edit, sorry as I am new to C++ I do not know the basics
Last edited on
http://www.cplusplus.com/doc/tutorial/pointers/
What Athar was talking about

And

For the basics
http://www.cplusplus.com/doc/tutorial

But for a quick solution

Just do this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
string renameMonths(string month)
{
    if (month == "jan")
   {
        month = "january";
   }
   return month;
}

int main()
{
   string month;
   month = "jan";
   month = renameMonths(month);
   cout << month << endl;
   cout << "Press ENTER to end the program." << endl;
   cin.get ()
   return 0;
}
closed account (D80DSL3A)
TheMassiveChipmunks solution should work but I think Athar meant that you should pass month to renameMonths() by reference. Change line 1 in your code to:
void renameMonths(string& month)

Take the time to learn the basics or you will be struggling a lot!
Topic archived. No new replies allowed.