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.
This conditional statement compares letters with numbers according to the keyboard of old button-phones.
In this statement, every if-else condition is the same. How can I shorten this piece of code?
//compares every name and pattern character
//every number represents some letters from latin alphabet
if(lowch >= 97 && lowch <= 99 && pattern[ptrch] == '2'){
ptrch++;
mir++;
}
else if(lowch >= 100 && lowch <= 102 && pattern[ptrch] == '3'){
ptrch++;
mir++;
}
else if(lowch >= 103 && lowch <= 105 && pattern[ptrch] == '4'){
ptrch++;
mir++;
}
else if(lowch >= 106 && lowch <= 108 && pattern[ptrch] == '5'){
ptrch++;
mir++;
}
else if(lowch >= 109 && lowch <= 111 && pattern[ptrch] == '6'){
ptrch++;
mir++;
}
else if(lowch >= 112 && lowch <= 115 && pattern[ptrch] == '7'){
ptrch++;
mir++;
}
else if(lowch >= 116 && lowch <= 118 && pattern[ptrch] == '8'){
ptrch++;
mir++;
}
else if(lowch >= 119 && lowch <= 122 && pattern[ptrch] == '9'){
ptrch++;
mir++;
}
else if(lowch == '+' && pattern[ptrch] == '0'){
ptrch++;
mir++;
}
else{
ptrch = 0;
mir = 0;
}
}
**ptrch- pattern char
*mir - matches in a row
Define a macro that encapsulates the comparisons, and then use a single condition with ||.
#define MATCH(start, end, ch) (lowch >= start && lowch <= end && pattern[ptrch] == ch)
if (MATCH('a', 'c', '2') ||
MATCH('d', 'f', '3') ||
MATCH('g', 'i', '4') ||
MATCH('j', 'l', '5') ||
MATCH('m', 'o', '6') ||
MATCH('p', 's', '7') ||
MATCH('t', 'v', '8') ||
MATCH('w', 'z', '9') ||
MATCH('+', '+', '0')) {
ptrch++;
mir++;
} else {
ptrch = 0;
mir = 0;
}
Another option would be to use an array of structures.
struct key {
char start,
char end,
char ch
} keys[] = {
{'a', 'c', '2'},
{'d', 'e', '2'},
...
{'+', '+', '0'}
};
bool found = false;
for (int i = 0; i < sizeof keys/sizeof keys[0]; i++) {
if (lowch >= keys[i].start && lowch <= keys[i].end && pattern[ptrch] == keys[i].ch) {
ptrch++;
mir++;
found = true;
break;
}
if (!found) {
ptrch = 0;
mir = 0;
}
A solution you might consider is to map the letters in an array.
// . . . . . . . . . . abcdefghijklmnopqrstuvwxyz
const char* numbers = "22233344455566677778889999";
char digit = (lowch >= 'a' && lowch <= 'z') ? numbers[lowch - 'a'] : 0;
if (digit == pattern[ptrch]) {
ptrch++;
mir++;
}
else if(lowch == '+' && pattern[ptrch] == '0') {
ptrch++;
mir++;
}
else {
ptrch = mir = 0;
}
Or (slightly overkill) you could build a full table to handle any character...
// Build the table once
char dialpad[1 << CHAR_BIT] = { 0 };
const unsigned char *map_from = "abcdefghijklmnopqrstuvwxyz+";
const char *map_to = "222333444555666777788899990";
for (int i = 0; map_from[i]; i++) dialpad[map_from[i]] = map_to[i];
// Later on...
if (dialpad[(unsigned char)lowch] == pattern[ptrch]) {
ptrch++;
mir++;
}
else {
ptrch = mir = 0;
}
All your characters (except the plus sign) are lower-case letters. In ASCII, these letters are in a contiguous range. You can therefore use an array to map lower-case letters to a number code:
// abcdefghijklmnopqrstuvwxyz
const char code[26] = "22233344455566677778889999";
if (lowch >= 'a' && lowch <= 'z' && code[lowch - 'a'] == pattern[ptrch]) {
ptrch++;
mir++;
} else if (lowch == '+' && pattern[ptrch] == '0') {
ptrch++;
mir++;
} else{
ptrch = 0;
mir = 0;
}
Before accessing the array, you must check whether lowch is a valid lower-case letter. The plus sign must be treated as special case here. (But you could make the array cover the whole 7-bit ASCII range, where the characters that don't have a umber code have a special value, perhaps '\0'.)
I am writing a code in C with two functions.
The first (WorkDay) takes a date and says if it is a working day (return 1) or not (return 0). I think the first function is OK, although it could be better, but it is working. It includes weekends and public holidays in my country.
The problem comes with the second function (CountWorkDays). It should take two dates and say if they are correct (return 1) and in that case also say how many working days is among them, including the entered dates (cnt=). If the dates aren't correct (first is bigger than second etc.), there is return 0. I've tried to make a helping function next_day(), but I am pretty sure it is wrong. Can you help me with the second function please?
I cannot use <time.h> and I put there some asserts, which I am testing the function with.
#include <stdio.h>
#include <math.h>
#include <assert.h>
int WorkDay ( int y, int m, int d )
{
int h, day;
h= (d + floor(((m+1)*26)/10) + y + floor(y/4) + 6*floor(y/100) + floor(y/400));
day=h%7;
if ((y%4!=0) && m==2 && d==29)
return 0;
else if (((y%4==0 || y%400==0) && (y%100!=0 || y%4000!=0)) && m==2 && d==29)
{
if (day==2 || day==3 || day==4 || day==1 || day==0)
return 1;
else
return 0;
}
else if ((d==1 && m==1) || (d==1 && m==5) || (d==8 && m==5) || (d==5 && m==7) || (d==6 && m==7) || (d==28 && m==9) || (d==28 && m==10) || (d==17 && m==11) || (d==24 && m==12) || (d==25 && m==12) || (d==26 && m==12) || day==0 || day==1 || (m==1 && (d>31 || d<1)) || (m==2 && (d>29 || d<1)) || (m==3 && (d>31 || d<1)) || (m==4 && (d>30 || d<1)) || (m==5 && (d>31 || d<1)) || (m==6 && (d>30 || d<1)) || (m==7 && (d>31 || d<1)) || (m==8 && (d>31 || d<1)) || (m==9 && (d>30 || d<1)) || (m==10 && (d>31 || d<1)) || (m==11 && (d>30 || d<1)) || (m==12 && (d>31 || d<1)) || y<2000 || m>12 || m<1)
return 0;
else if (day==2 || day==3 || day==4 || day==5 || day==6)
return 1;
else
return 0;
}
int next_day()
{
int y1, m1, d1;
static int days_in_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
unsigned short day_counter;
d1 += 1; day_counter++;
if (d1 > days_in_month[m1])
{
d1 = 1;
m1 += 1;
if (m1 > 12)
{
m1 = 1;
y1 += 1;
if (((y1%4==0 || y1%400==0) && (y1%100!=0 || y1%4000!=0)))
{
days_in_month[2] = 29;
}
else
{
days_in_month[2] = 28;
}
}
}
return 0;
}
int CountWorkDays ( int y1, int m1, int d1,
int y2, int m2, int d2,
int * cnt )
{
int i,x;
if ( (m1==1 && (d1>31 || d1<1)) || (m1==2 && (d1>29 || d1<1)) || (m1==3 && (d1>31 || d1<1)) || (m1==4 && (d1>30 || d1<1)) || (m1==5 && (d1>31 || d1<1)) || (m1==6 && (d1>30 || d1<1)) || (m1==7 && (d1>31 || d1<1)) || (m1==8 && (d1>31 || d1<1)) || (m1==9 && (d1>30 || d1<1)) || (m1==10 && (d1>31 || d1<1)) || (m1==11 && (d1>30 || d1<1)) || (m1==12 && (d1>31 || d1<1)) || y1<2000 || m1>12 || m1<1 ||(m2==1 && (d2>31 || d2<1)) || (m2==2 && (d2>29 || d2<1)) || (m2==3 && (d2>31 || d2<1)) || (m2==4 && (d2>30 || d2<1)) || (m2==5 && (d2>31 || d2<1)) || (m2==6 && (d2>30 || d2<1)) || (m2==7 && (d2>31 || d2<1)) || (m2==8 && (d2>31 || d2<1)) || (m2==9 && (d2>30 || d2<1)) || (m2==10 && (d2>31 || d2<1)) || (m2==11 && (d2>30 || d2<1)) || (m2==12 && (d2>31 || d2<1)) || y2<2000 || m2>12 || m2<1 || y2>y1 || (y1==y2 && m2>m1) || (y1==y2 && m1==m2 && d2>d1) )
return 0;
else
{
while (y1!=y2 && m1!=m2 && d1!=d2)
{
while (next_day())
{
if (WorkDay( y1, m1, d1 ) == 1)
i=0;
x=i++;
}
}
*cnt=x;
return 1;
}
}
int main ( int argc, char * argv [] )
{
int cnt;
assert ( WorkDay ( 2016, 11, 11 ) );
assert ( ! WorkDay ( 2016, 11, 12 ) );
assert ( CountWorkDays ( 2016, 11, 1,
2016, 11, 30, &cnt ) == 1
&& cnt == 21 );
assert ( CountWorkDays ( 2001, 1, 1,
2015, 2, 29, &cnt ) == 0 );
return 0;
}
The problem is in next_day() and CountWorkDays().
next_day() is using local variables d1, m1 and y1. It is calculating next day of some random day.
In CountWorkDays(), you are not incrementing d1, m1, y1.
Solution:
Modify next_day() to receive a day as input and return next day as output.
int next_day(int *d, int *m, int *y)
{
/* Use current day to start. */
int y1 = *y;
int m1 = *m;
int d1 = *d;
/* Your code to find next day. Remove day_counter here as it is unnecessary.*/
/* Return next day. */
*y = y1;
*m = m1;
*d = d1;
return 0;
}
In CountWorkDays() call next_day with d1, m1, y1 as below.
else
{
while (1)
{
if (y1!=y2 && m1!=m2 && d1!=d2) //Check if end day is reached.
{
if (WorkDay( y1, m1, d1 ) == 1)
x++; //At the beginning initialize x to 0.
}
else
{
break;
}
next_day(&d1, &m1, &y1); //Get the next day.
}
}
int CountWorkDays(int y1, int m1, int d1, int y2, int m2, int d2, int * cnt)
{
int i, x;
if( (m1 == 1 && (d1>31 || d1<1)) || (m1 == 2 && (d1>29 || d1<1)) || (m1 == 3 && (d1>31 || d1<1)) || (m1 == 4 && (d1>30 || d1<1)) || (m1 == 5 && (d1>31 || d1<1)) || (m1 == 6 && (d1>30 || d1<1)) || (m1 == 7 && (d1>31 || d1<1)) || (m1 == 8 && (d1>31 || d1<1)) || (m1 == 9 && (d1>30 || d1<1)) || (m1 == 10 && (d1>31 || d1<1)) || (m1 == 11 && (d1>30 || d1<1)) || (m1 == 12 && (d1>31 || d1<1)) || y1<2000 || m1>12 || m1<1 ||
(m2 == 1 && (d2>31 || d2<1)) || (m2 == 2 && (d2>29 || d2<1)) || (m2 == 3 && (d2>31 || d2<1)) || (m2 == 4 && (d2>30 || d2<1)) || (m2 == 5 && (d2>31 || d2<1)) || (m2 == 6 && (d2>30 || d2<1)) || (m2 == 7 && (d2>31 || d2<1)) || (m2 == 8 && (d2>31 || d2<1)) || (m2 == 9 && (d2>30 || d2<1)) || (m2 == 10 && (d2>31 || d2<1)) || (m2 == 11 && (d2>30 || d2<1)) || (m2 == 12 && (d2>31 || d2<1)) || y2<2000 || m2>12 || m2<1 ||
y2>y1 || (y1 == y2 && m2>m1) || (y1 == y2 && m1 == m2 && d2>d1))
...
x++;
Your logic is wrong and redundant. You need to check only once if day is less than 1. You are checking at least 20 times. Then you check it again another 20 times in another function. You repeat that for d2. This makes your code unreadable and prone to errors.
Also you are not initializing variables. The starting value of x is undefined. Put int x = 0; and i = 0;
Later you have:
i=0;
x=i++;
You could rewrite this as x = 1. But you probably mean to write x++
Here it seems you are checking for holidays:
else if((
d == 1 && m == 1) ||
(d == 1 && m == 5) ||
(d == 8 && m == 5) ||
(d == 5 && m == 7) ||
(d == 6 && m == 7) ||
(d == 28 && m == 9) ||
(d == 28 && m == 10) ||
(d == 17 && m == 11) ||
(d == 24 && m == 12) ||
(d == 25 && m == 12) ||
(d == 26 && m == 12) ||
This type of coding is not wrong, but it's not practical either. I would put the holidays in a structure:
struct holidays_t
{
int day, month;
};
struct holidays_t holidays[] = {
{ 1 , 1 },
{ 1 , 5 },
...
};
Then loop through holidays array to see if the dates match. If you have not learned structures yet, then put the days and months in two different arrays. Example:
int holiday_days[]={1,1,...};
int holiday_months[]={1,5,...};
Your formula for weekday is wrong. You need to fix that.
#include <stdio.h>
#include <math.h>
int is_leap_year(int y)
{
return (y % 4) == 0 && y % 100 != 0 || (y % 400 == 0 && y != 4000);
}
int get_days_in_month(int year, int month)
{
const int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if(month >= 1 && month <= 12)
{
int days = days_in_month[month - 1];
if(month == 2 && is_leap_year(year))
days++;
return days;
}
return 0;
}
int is_date_valid(int year, int month, int day)
{
if(get_days_in_month(year, month) == 0) return 0;
return day >= 1 && day <= get_days_in_month(year, month);
}
//this is wrong: ***************************************************
int get_weekday(int y, int m, int d)
{
int h = (d + floor(((m + 1) * 26) / 10) + y + floor(y / 4) + 6 * floor(y / 100) + floor(y / 400));
int weekday = h % 7;
return weekday;
}
struct holidays_t
{
int day, month;
};
int is_holiday(int m, int d)
{
const struct holidays_t holidays[] = {
{ 1 , 1 },
{ 1 , 5 },
{ 8 , 5 },
{ 5 , 7 },
{ 6 , 7 },
{ 28, 9 },
{ 28, 10 },
{ 17, 11 },
{ 24, 12 },
{ 25, 12 },
{ 26, 12 },
};
int count = sizeof(holidays) / sizeof(holidays[0]);
int i;
for(i = 0; i < count; i++)
if(holidays[i].month == m && holidays[i].day == d)
return 1;
return 0;
}
int CountWorkDays(int y1, int m1, int d1, int y2, int m2, int d2, int *ptr)
{
int count = 0;
*ptr = 0;
if(!is_date_valid(y1, m1, d1)) return 0;
if(!is_date_valid(y2, m2, d2)) return 0;
int y, m, d;
for(y = y1; y <= y2; y++)
{
int month_start = 1;
int month_end = 12;
if(y == y1) month_start = m1;
if(y == y2) month_end = m2;
for(m = month_start; m <= month_end; m++)
{
int day_start = 1;
int day_end = get_days_in_month(y, m);
if(y == y1 && m == m1) day_start = d1;
if(y == y2 && m == m2) day_end = d2;
for(d = day_start; d <= day_end; d++)
{
int test = 0;
if(is_holiday(m, d))
{
test = 1;
printf("holiday %d %d %d\n", y, m, d);
}
int weekday = get_weekday(y, m, d);
if(weekday == 0 || weekday == 6)
{
test = 1;
printf("weekend %d %d %d %s\n", y, m, d, (weekday == 0) ? "sunday" : "staurday");
}
if(!test)
{
count++;
}
}
}
}
*ptr = count;
//return success
return 1;
}
int main()
{
int count = 0;
CountWorkDays(2016, 1, 1, 2016, 12, 31, &count);
printf("count %d\n", count);
return 0;
}
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
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";
}