Cases won't execute in Switch statement...? - switch-statement

I'm trying to attempt something that seems super simple, but right now I want to throw my monitor outside out into the snow. I can't see why my switch statement is not executing when called.
Here it is:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) // Arithmetic Button Click
{
String^ riS = dc1->Text;
String^ ciS = dc2->Text;
colint = int::Parse(dc2->Text);
std::ostringstream ss;
String^ answer;
op1 = Double::Parse(foValue->Text);
op2 = Double::Parse(soValue->Text);
// Enter switch
switch(op_sym)
{
case '+':
sum = op1 + op2;
DGV->CurrentCell = DGV->Rows[RI]->Cells[CI];
ss << sum;
answer = Convert::ToString(answer);
MessageBox::Show(answer);
DGV->CurrentCell->Value = answer;
sumLabel->Text = "TEST";
break;
case '-':
sum = op1 - op2;
break;
case '*':
sum = op1 * op2;
break;
case '/':
if (op2 == 0)
{
MessageBox::Show("Sorry, you cannot divide by zero \n Please, reselect yoru second cell operand");
secondOpText->Text = "";
}
else
{
sum = op1/op2;
}
break;
default:
MessageBox::Show("I'm sorry. Please select one of these four arithmetic symbols from the drop down list: \n +, -, *, /");
break;
}
}
I'm getting the op_sym from right above:
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
Object^ selectedItem = comboBox1->SelectedItem;
String^ cb = selectedItem->ToString();
if( cb = "+")
{
op_sym = '+';
}
if(cb = "-")
op_sym = '-';
if(cb = "/")
op_sym = '/';
if(cb = "*")
op_sym = '*';
}
op_sym has already been declared as a char at the top. If someone would inform me of my most likely, beginner's mistake, I would be much appreciate. Thanks.
EDIT
...
case '+':
{
sum = op1 + op2;
DGV->CurrentCell = DGV->Rows[RI]->Cells[CI];
ss << sum;
answer = Convert::ToString(sum);
MessageBox::Show( answer);
DGV->CurrentCell->Value = answer;
sumLabel->Text = answer;
break;
}
...
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
Object^ selectedItem = comboBox1->SelectedItem;
String^ cb = selectedItem->ToString();
if( cb == "+")
{
op_sym = '+';
}
if(cb == "-")
op_sym = '-';
if(cb == "/")
op_sym = '/';
if(cb == "*")
op_sym = '*';
}

Notice in your second function (comparing the actual value of the op_sym):
if( cb = "+")
{
op_sym = '+';
}
if(cb = "-")
op_sym = '-';
if(cb = "/")
op_sym = '/';
if(cb = "*")
op_sym = '*';
You're performing assignments to cb and not actually comparisons. Try using the == operator for comparing two values:
if ( cb == "+" )
...
When you want to change the value of the op_sym, you use the assignment operator (=). When you want to compare values of String's, use the comparison operator (==).
Also - Check out the API for working with String is VC++: http://msdn.microsoft.com/en-us/library/ms177218.aspx

single = is assignment
== is equals
if( cb == "+")
{
op_sym = '+';
}
if(cb == "-")
op_sym = '-';
if(cb == "/")
op_sym = '/';
if(cb == "*")
op_sym = '*';

Related

How does the second argument use a variable when using bpf_probe_read_kernel_str() in ebpf?

`
size_t pos = 0;
const u32 MAX_BACKTRACE_DEPTH = 20;
for (u32 cnt = MAX_BACKTRACE_DEPTH; cnt != 0; --cnt) {
if (err || curr_dentry == NULL) {
break;
}
int name_len = BPF_CORE_READ(curr_dentry, d_name.len);
const u8 *name = BPF_CORE_READ(curr_dentry, d_name.name);
if (name_len <= 1) {
break;
}
if (name_len + pos > 512) {
break;
}
name_len = bpf_probe_read_kernel_str(filename + pos, name_len, name);
if (name_len <= 1) {
break;
}
pos += name_len;
filename[pos - 1] = '/';
struct dentry *temp_dentry = BPF_CORE_READ(curr_dentry, d_parent);
if (temp_dentry == curr_dentry || temp_dentry == NULL) {
break;
}
curr_dentry = temp_dentry;
}
`
ebpf validator prompts "R2 unbounded memory access, use 'var &= const' or 'if (var < const)'" when the second argument to function bpf_probe_read_kernel_str is a variable.
I tried to add const to name_len and I couldn't.
You need to have a strict bound on that name_len variable. It is currently only bounded by 512 - pos which is not a constant.

Dart - How to concatenate a String and my integer

How can I concatenate my String and the int in the lines:
print('Computer is moving to ' + (i + 1));
and print("Computer is moving to " + (i + 1));
I cant figure it out because the error keeps saying "The argument type 'int' can't be assigned to the parameter type 'String'
void getComputerMove() {
int move;
// First see if there's a move O can make to win
for (int i = 0; i < boardSize; i++) {
if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
String curr = _mBoard[i];
_mBoard[i] = computerPlayer;
if (checkWinner() == 3) {
print('Computer is moving to ' + (i + 1));
return;
} else
_mBoard[i] = curr;
}
}
// See if there's a move O can make to block X from winning
for (int i = 0; i < boardSize; i++) {
if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
String curr = _mBoard[i]; // Save the current number
_mBoard[i] = humanPlayer;
if (checkWinner() == 2) {
_mBoard[i] = computerPlayer;
print("Computer is moving to " + (i + 1));
return;
} else
_mBoard[i] = curr;
}
}
}
With string interpolation:
print("Computer is moving to ${i + 1}");
Or just call toString():
print("Computer is moving to " + (i + 1).toString());
You can simply use .toString which will convert your integer to String :
void main(){
String str1 = 'Welcome to Matrix number ';
int n = 24;
//concatenate str1 and n
String result = str1 + n.toString();
print(result);
}
And in your case it's gonna be like this :
print("Computer is moving to " + (i + 1).toString());

hungarian BBAN validation

can anybody tell me how to validate Hungarian BBAN account numbres ?
on the internet i have only found that it is 24 numbers length
And in format
bbbs sssk cccc cccc cccc cccx
b = National bank code
s = Branch code
c = Account number
x = National check digit
but how to calculate x = National check digit ?
I have tried to remove last char and modulo rest by 97 but it does not work
(the result is not 1 for valid account numbers)
thanks in advance for any help
I just finished Hungarian account validation function. This is the first version of this function but it's work well.
public string sprawdzWegierskitempAccountNumber(string _accountNumberToCheck, bool _iban) //if iban is true then function result will be always IBAN (even if _accountNumberToCheck will be BBAN)
{
string _accountNumberCorrected = _accountNumberToCheck.Replace(" ", "");
_accountNumberCorrected = _accountNumberCorrected.Replace("-", "");
_accountNumberCorrected = _accountNumberCorrected.Replace("//", "");
_accountNumberCorrected = _accountNumberCorrected.Replace("\", "");
string _accountNumberCorrectedFirst = _accountNumberCorrected;
if (_accountNumberCorrected.Length == 16 || _accountNumberCorrected.Length == 24 || _accountNumberCorrected.Length == 28)
{
if (_accountNumberCorrected.Length == 28) //IBAN Account number
{
_accountNumberCorrected = _accountNumberCorrected.Substring(4, _accountNumberCorrected.Length - 4); //we don't need first four digits (HUxx)
}
string digitToMultiply = "9731";
int checkResult = 0;
//checking first part of account number
for (int i = 0; i <= 6; i++)
{
checkResult = checkResult + int.Parse(_accountNumberCorrected.ToCharArray()[i].ToString()) * int.Parse(digitToMultiply.ToCharArray()[i % 4].ToString());
}
checkResult = checkResult % 10;
checkResult = 10 - checkResult;
if (checkResult == 10)
{
checkResult = 0;
}
if (checkResult.ToString() != _accountNumberCorrected.ToCharArray()[7].ToString())
{
throw new Exception("Wrong account number");
}
else
{
//if first part it's ok then checking second part of account number
checkResult = 0;
for (int i = 8; i <= _accountNumberCorrected.Length-2; i++)
{
checkResult = checkResult + int.Parse(_accountNumberCorrected.ToCharArray()[i].ToString()) * int.Parse(digitToMultiply.ToCharArray()[i % 4].ToString());
}
checkResult = checkResult % 10;
checkResult = 10 - checkResult;
if (checkResult == 10)
{
checkResult = 0;
}
if (checkResult.ToString() != _accountNumberCorrected.ToCharArray()[_accountNumberCorrected.Length-1].ToString())
{
throw new Exception("Wrong account number");
}
}
string tempAccountNumber = _accountNumberCorrected + "173000";
var db = 0; var iban = 0;
var maradek = 0;
string resz = "", ibanstr = "", result = "";
while (true)
{
if (db == 0)
{
resz = tempAccountNumber.Substring(0, 9);
tempAccountNumber = tempAccountNumber.Substring(9, (tempAccountNumber.Length - 9));
}
else
{
resz = maradek.ToString();
resz = resz + tempAccountNumber.Substring(0, (9 - db));
tempAccountNumber = tempAccountNumber.Substring((9 - db), (tempAccountNumber.Length - 9 + db));
}
maradek = int.Parse(resz) % 97;
if (maradek == 0)
db = 0;
else
if (maradek < 10)
db = 1;
else
db = 2;
if ((tempAccountNumber.Length + db) <= 9)
break;
}
if (maradek != 0)
{
resz = maradek.ToString();
resz = resz + tempAccountNumber;
}
else
resz = tempAccountNumber;
maradek = int.Parse(resz) % 97; ;
iban = 98 - maradek;
if (iban < 10)
ibanstr = "0" + iban.ToString();
else ibanstr = iban.ToString();
if (_accountNumberCorrected.Length == 16)
{
_accountNumberCorrected = _accountNumberCorrected + "00000000";
_accountNumberCorrectedFirst = _accountNumberCorrectedFirst + "00000000";
}
if (_iban)
{
result = "HU" + ibanstr + _accountNumberCorrected;
}
else
{
result = _accountNumberCorrectedFirst;
}
return result;
}
else
{
throw new Exception("Wrong length of account number");
}
}

sum and substract using char and string only

I have to make a code that sums and subtracts two or more numbers using the + and - chars
I managed to make the sum, but I have no idea on how to make it subtract.
Here is the code (I'm allowed to use the for and while loops only):
int CM = 0, CR = 0, A = 0, PS = 0, PR = 0, LC = 0, D;
char Q, Q1;
String f, S1;
f = caja1.getText();
LC = f.length();
for (int i = 0; i < LC; i++) {
Q = f.charAt(i);
if (Q == '+') {
CM = CM + 1;
} else if (Q == '-') {
CR = CR + 1;
}
}
while (CM > 0 || CM > 0) {
LC = f.length();
for (int i = 0; i < LC; i++) {
Q = f.charAt(i);
if (Q == '+') {
PS = i;
break;
} else {
if (Q == '-') {
PR = i;
break;
}
}
}
S1 = f.substring(0, PS);
D = Integer.parseInt(S1);
A = A + D;
f = f.substring(PS + 1);
CM = CM - 1;
}
D = Integer.parseInt(f);
A = A + D;
salida.setText("resultado" + " " + A + " " + CR + " " + PR + " " + PS);
The following program will solve your problem of performing addition and subtraction in a given equation in String
This sample program is given in java
public class StringAddSub {
public static void main(String[] args) {
//String equation = "100+500-20-80+600+100-50+50";
//String equation = "100";
//String equation = "500-900";
String equation = "800+400";
/** The number fetched from equation on iteration*/
String b = "";
/** The result */
String result = "";
/** Arithmetic operation to be performed */
String previousOperation = "+";
for (int i = 0; i < equation.length(); i++) {
if (equation.charAt(i) == '+') {
result = performOperation(result, b, previousOperation);
previousOperation = "+";
b = "";
} else if (equation.charAt(i) == '-') {
result = performOperation(result, b, previousOperation);
previousOperation = "-";
b = "";
} else {
b = b + equation.charAt(i);
}
}
result = performOperation(result, b, previousOperation);
System.out.println("Print Result : " + result);
}
public static String performOperation(String a, String b, String operation) {
int a1 = 0, b1 = 0, res = 0;
if (a != null && !a.equals("")) {
a1 = Integer.parseInt(a);
}
if (b != null && !b.equals("")) {
b1 = Integer.parseInt(b);
}
if (operation.equals("+")) {
res = a1 + b1;
}
if (operation.equals("-")) {
res = a1 - b1;
}
return String.valueOf(res);
}
}

how to extract Paragraph text color from ms word using apache poi

i am using apache POI , is it possible to read text background and foreground color from ms word paragraph
I got the solution
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
Range range = doc.getRange();
String[] paragraphs = we.getParagraphText();
for (int i = 0; i < paragraphs.length; i++) {
org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
System.out.println(pr.getEndOffset());
int j=0;
while (true) {
CharacterRun run = pr.getCharacterRun(j++);
System.out.println("-------------------------------");
System.out.println("Color---"+ run.getColor());
System.out.println("getFontName---"+ run.getFontName());
System.out.println("getFontSize---"+ run.getFontSize());
if( run.getEndOffset()==pr.getEndOffset()){
break;
}
}
}
I found it in :
CharacterRun run = para.getCharacterRun(i)
i should be integer and should be incremented so the code will be as follow :
int c=0;
while (true) {
CharacterRun run = para.getCharacterRun(c++);
int x = run.getPicOffset();
System.out.println("pic offset" + x);
if (run.getEndOffset() == para.getEndOffset()) {
break;
}
}
if (paragraph != null)
{
int numberOfRuns = paragraph.NumCharacterRuns;
for (int runIndex = 0; runIndex < numberOfRuns; runIndex++)
{
CharacterRun run = paragraph.GetCharacterRun(runIndex);
string color = getColor24(run.GetIco24());
}
}
GetColor24 Function to Convert Color in Hex Format for C#
public static String getColor24(int argbValue)
{
if (argbValue == -1)
return "";
int bgrValue = argbValue & 0x00FFFFFF;
int rgbValue = (bgrValue & 0x0000FF) << 16 | (bgrValue & 0x00FF00)
| (bgrValue & 0xFF0000) >> 16;
StringBuilder result = new StringBuilder("#");
String hex = rgbValue.ToString("X");
for (int i = hex.Length; i < 6; i++)
{
result.Append('0');
}
result.Append(hex);
return result.ToString();
}
if you are working on docx(OOXML), you may want to take a look on this:
import java.io.*
import org.apache.poi.xwpf.usermodel.XWPFDocument
fun test(){
try {
val file = File("file.docx")
val fis = FileInputStream(file.absolutePath)
val document = XWPFDocument(fis)
val paragraphs = document.paragraphs
for (para in paragraphs) {
println("-- ("+para.alignment+") " + para.text)
para.runs.forEach { it ->
println(
"text:" + it.text() + " "
+ "(color:" + it.color
+ ",fontFamily:" + it.fontFamily
+ ")"
)
}
}
fis.close()
} catch (e: Exception) {
e.printStackTrace()
}
}

Resources