I wrote a program to test my skills in using classes and now I'm stuck. I'm basically doing input validation right now. The third do while loop is giving me some problems. I want it to only accept 4 digits for the year. I am trying to use the length() command. This statement:
} while (yearString.length() != 4);
It's giving me: "dateProg.cpp:83:11: error: ‘yearString’ was not declared in this scope"
But I did declare it here:
The only thing I can think of is that my data members for "setYear/getYear" are set to int right now, this may be causing a problem. I'm trying to use to_string in there as well as I can't figurte out a way of using length without using a string.
Also, I would like to know if there is a more elegant way of doing my input validation. I am using a local variable (i) and incrementing it so the "invalid month!" "Invalid Day!" only comes up once. How else would I have the "input Day" prompt, for instance come up everytime, then the "Invalid Day" prompt come up only when invalid? I feel I'm using a workaround, but it works.
Ok, I did not know that, thanks! It is compiling now but the year validation is doing an infinite loop, even when I put in a 4 digit number. Could this be where me using int's for my classes, setters and getters might be causing the problem?
OMG! I never would have thought that putting std::string in front of it would make that big of a difference! Thank you! - If I could ask one more thing. Why? Why does that make such a big difference?
(Type) (VariableName) = (Data); is declaring a new variable and initializing it with data
(VariableName) = (Data); is assigning data to a variable that ought to already exist.
When a declared parameter in an inner scope has the same name as the outer scope, it's called "shadowing" a variable -- a new variable is created, which has nothing to do with the former variable declared in an outer scope -- and it's best to avoid doing this. https://en.wikipedia.org/wiki/Variable_shadowing
"shadowing" was how you had it before. The yearString variable inside the loop shadowed the yearString variable that was declared outside the loop. After you removed std::string inside the loop there is only one variable named "yearString" and hence no shadowing.