datagridview and NumericUpDown? - winforms

I have a NumericUpDown box and depending on its value, I want to insert the letter into a DataGridView. Here is my code, but it does not insert into the column I want.
if (MarkNumericUpDown.Value < 50)
{
//dataGridView1.Rows.Add("F");
}
else if (MarkNumericUpDown.Value > 50 && MarkNumericUpDown.Value <= 64)
{
//dataGridView1.Rows.Add("D");
}
else if (MarkNumericUpDown.Value > 64 && MarkNumericUpDown.Value <= 68)
{
//dataGridView1.Rows.Add("D+");
}
else if (MarkNumericUpDown.Value > 68 && MarkNumericUpDown.Value <= 72)
{
//dataGridView1.Rows.Add("C-");
}
else if (MarkNumericUpDown.Value > 72 && MarkNumericUpDown.Value <= 76)
{
//dataGridView1.Rows.Add("C");
}
else if (MarkNumericUpDown.Value > 76 && MarkNumericUpDown.Value <= 80)
{
//dataGridView1.Rows.Add("C+");
}
else if (MarkNumericUpDown.Value > 80 && MarkNumericUpDown.Value <= 84)
{
//dataGridView1.Rows.Add("B-");
}
else if (MarkNumericUpDown.Value > 88 && MarkNumericUpDown.Value <= 92)
{
//dataGridView1.Rows.Add("B");
}
else if (MarkNumericUpDown.Value > 92 && MarkNumericUpDown.Value <= 96)
{
//dataGridView1.Rows.Add("B+");
}
else if (MarkNumericUpDown.Value > 96 && MarkNumericUpDown.Value <= 100)
{
//dataGridView1.Rows.Add("A-");
}

I suspect you are more interested in the Cells values rather than the Rows values. Try something like this:
if (MarkNumericUpDown.Value < 50)
{
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[1].Value = "F";
}
else if (MarkNumericUpDown.Value > 50 && MarkNumericUpDown.Value <= 64)
{
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[2].Value = "D";
}
Update:
Judging by your picture, it looks like you are only concerned about EDITING a row and not ADDING a row. If this is the case, you need to keep track of which row you are concerned with and which column you are concerned with (please change the variable names to something that makes more sense for your application):
int indexOfRowICareAbout = 0;
int indexOfColumnIStoreLettersIn = 4; //Judging by your picture
if (MarkNumericUpDown.Value < 50)
{
dataGridView1.Rows[indexOfRowICareAbout].Cells[indexOfColumnIStoreLettersIn].Value = "F";
}
else if (MarkNumericUpDown.Value > 50 && MarkNumericUpDown.Value <= 64)
{
dataGridView1.Rows[indexOfRowICareAbout].Cells[indexOfColumnIStoreLettersIn].Value = "D";
}

Related

Function only returning return value it is not supposed to reach

I am working on a project for a programming class, where the goal is to verify a strength of a password based on different security levels. My problem is that with 2nd level, the:
Counter of numbers and special characters isn't working properly and isn't detecting numbers (it was working before I put it in its own function), but more importantly, the function only returns the value set in the very end. I have no idea what else to try.
int level2() {
while ((fgets(given_string, 100, stdin) != NULL)) {
for (int i = 0; given_string[i] != '\n'; i++) {
if (given_string[i] >= 'a' && given_string[i] <= 'z') {
lowerCaseLetterFound++;
} else if (given_string[i] >= 'A' && given_string[i] <= 'Z') {
upperCaseLetterFound++;
} else if (given_string[i] >= '0' && given_string[i] <= '9') {
numberFound++;
} else {
specialCharacterFound++;
}
}
}
printf("%d %d %d %d\n", lowerCaseLetterFound, upperCaseLetterFound, numberFound, specialCharacterFound);
if (param == 1 || param == 2) {
if (lowerCaseLetterFound >= 1 && upperCaseLetterFound >= 1) {
return 1;
} else {
return 0;
}
} else if (param == 3) {
if (((lowerCaseLetterFound >= 1 && upperCaseLetterFound >= 1) && numberFound >= 1) ||
((lowerCaseLetterFound >= 1 && upperCaseLetterFound >= 1) && specialCharacterFound >= 1)) {
return 1;
} else {
return 0;
}
} else if (param >= 4) {
if (lowerCaseLetterFound >= 1 && upperCaseLetterFound >= 1 && numberFound >= 1 &&
specialCharacterFound >= 1) {
return 1;
} else {
return 0;
}
}
return 0;
}
PS: This is my first time asking question here, and I am a programming newbie. Thanks for your help.
Update: Adding the whole code as I have it RN.
char given_string[100];
int lowerCaseLetterFound = 0;
int upperCaseLetterFound = 0;
int numberFound = 0;
int specialCharacterFound = 0;
int repeatedCharacter = 0;
int param;
int level;
int level1 () {
while ((fgets(given_string, 100, stdin) != NULL)) {
for (int i = 0; given_string[i] != '\n'; i++) {
if (given_string[i] >= 'a' && given_string[i] <= 'z') {
lowerCaseLetterFound++;
} else if (given_string[i] >= 'A' && given_string[i] <= 'Z') {
upperCaseLetterFound++;
}
}
}
if (lowerCaseLetterFound >= 1 && upperCaseLetterFound >= 1) {
return 1;
} else {
return 0;
}
}
int level2() {
while ((fgets(given_string, 100, stdin) != NULL)) {
for (int i = 0; given_string[i] != '\n'; i++) {
if (given_string[i] >= 'a' && given_string[i] <= 'z') {
lowerCaseLetterFound++;
} else if (given_string[i] >= 'A' && given_string[i] <= 'Z') {
upperCaseLetterFound++;
} else if (given_string[i] >= '0' && given_string[i] <= '9') {
numberFound++;
} else {
specialCharacterFound++;
}
}
}
printf("%d %d %d %d\n", lowerCaseLetterFound, upperCaseLetterFound, numberFound, specialCharacterFound);
if (param == 1 || param == 2) {
if (lowerCaseLetterFound >= 1 && upperCaseLetterFound >= 1) {
return 1;
} else {
return 0;
}
} else if (param == 3) {
if (((lowerCaseLetterFound >= 1 && upperCaseLetterFound >= 1) && numberFound >= 1) ||
((lowerCaseLetterFound >= 1 && upperCaseLetterFound >= 1) && specialCharacterFound >= 1)) {
return 1;
} else {
return 0;
}
} else if (param >= 4) {
if (lowerCaseLetterFound >= 1 && upperCaseLetterFound >= 1 && numberFound >= 1 &&
specialCharacterFound >= 1) {
return 1;
} else {
return 0;
}
}
return 0;
}
/*
int level3() {
while ((fgets(given_string, 100, stdin) != NULL)) {
for (int i = 0; given_string[i] != '\n'; i++) {
if (given_string[i] >= 'a' && given_string[i] <= 'z') {
lowerCaseLetterFound++;
} else if (given_string[i] >= 'A' && given_string[i] <= 'Z') {
upperCaseLetterFound++;
} else if (given_string[i] >= '0' && given_string[i] <= '9') {
numberFound++;
} else {
specialCharacterFound++;
}
}
}
}
*/
int main(int argc, const char *argv[]) {
//ukládám argumenty, prozatím pouze 2 ze 3
if (argc <= 1) {
printf("Not enough arguments provided. Please, provide LEVEL and PARAM arugments.\n");
} else if (argc >= 5) {
printf("Too many arguments provided.\n");
}
int level = atoi(argv[1]);
int param = atoi(argv[2]);
if (level <= 0 || level >= 5) {
printf("Level must be 1 through 4.\n");
}
if (param <= 0) {
printf("Parameter is not a full positive number.\n");
}
//vyhodnocování na základě zadaných parametrů - kontrola
//LEVEL = 1
if (level == 1) {
level1();
if (level1() == 1) {
printf("Password passed check 1.\n");
} else {
printf("Password did not pass check 1.\n");
}
}
if (level == 2) {
if (level1() == 1) {
if (level2() == 1) {
printf("Password did pass the check.\n");
}
} else {
printf("Password did not pass the check.\n");
}
if (level1() == 0) {
printf("Password did not pass the check.\n");
}
}
if (level == 3) {
}
}
Try to put your code in more than one function.
I think you can't print sth in your function. You should return just one thing you want to get from your function and then in another function, get input the first function's return.
It looks like the problem is that you’re re-defining your global variables in your main function. For example, when you do int param = atoi(argv[2]); in your main, param is scoped to your main. As soon as you leave your main function, you will be accessing the global param which has never been set.
You can either remove the variable type in your main so you are setting the global variables:
param = atoi(argv[2]);
Or get rid of the global variables and pass the ones created in main to any function that needs them.

Why do I keep getting the same output in this problem?

C
What is that that I'm doing wrong?I'm trying to print for the different months with dates their corresponding seasons But I keep getting the first (printf) for all of them which is Winter.
dates for each season are:
Spring: March 20 - June 20
Summer: June 21 - September 21
Autumn: September 22 - December 20
Winter: December 21 - March 19
#include <stdio.h>
#include <string.h>
int main(void) {
char inputMonth[50];
int inputDay;
scanf("%s", inputMonth);
scanf("%d", & inputDay);
/* Type your code here. */
//winter
if (strcmp(inputMonth, "December") && (21 <= inputDay && inputDay <= 30)) {
printf("Winter\n");
} else if (strcmp(inputMonth, "January") && (1 <= inputDay && inputDay <= 31)) {
printf("Winter\n");
} else if (strcmp(inputMonth, "February") && (1 <= inputDay && inputDay <= 29)) {
printf("Winter\n");
} else if (strcmp(inputMonth, "March") && (1 <= inputDay && inputDay <= 19)) {
printf("Winter\n");
}
//spring
else if (strcmp(inputMonth, "March") && (20 <= inputDay && inputDay <= 31)) {
printf("Spring\n");
} else if (strcmp(inputMonth, "April") && (1 <= inputDay && inputDay <= 31)) {
printf("Spring\n");
} else if (strcmp(inputMonth, "May") && (1 <= inputDay && inputDay <= 30)) {
printf("Spring\n");
} else if (strcmp(inputMonth, "June") && (1 <= inputDay && inputDay <= 20)) {
printf("Spring\n");
}
//summer
else if (strcmp(inputMonth, "June") && (21 <= inputDay && inputDay <= 31)) {
printf("Summer\n");
} else if (strcmp(inputMonth, "July") && (1 <= inputDay && inputDay <= 31)) {
printf("Summer\n");
} else if (strcmp(inputMonth, "August") && (1 <= inputDay && inputDay <= 31)) {
printf("%sSummer\n");
} else if (strcmp(inputMonth, "September") && (1 <= inputDay && inputDay <= 21)) {
printf("Summer\n");
}
//autumn
else if (strcmp(inputMonth, "September") && (22 <= inputDay && inputDay <= 30)) {
printf("Autumn\n");
} else if (strcmp(inputMonth, "October") && (1 <= inputDay && inputDay <= 31)) {
printf("Autumn\n");
} else if (strcmp(inputMonth, "November") && (1 <= inputDay && inputDay <= 30)) {
printf("Autumn\n");
} else if (strcmp(inputMonth, "December") && (1 <= inputDay && inputDay <= 20)) {
printf("Autumn\n");
} else {
printf("Invalid\n");
}
return 0;
}
strcmp(inputMonth, "December")
This returns true if inputMonth is different from December.
strcmp just returns a negative value,0 or a positive value if the first is less than/equal to/greater than the second argument. So, if both strings are the same, strcmp() returns 0.
If you wish to check for "December" then you need !strcmp()
Thus,
if (!strcmp(inputMonth, "December"))

ng-pattern for mobile number validation

I am trying to implement mobile number validation on a Visualforce page using Angular JS, but am having problems getting my head around how to write the regex expression.
The requirements, as given to me, are fairly simple:
The number should be 10 digits long (I've already set the maxlength attribute on the field so this one is kind of taken care of already)
It should start with 04 (as it is an Australian mobile)
Only numbers should be allowed.
The regex expression I am using (in the ng-pattern attribute for my phone number input field) is:
^/(04)[0-9]{10}/$
This works up to a point - it does not allow anything other than numbers and does let me enter a 10 digit number starting with 04. However it also lets me enter numbers starting with, for example, 02, 03 etc....
Probably quite a simple thing I'm missing but I've had a look at quite a few sites, including this one, and can't find the answer.
Any help would be hugely appreciated as this one has caused me a few grey hairs already.
Try using this pattern
Use this in ur HTML file:
<input type="text" (keypress)="keyPress($event)" minlength=10 maxlength=10>
Use this in ur JS file:
keyPress(event: any) {
const pattern = /[0-9\+\-\ ]/;
let inputChar = String.fromCharCode(event.charCode);
if (event.keyCode != 8 && !pattern.test(inputChar)) {
event.preventDefault();
}
}
Try this one:
Mobile Number :
//inside view:
<input type="text" class="form-control" ng-model="mobileNo" name="mobileNo" ng-pattern="regEx" />
//inside controller:
$scope.regEx="/^[0-9]{10,10}$/";
For starters I had to create a whole js function for it... and it does validates as you type. here is my full code I hope this can help you get where you need.
This function gets the string every time a key is beign pressed and it allows the carrete to move front, back, delete and backspace. check it out and let me know if it helps you. you can run it on any situation and this is how I would add the "04" validation
//phone validation 10 digits and parenthesis (344)567-0011
function validatePhone(inputId) {
let validKey = false;
const input = document.getElementById(inputId);
let enteredDigits = input.value;
//switch to remove the country code added by default on autoComplete forms.
if (enteredDigits.length > 10 && enteredDigits[0] == '+') {
switch (enteredDigits.length) {
case 12:
enteredDigits = enteredDigits.slice(2);
break;
case 13:
enteredDigits = enteredDigits.slice(3);
break;
case 14:
enteredDigits = enteredDigits.slice(4);
break;
default:
enteredDigits = enteredDigits.replace(/\D+/g, '');
}
}
let newPhone = enteredDigits.replace(/\D+/g, ''); // This replace any character that is not a number.
const key = event.keyCode || event.charCode; // Get the pressed key.
let caretPosition = input.selectionStart; // get the carret position.
// splits, removes the "-" and converts from array to string and gives the needed digits.
const areaCode = newPhone.split('').splice(0, 3).toString().replace(/,/g, '');
const threeDigit = newPhone.split('').splice(3, 3).toString().replace(/,/g, '');
const fourDigit = newPhone.split('').splice(6, 8).toString().replace(/,/g, '');
// Moving carret on different positions. when the numeric keys are being pressed.
// Key >= 48 && key <= 58 number keys.
// Key >= 96 && key <= 105 numeric path number keys.
if ((key >= 48 && key <= 58) || (key >= 96 && key <= 105)) {
validKey = true;
if (threeDigit.length > 0) {
if (caretPosition == 1) {
caretPosition = caretPosition + 1;
} else if (caretPosition == 4 && newPhone.length == 4) {
caretPosition = caretPosition + 2;
} else if (caretPosition == 4 && newPhone.length >= 5) {
caretPosition = caretPosition + 1;
} else if (caretPosition == 5) {
caretPosition = caretPosition + 1;
} else if (caretPosition >= 2 && caretPosition <= 3 && newPhone.length <= 4) {
caretPosition = caretPosition + 1;
}
}
if (fourDigit.length > 0 && caretPosition == 9) {
caretPosition = caretPosition + 1;
}
}
// Key = 8 = Backspace.
else if (key == 8) {
validKey = true;
if (caretPosition == 3 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if (caretPosition == 2 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if (caretPosition == 1 && newPhone.length == 3 && threeDigit.length == 0) {
caretPosition = caretPosition - 1;
}
}
// Key = 46 = Delete. Key =37 = ArrowLeft. Key = 39 = ArrowRight.
else if (key == 46 || key == 39 || key == 37) {
validKey = true;
// Delete
if (key == 46) {
if (caretPosition == 0 && newPhone.length > 3) {
caretPosition = caretPosition + 1;
} else if (caretPosition == 1 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if (caretPosition == 2 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if (caretPosition == 3 && newPhone.length == 3) {
caretPosition = caretPosition - 1;
} else if ((newPhone.length >= 4 && caretPosition == 4) || (newPhone.length >= 4 && caretPosition == 8)) {
caretPosition = caretPosition + 1;
}
}
}
//here is the validation for the country that you need.
if ((newPhone.length == 1 && newPhone[0] != '0') || (newPhone.length >= 2 && newPhone[1] != '4')) {
alert('Must start with 04');
newPhone = '';
}
// Adding the special character for formatting.
if (threeDigit.length > 0 && fourDigit.length == 0) {
newPhone = '(' + areaCode + ')' + threeDigit;
} else if (fourDigit.length > 0 && threeDigit.length == 3) {
newPhone = '(' + areaCode + ')' + threeDigit + '-' + fourDigit;
}
if (!validKey) {
caretPosition = caretPosition - 1;
}
// Set new values.
newPhone = newPhone.substring(0, 13);
input.value = newPhone;
input.focus();
input.setSelectionRange(caretPosition, caretPosition);
}
<form name="myForm"
onsubmit="return validateForm()"
method="post">
Phone number: <input type="text"
id="phoneNumber"
name="fPhone"
onkeyup="validatePhone('phoneNumber')">
<input type="submit"
value="Submit">
</form>

Multiple value array

I am new to jScript and have written this code [which works perfectly]. Its purpose is to test that the term for the amount of loan is not exceeded. Can the process be consolidated into one array where you pass the loan amount which returns the term based on the range i.e. 6000 to 7000 = 96
function TestMaxTerm()
{
var LnAmt = 14000 //Testing Purposes
var Term = 0 //Testing Purposes
if (LnAmt > 0 && LnAmt <= 1000){Term = 0;}
if (LnAmt > 1000 && LnAmt <= 2000){Term = 1;}
if (LnAmt > 2000 && LnAmt <= 3000){Term = 2;}
if (LnAmt > 3000 && LnAmt <= 4000){Term = 3;}
if (LnAmt > 4000 && LnAmt <= 5000){Term = 4;}
if (LnAmt > 5000 && LnAmt <= 6000){Term = 5;}
if (LnAmt > 6000 && LnAmt <= 7000){Term = 6;}
if (LnAmt > 7000 && LnAmt <= 8000){Term = 7;}
if (LnAmt > 8000 && LnAmt <= 9000){Term = 8;}
if (LnAmt > 9000 && LnAmt <= 10000){Term = 9;}
if (LnAmt > 10000 && LnAmt <= 11000){Term = 10;}
if (LnAmt > 11000 && LnAmt <= 12000){Term = 11;}
if (LnAmt > 11000){Term = 12;}
//Obtain Maximum Term for Loan Amount
var MaxTerm = new Array();
MaxTerm[0] = 24; MaxTerm[1]=36; MaxTerm[2] = 48; MaxTerm[3] = 60;
MaxTerm[5] = 72; MaxTerm[5]=84; MaxTerm[6] = 96; MaxTerm[7] = 108;
MaxTerm[8] = 120; MaxTerm[9]=132; MaxTerm[10] = 164; MaxTerm[11] = 176;
MaxTerm[12] = 420;
var text = MaxTerm[Term];
alert(text);
}
You could put all of the data into a 2D array and loop through the array to find your answer.
Here's some sample code which you can tweak for your JScript implementation. Your last entry could go from 12000 to your max loan amount or Number.MAX_VALUE
var loanAmt = 2200;
var answer = -1;
var myData =
[
[0, 1000, 24],
[1000, 2000, 36],
[2000, 3000, 48],
[3000, 4000, 60]
];
for (var x=0; x < myData.length; x++) {
if ( (loanAmt > myData[x][0]) && (loanAmt <= myData[x][1]) ) {
answer = myData[x][2];
break;
}
}
alert(answer);
As the terms are multiples of 12 and the grouping is based on 1000, the term can be computed:
% function f(amt) {return 12 * (2 + Math.floor(amt / 1000));}
undefined
% f(6500)
96
% f(3999)
60
% f(4000)
72
%
On second thought:
% function g(amt) {return 24 + 12 * Math.floor((amt - 1) / 1000);}
undefined
% g(1)
24
% g(999)
24
% g(1000)
24
% g(1001)
36
% g(6000)
84
% g(6001)
96
% g(6999)
96
% g(7000)
96
% g(7001)
108
%

Is my code correctly portraying the diagram?

What my function does, is it takes the values of the profit (function declared as fieldProfit) and the field score (function declared as fieldScore); and if both are above 10, then you earn a badge, hence, innerbadge = 1. BUT, there's also another condition that must be met, the field or (x, y) coordinates, have to fall in the area depicted as the shaded in box that has a hole in the middle. I've written the code for it, and I just wanted to make sure that my logic/syntax is correct! Any help is appreciated!
Here's my code:
int badgeInnerCircle(int x, int y) {
double fprofit, fscore;
int innerbadge;
if ((x >= 1 && x <= 20) && (y >= 1 && y <= 20)) {
if (((x == 7 || x == 8) && (y >= 7 && y <= 14)) || ((x == 13 || x == 14)
&& (y >= 7 && y <= 14)) || ((x >= 7 && x <= 14) && (y == 7 || y == 8))
|| ((x >= 7 && x <= 14) && (y == 13 || y == 14))) {
fprofit = fieldProfit(x, y);
fscore = fieldScore(x, y);
if (fprofit >= 10 && fscore >= 10) {
innerbadge = 1;
}
else {
innerbadge = 0;
}
}
}
else {
innerbadge = -1;
}
return innerbadge;
}
no, your code is not correct.
int innerbadge;
if (condition) {
if (condition) {
if (condition) {
innerbadge = 1;
}
else {
innerbadge = 0;
}
}
//else unidentified!
}
else {
innerbadge = -1;
}
you should change the initialisazion to "int innerbadge = 0;" or something approriate

Resources