-2

My assignment is to create a calendar when the user enters a month and year. All of that is working great, but I can't figure out how to update a char monthName[256]; variable. My guess is monthName = "January" (or whichever month), but that doesn't work. Not sure what I'm doing wrong.

FWIW, the user doesn't enter the month name. I have to print it to the screen based on the month number the user enters.

Ron
  • 14,674
  • 4
  • 34
  • 47
Great Scott
  • 1,325
  • 15
  • 22
  • 1
    If you use `std::string` instead of `char []` you could use `monthName = "January"` – Nogoseke Feb 23 '18 at 22:20
  • 2
    If you for some unknown reason must use `char[]` use [`sprintf_s`](https://msdn.microsoft.com/pl-pl/library/ce3zzk1k.aspx) or [`snprintf`](http://www.cplusplus.com/reference/cstdio/snprintf/) or `strncpy`. – Poeta Kodu Feb 23 '18 at 22:22
  • It is reasonable to believe he has to use C types. It's a *intro to Computer Science* class. It's believable that they are just studying C currently before advancing to C++ enhancements. – Yotam Salmon Feb 23 '18 at 22:23
  • Whoa! Lots of hate here. Why all the downvotes? What? Noobs aren't allowed to ask questions? – Great Scott Feb 23 '18 at 22:25
  • 1
    You're right, @YotamSalmon. We're just getting started. C vs. C++ is greek to me at this point. Thanks for understanding. – Great Scott Feb 23 '18 at 22:27
  • Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Feb 23 '18 at 22:28
  • @Scott Welcome. When I used to teach intro to CS using C, I too used C++ compilers for my students and did not make the distinction between the two. So it's understandable. – Yotam Salmon Feb 23 '18 at 22:30
  • Even if they are "learning C", chances are the university or textbook is presenting it as C++ and it is fundamental to career success that the difference be learned and be learned early. Use std::string for text in C++ unless your teacher explicitly prohibits it, and even then ask why it is being prohibited. Later on, you might go c-style string or std::string depending on performance requirements or if you have some requirement for a fixed memory size or something. But do, learn what "C-style string" means, how to use it, and how to use "std::string" on day one! – Christopher Pisz Feb 23 '18 at 22:32
  • @Scott it's usually not a very productive idea to *guess* what you can or should do. I would recommend that you ask your instructor to teach you how arrays can be modified - surely they must do so before they ask you to do an assignment where you need to modify an array. – eerorika Feb 23 '18 at 22:56
  • You would think. Believe me, there has been a lot of complaining from the class due to the inadequate textbook material. – Great Scott Feb 23 '18 at 23:05

2 Answers2

5

You should use std::string instead, such as std::string monthName = "Jan"; monthName = "Feb";.

If there is some reason you must use C-style strings, then std::strcpy will solve your problem. Such as std::strcpy(monthName,"Jan");, which will copy the string "Jan" into the character array. You cannot assign a character string directly to an array like that.

Daniel
  • 1,291
  • 6
  • 15
0

You are not able to change the address of the string monthName by design, it's constant in C/C++. You can't change the variable, but only its indexes (monthName[0] = 'J' would work).

You can read more about how string literals and how char arrays are saved here

Yotam Salmon
  • 2,400
  • 22
  • 36
  • 1
    1. this is not C, and 2. arrays are not pointers in C nor in C++. – Quentin Feb 23 '18 at 22:22
  • 1
    I believe C and C++ are no different in the matter I wrote (char arrays), although I did miss the fact he wrote C++. And [this](https://stackoverflow.com/a/1642423/3873323) is what I meant when said pointer. Of course the term "address" is more accurate. – Yotam Salmon Feb 23 '18 at 22:26