This doesnt work as it should...

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.

It works exactly as it should. You're passing month by value, so within renameMonths() "january" is being assigned to a copy. The original object in main() remains the same. To change the original object, you need to pass a reference (or a pointer):

void renameMonths(string& month)
{
    if (month == "jan") {
        month = "january";
    }
}

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

Ah... thanks a ton,
but how is a reference (&) different from a pointer(*)?

In overly simplified terms, a Reference is a simplified form of a pointer. They work similarly, but they don't have all the extra syntax, and aren't quite as powerful.

Pointers are explicit while references are implicit and more of a synonym than an object in their own right. Here's the same code using a pointer instead of a reference:

void renameMonths(string *month)
{
    if (*month == "jan") {
        *month = "january";
    }
}

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

Notice how renameMonths() must dereference the pointer to get to the string, and the call to renameMonths() must pass the address of the object. All of this is hidden from you with references (which are typically implemented as pointers under the hood).

Now, because references are a synonym for an object, there's no way to define a reference to nothing. However, you can have a null pointer that points to nothing. So one reason why a pointer might be used instead of a reference is if you need to represent a nonexistent object.

Oh wow thanks!!
I sort of get it haha (as you can see I'm really really new to C++).

This is my real code, but I have another minor problem..

void renameMonths(string& month)
  {
  if (month == "jan" or "january")
    {
    month = "January";
    }
  if (month == "feb" or "february")
    {
    month = "February";
    }
  if (month == "mar" or "march")
    {
    month = "March";
    }
  if (month == "apr" or "april")
    {
    month = "April";
    }
  if (month == "may")
    {
    month = "May";
    }
  if (month == "jun" or "june")
    {
    month = "June";
    }
  if (month == "jul" or "july")
    {
    month = "July";
    }
  if (month == "aug" or "august")
    {
    month = "August";
    }
  if (month == "sep" or "september")
    {
    month = "September";
    }
  if (month == "oct" or "october")
    {
    month = "October";
    }
  if (month == "nov" or "november")
    {
    month = "November";
    }
  if (month == "dec" or "december")
    {
    month = "December";
    }
  }

But any "month" I input automatically changes to 'December,' I've been tinkering around for a few hours now... not sure what the problem is..

The left hand side of an expression doesn't carry over, so month == "dec" or "december" is translated as month == "dec" or "december" != 0 . Since "december" is never going to be a null pointer, it's essentially month == "dec" or true , which isn't very helpful. ;) What you really want is month == "dec" or month == "december" . Naturally that should apply to all of your if statements.

It would also be a good idea to use an if..else if..else chain to avoid checking all of the months after a match.

Thank you SO MUCH Narue!
You are the best!! I keep getting stuck on these little things.. hopefully I'll be able to spot such things better :(

Wow, I didn't know C++ had or, I thought only Python had it, and I always used ||. Is "or" a standard or is it just added by the compilers, because they are so nice?

Wow, I didn't know C++ had or, I thought only Python had it, and I always used ||. Is "or" a standard or is it just added by the compilers, because they are so nice?

There is no "or" operator. Narue's discussion is written in pseudocode. The actual code would be if ((month == "dec") || (month == "december"))

Well, in ksm092's code, there is or, and I checked it with gcc, and it compiled. My IDE even changed the color of the word "or", to blue, so it clearly does exist, not only in pseudo code.

There is no "or" operator.

Actually, there is. It's part of the alternative tokens. Though they're not often used, and some compilers (such as Visual C++) disable the keywords by default. As an alternative, <ciso646> defines them as macros rather than keywords.

Thanks for info :)

So it's not "strictly" correct and is loosely defined, but it is part of the standard...?

Interesting, I've never seen it in anything I've read. (Admittedly, I haven't read the standard, I don't have a copy of it.)

EDIT:
It seems to work in VC++2008 as well.

So it's not "strictly" correct and is loosely defined, but it is part of the standard...?

Um, it's strictly correct and quite well defined. I'm not sure I understand what the problem is here aside from alternative tokens being uncommon (probably because few people know about them) and Microsoft disabling them when extensions are disabled (likely due to a belief that they'll break existing code). Actually, I do understand the problem, but that doesn't spawn entertaining conversation, does it? ;)

Interesting, I've never seen it in anything I've read.

To be fair, the only book I've seen alternative tokens seriously used in is Ray Lischner's Exploring C++.

Admittedly, I haven't read the standard, I don't have a copy of it.

The draft is freely available online, up to date, and sufficiently complete for anyone who's not trying to write a conforming compiler.

>>The draft is freely available online, up to date, and sufficiently complete for anyone who's not trying to write a conforming compiler.
Can you provide a link? I've found several via google, but I'm not sure which would be the most reliable. I would really like to get at that.

This is the current draft of C++0x directly from the committee website.

commented: I did find that link. I thought it was. Thx. :) +13
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.