Hot Mess: Conditional Statement with strncmp will not Execute - visual-c++

I feel like my code is what the kids would call a hot mess, but more like someone,i.e. me, took a fresh dump on everything. I wrote this in C++ to practice, but things a not happening the way I want it to.
//validation: check for other values other than three temp units
} while ((strncmp(temp_data.current_temp, KELVIN, strlen(temp_data.current_temp)) +
strncmp(temp_data.current_temp, CELSIUS, strlen(temp_data.current_temp)) +
strncmp(temp_data.current_temp, FAHRENHEIT, strlen(temp_data.current_temp))==0) &&
(strncmp(local_convert_temp, KELVIN, strlen(local_convert_temp)) +
strncmp(local_convert_temp, CELSIUS, strlen(local_convert_temp)) +
strncmp(local_convert_temp, FAHRENHEIT, strlen(local_convert_temp)) == 0));
I tried a different iteration using more of a conditional statement instead of simple arithmetic, but that didn't work either. I took the entire statement apart and each strncmp is returning a 0 value when the condition is met; with or without the macro. The situation is the same with this piece of the code:
strncmp(current_temp, CELSIUS, strlen(current_temp)) + strncmp(convert_temp, KELVIN, strlen(convert_temp)) == 0)
I tried taking the statement apart check to see if the in-block statement will execute if the condition is met and that works. The issue arise when I make the conditional statement more complex. What I am expecting is that when both conditions are met the statement will simply execute.

Related

If(AND) combination produces "You've entered too many arguments for this function" error

hello everyone I'm here and needs help with excel
message from excel is
(you've entered too many arguments for this function)
and that my function.
=IF(AND(H2="A","B"),"group 1",IF(AND(N2H2="C","D"),"group 2",""))
please any one can help ?
The first condition to test within the IF statement "AND(H2="A","B")" will always return false - as this tests "Is H2 = "A" ? AND "Is '"B"'?
H2 cannot equal "A" and "B" simultaneously, and besides, even if you wanted to test that you'd have to use "AND(H2="A", H2="B").
Imagine someone asking you the Q "Is H2 = "A"?"
You could answer this if you knew what was in cell H2 (like Excel does) - and you could answer 'True' (yes) or 'False' (no).
But if you were asked is "B" as well? you would probably be quite puzzled - perhaps you would reply "Is "B" what as well"?
Excel would also be puzzled and in such circumstances the default response is 'False' (until proven otherwise!)
The 2nd AND statement is the wrong syntax - see here for some examples of how to use the AND statement.
This is probably why you are seeing the error you see - again:
If someone asked you "Is N2H2="C" and is "B" as well?
You wouldn't know which cell I was referring to (N2H2 does not exist in Excel)
Further, you wouldn't know what to say to "Is "B"?" (Is it a consonent? Is it capital? Is it the 2nd letter of the alphabet? Is it what?
Using this equation Excel will not return an error "too many arguments", it will return "invalid name error" (because there is no such thing as "N2H2" as far as Excel is concerned")
You need to use the following generic syntax for AND statements of this type:
AND(cell 1=some value1, cell 2= some value2) - not AND(cell1cell2=some value1, some value2)
i.e. this would be correct syntax:
=IF(AND(H2="A",N2="B"),"group 1",IF(AND(H2="D",N2="C"),"group 2",""))
(but it assumes you are trying to test cell N2 = B in the first AND statement because it's impossible for cell H2 to equal both A and B at the same time as I've said above)
As someone has pointed out in the comments - if you're testing whether H2 can be "A" OR "B" then simply use the OR statement - i.e. something like this:
=IF(OR(H2="A",H2="B"),"group 1",IF(AND(H2="D",N2="C"),"group 2",""))
https://support.microsoft.com/en-us/office/and-function-5f19b2e8-e1df-4408-897a-ce285a19e9d9

Combining CONCAT, DATEDIF, and nested IF -getting #VALUE! error

Trying to fill an investment field with Short/Long Term & Capital Gain/Loss. Ending values should be one of the following - STCG, STCL, LTCG, LTCL. (I tried to cut/paste an excel image but it's icky, sorry)
Mini Spreadsheet - Date Acquired (H62), G/L per share (J62), Position (result)
For some investments, the date acquired is Var (for various purchases) or blank (for cash). If it is "Var", the function should return "LT" and skip the DATEDIF function. If the date is blank, the function should be skipped entirely and return blank as there is neither gain nor loss.
=IF($H62="","",CONCAT(IF(OR($H62="Var", DATEDIF($H62,TODAY(),"d")<365),"ST","LT"),IF($J62<0,"CL","CG")))
My original function worked well, except when the Date Acquired field (H62) had "Var", I got a #VALUE! error. I tried to do this with nested IFs and with IFS function, but couldn't get it to work (Problem with this formula error): =IF(H61="","",CONCAT(IF($H61="Var", "LT",IF(DATEDIF($H61,TODAY(),"d")<365),"ST","LT")),IF(J61<0,"CL","CG")))
The logic should be: If date="" then "" else concat LT/ST test and CL/CG test
LT/ST test: If date="Var" then "LT" else if date days before today < 365,"ST" else "LT"
CL/CG test: If gain per share <0 "CL" else "CG" (working)
Please help! I could add a hidden column (I'd rather not) or do the whole thing with nested IFs or IFS but I'd have to repeat arguments and that's sloppy & asking for trouble.

Multiple IF statements in an Excel spreadsheet

I've researched this quite a bit but to no avail, I've tried to do a choose with:
=CHOOSE(MATCH(R5,{"PASS","MERIT","DISTINCTION"},FALSE),"70","80","90")
Alternatively I've done:
=IF(R5="PASS","70"), IF(R5="MERIT","80"), IF(R5="DISTINCTION","90")
The plan is to see what the input would be for the cell R5, if it is pass then the cell that is selected which is U5 would show the appropriate points.
Pass is 70 points, Merit is 80 and a Distinction would be 90 so you can see my reasoning behind the choices in my code. Not entirely sure how to go about it. Also tried using ORs in the IF statement but I might not have coded it in completely correctly.
The Excel IF statement is defined as:
=IF(condition,value_if_true,value_if_false)
You can substitute the values with additional statements, so it becomes:
=IF(R5="PASS","70",IF(R5="MERIT","80",IF(R5="DISTINCTION","90")))
This is equivalent to the following pseudo-code:
if(R5 equals "PASS"){
"70"
} elseif (R5 equals "MERIT") {
"80"
} elseif (R5 equals "DISTINCTION") {
"90"
}

fminsearch is overwriting in loop while storing data

I have the code mentioned below in matlab. I want to write all the 162 rows and 4 columns calculated into an excel file.
When i use xlswrite in the code i get only one row and 4 columns as the value of P gets overwritten in each iterative step.
If i use another loop inside the for loop the execution time increase drastically. Please help to least write the values of P into an array which i can later write into excel file(when i tried 'In an assignment A(I) = B, the number of elements in B and I must be the same' error appeared.)
please help
function FitSMC_BC
clc
% Parameters: P(1)=theta_S; P(2)=theta_r; P(3)=psib; P(4)=lamda;
smcdata=xlsread('asimdata');
nn=length(smcdata)-1;
for i=1:nn
psi=smcdata(:,1);
thetaObs=smcdata(:,i+1);
%Make an initial guess:
Pini=[0.5 0.1 -1 1.5];
P=fminsearch(#ObFun,Pini,[],psi,thetaObs);
disp(['result',num2str(i),': P=',num2str(P)]);
theta=Gettheta(P,psi);
end
function OF=ObFun(P,psi,thetaObs)
theta=Gettheta(P,psi);
OF=sqrt(mean((theta - thetaObs).^2));
function theta=Gettheta(P,psi)
SoilPars.theta_S=P(1);
SoilPars.theta_r=P(2);
SoilPars.psib=P(3);
SoilPars.lamda=P(4);
[theta]=thetaFun(psi,SoilPars);
function [theta]=thetaFun(psi,SoilPars)
theta_S=SoilPars.theta_S;
theta_r=SoilPars.theta_r;
psib=SoilPars.psib;
lamda=SoilPars.lamda;
theta=theta_r+((theta_S-theta_r)*((psib./psi).^lamda));
theta(psi>psib)=theta_S;
You can modify the P line with
P(i,:) = fminsearch(#ObFun,Pini,[],psi,thetaObs);
P will store each calculation (4 element vector) in a new line.
You may also initialise P before the for loop with P = nan(nn, 4);
Then write P in an Excel file using xlswrite.
I haven't studied your code in-depth, but as far as I can tell, you have two options:
Create a matrix P and use xlswrite on the entire matrix. This seems to me like the most reasonable approach.
Use xlswrite1 from the fileexchange in a loop. This will increase execution time a bit, but not nearly as much as using regular xlswrite as it is specially deigned to be used inside loops. The reason why it is so much faster is because it only opens and closes the Excel-file once, whereas the regular xlswrite opens and closes it every time you call the function.
You seem to know how to use indexing so I'm not sure why you're simply doing something like this:
P = zeros(size(smcdata,1),nn)
for i=1:nn
...
P(:,i) = fminsearch(#ObFun,Pini,[],psi,thetaObs);
disp(['result',num2str(i),': P=',num2str(P(:,i))]);
theta = Gettheta(P(:,i),psi); % Why is this here? Are you writing it to file too?
end
xlswrite('My_FileName.xls',P);
Or you could call xlswrite on each iteration of the loop (probably slower) and append the new data using something like this:
for i=1:nn
...
P = fminsearch(#ObFun,Pini,[],psi,thetaObs);
disp(['result',num2str(i),': P=',num2str(P)]);
theta = Gettheta(P,psi); % Why is this here? Are you writing it to file too?
xlswrite('My_FileName.xls',P,1,['A' int2str((i-1)*size(P,2)+1)]);
end
Of course your code isn't runnable so you'll have to debug any other little errors. Also, since smcdata seems to be a matrix rather than a vector, you should be careful using length with it. You probably should use size.

How to write a compound IF statement checking two sets of two values

I know the title is confusing, but I can't figure our how to word it properly. I'm trying to figure out how to properly format a compound conditional in an IF statement in Excel. It's for a school project that's due tomorrow.
I already have something like this
=if(AND(b152="oval.jpg",c152="q'")OR(AND(b152="triangle.jpg", c153="p'")), "Correct", "Incorrect")
In psuedocode I want it to run something like this:
if (b152=="oval.jpg" && c152=="q'") or (b152=="triangle.jpg", c153="p'"):
print("YES!")
else
print("False!")
I know I'm missing something here. My current excel code returns false even if the conditions are true. Thanks ahead of time!
OR is a function in Excel, like AND. Try something like this:
=if(OR(AND(b152 = "oval.jpg", c152 = "q'"), AND(b152 = "triangle.jpg", c153 = "p'")), "Correct", "Incorrect")

Resources