You have too many tests in whatseason; you have not used the final version of the cascaded if/else as your example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//tells you what season it is according to the temperature
void whatseason(double x){
outfile<<"The average is "<<x<<endl;
if(x>=100)
outfile<<"It is roasting season"<<endl<<endl;
elseif(x>=80&&x<100)
outfile<<"It is summer"<<endl<<endl;
elseif(x>=60&&x<80)
outfile<<"It is spring"<<endl<<endl;
elseif(x>=40&&x<60)
outfile<<"It is fall"<<endl<<endl;
elseif(x>=0&&x<40)
outfile<<"It is winter"<<endl<<endl;
elseif(x<0)
outfile<<"It is freezin' season"<<endl<<endl;
return;
}
I honestly have no idea what she means. And shes not one to tell me exactly what to do, instead, she would just give hints here and there. Im really stuck and dont know what to do. Can someone please help me. It would be gratefully appreciated.
remember that you can test a bool without a comparison
What she means is that any non zero value evaluates to true, and a value of 0 evaluates to false. So... you can replace if (validx==true&&validy==true&&validz==true){
with if (validx && validy && validz)
In other words
1 2
if(1)
cout << "Hello";
would print Hello and
1 2
if(0)
cout << "Hello";
would not print Hello.
Also, you can replace elseif(x<0) with just else
For formatting purposed, you shouldn't keep indenting each else if statement because it becomes hard to read. Try it like this:
You have too many tests in whatseason; you have not used the final version of the cascaded if/else as your example.
You carry out some redundant tests in your else if statements. Look at the order of execution logically - if line 7 is reached, that would mean that line 5 would be false, which means that x < 100. So it's already known at line 7 that x < 100, but you test for it regardless.
Yes, I didn't see that at first glance so credit branflakes for that answer:). Make sure to do that for all the if else statements. Also, like I said previously , replace else if(x < 0) with just else for the same reasons that you are replacing the other ones.
//Check to see if temperature>=-10 and <=105
bool validtemperature(int x,int y,int z){
bool validx, validy, validz;
validx=isvalid(x);
validy=isvalid(y);
validz=isvalid(z);
if (validx&&validy&&validz)
returntrue;
elsereturnfalse;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//tells you what season it is according to the temperature
void whatseason(double x){
outfile<<"The average is "<<x<<endl;
if(x>=100)
outfile<<"It is roasting season"<<endl<<endl;
elseif(x>=80)
outfile<<"It is summer"<<endl<<endl;
elseif(x>=60)
outfile<<"It is spring"<<endl<<endl;
elseif(x>=40)
outfile<<"It is fall"<<endl<<endl;
elseif(x>=0)
outfile<<"It is winter"<<endl<<endl;
else
outfile<<"It is freezin' season"<<endl<<endl;
return;
}
I mean you can replace the code in validtemperature with one line that does the same thing. When I say "without reducing readability" I mean usually when you can reduce a bunch of operations down to one line it typically becomes an obfuscated piece of code. Not in this case.
will always return true because the expression isvalid evaluates to the address of the function, which is non-zero, which is true. Check out the code that Maese909 posted above but try doing that without using the three variables.
//Check to see if temperature>=-10 and <=105
bool validtemperature(int x,int y,int z){
bool validx, validy, validz;
validx=isvalid(x);
validy=isvalid(y);
validz=isvalid(z);
Then delete this
1 2 3 4 5
if (validx&&validy&&validz)
returntrue;
elsereturnfalse;
}
Not quite, like Shacktar stated, you want the only line of code in your function to look like this: return (X);
Here's another hint, use the three variables declared in your parameter list (x, y, z), call the function isvalid for each, and totally disregard the other variables you have declared.
Keep in mind that (true && true && true) will return a true