Hello maddimarrone,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
As
jlb pointed out.
else if ((timeOfgreeting >= 1700) || (timeOfgreeting < 2300))
This is an || (OR) condition. Which means that only one side has to be true. When checking a range like this the && (AND) both sides have to be true. So
else if ((timeOfgreeting >= 1700) && (timeOfgreeting < 2300))
And say that the time is 1800 it is both greater than 1700 and less than 2300.
Also, should my variable in my tests then be "time" not "timeOfgreeting"?
|
Looking at your function it takes one parameter (time) then you define
int timeOfgreeting;
which is uninitialized and contains a garbage value, whatever is in the memory location set aside for "timeOfgreeting" and tries to make an "int" out of this. This does not work as you may get a value of (-858993460) which could be different on your computer.
What you could have done is
int timeOfgreeting = time;
. This makes use of the parameter of the function. Or better is not define "timeOfgreeting" and just use "time". Most times it is better to use what you have and not do something you do not need.
else if ((time >= 2300 && time <= 2400) || (time >= 0 && time <= 400))
. This does work, but all you really need is
else if (time >= 2300 || time <= 400)
. Here the || (OR) works because of the two different times do not make a good range to check.
Based on what
jlb said I came op with this, which may still be improved on, but works with the tests I did.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
string time2greeting(int time) // add the parameter here // computes the greeting at a certain time
{
// add logic here to return
string greeting;
//int timeOfgreeting = time;
// What are you doing up, between 11PM and 4AM
if (time > 2259 || time < 400)
greeting = "What are you doing up, ";
// Good Evening from 5PM to 11PM
else if (time > 1659)
greeting = "Good Evening, ";
// Good Afternoon from Noon to before 5PM
else if (time > 1159)
greeting = "Good Afternoon, ";
else if (time > 359)
greeting = "Good Morning, ";
//For illegal values, say:
else
greeting = "That is an illegal time, ";
return greeting;
}
|
Line 5 I left to show you what you should have done, but this variable is not needed.
Next notice that the if/else if statements are in reverse order. Sometimes you have to do it this way to make it work out especially when only having one condition to check.
Next problem is that you defined "greetings", but never gave it a value before you returned the string. The "cout" statements should not be in the function, but back in "main" where you need it, so setting "greatings" to a different value makes more sense here.
Although you are free to name a variable any way that you want it is generally accepted that regular variables start with a lower case letter. Classes and structs start with a capital letter and variables defined as a constant use all capital letters. This is not a written rule, but what most people follow along with using "camelCase" with a name that is two or more words or using the underscore (_) to denote a space where a space is not allowed.
If you are still watching this it may help.
Andy