I am basically new to C++. After being quite adept at Applesoft BASIC & Pascal, back in the day, it is frustrating for me to seem so clueless. I do clearly understand the difference between high & low level languages, but its still frustrating.
This is for a class I am taking at SFSU, and I have tried everything under the sun that I can see, or think, to try, and yet... I feel that it is staring me in the face, but I have looked at the code for to long, and am not seeing it. Which is why I am asking for help from the larger community. Here is the very simple code, part of the larger program, that when I resolve the issues here, I will understand how to apply them overall (hopefully).
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string monthNum;
string dateNum;
string month;
string day;
int nineTeen = 19;
string tens;
string year;
cout << "\n\nPlease enter a date in the format MM/DD/YY: ";
cin >> dateNum;
tens = dateNum.substr(6,2);
day = dateNum.substr(3,2);
monthNum = dateNum.substr(0,2);
{
if (monthNum = "01" )
month = "Janurary";
else
if (monthNum = "02" )
strcpy(month, "Feruary");
else
if (strcmp(monthNum, "03")) = 0)
strcpy(month, "March");
Just after the line containing the first "if", I am getting an error stating "Could not convert monthNum. sd::basic_string..." The other "if" statements, and subsequent, are giving me similar issues. The above three "if" evaluations are the various forms I have tried, to no avail.
Would very much appreciate a bit of guidance, not a complete answer, mind, but just a slap upside the head as to what I am missing/not seeing.
C++ is still high-level, but it is lower-level than things like BASIC or Pascal.
MM/DD/YY
And I thought this had been out-grown.
if (monthNum = "01")
You must mean
if (monthNum == "01")
Common newbie error.
strcmp(monthNum, "03"))
std::string already has a comparison method. The operator==. You don't need to use strcmp(), but if you really want to, you're doing it wrong:
1 2 3
if (strcmp(monthNum.c_str(), "03")) == 0){
//...
}
std::string::c_str() returns a pointer to the internal C string. You can't assign a value to the return value of a function.
[qoute]strcpy(month, "March");[/quote]You can't use any of the C library functions directly on an std::string, but it's not wise to mix C++ types with C functions, anyway.
helios & firedraco, I thank you very much for the clarification. As I expected, it was something I knew, but had not completely assimilated, and when I went over the code... my brain was flat lining. Quickly got rid of the rubble, and it complied fine. Again, my thanks.
One follow up question, if I may. Same bit of code, but do you see any reason the program would hang/stop following the "cin >>"?
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string monthNum;
string dateNum;
string month;
string day;
int nineTeen = 19;
string tens;
string year;
cout << "\n\nPlease enter a date in the format MM/DD/YY: ";
cin >> dateNum;
tens = dateNum.substr(6,2);
day = dateNum.substr(3,2);
monthNum = dateNum.substr(0,2);
No, perhaps I miss stated that. I meant the hang occurred after the user had input the date, and pressed return. I found the problem; apparently, it does not accept the "Enter" key as indicating you are finished with your input. I must use the "Return" key, and then it works fine.
It might be because the return key is sending a \r, and cin wants a \n (enter might send \n or \r\n, I don't know). Don't quote me on this though...I don't have a return key on my keyboard.