Two If statements execute at once, instead of one - c

I've got a program that takes input from a char Array, using the strtok function to check if the input contains the words "up" or "down". If it contains the word "up", my b value is set false (i.e b = 0) and my c value is set to false as well. If the char array contains the words "down", b is set to false, however my c value is set to true (i.e c= 1).
My problem occurs when the word "up" are contained in the string, as the first if statement executes, and prints the resultant characters on the screen, but somehow the second if also executes printing those characters as well. Any input on this matter would be much appreciated
EDIT: The same problem occurs if I use the word "down", both if statements are executed.
int moveC(int y, int x, int b, int i, int c) {
// int c is a static variable(static int c = FALSE;) defined in the previous function
int j;
int k;
switch (b) //assume b is always false (which it is)
{
case FALSE:
if (c == 0) {
mvprintw(y, x, "^");
refresh();
for (j = 1; j <= i; j++) {
mvprintw(y + j, x, ".");
refresh();
}
break;
}
if (c == 1) //tried using else if, same result
{
mvprintw(y, x, "^");
refresh();
for (j = 1; j <= i; j++) {
mvprintw(y - j, x, ".");
refresh();
}
break;
}
}
return 0;
}

Your code is most likely executing twice. The giveaway is that you tried an if/else. Only one block will ever be executed in one execution of an if statement.

Related

Substitute characters in a string with their values?

I have a string given (a+b)&(a+c) and I have created a truth table with values of a,b, and c. Now the problem is to evaluate the logic expression by substituting a,b, and c with corresponding values from the truth table. How it can be done in C?
Ex: a=0 b=0 c=0 r=(0+0)&(0+)=0
a=0 b=0 c=1 r=(0+0)&(0+1)=0
and so on
The code itself looks like this
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c,* str, *vars, **result;
int i=0,count=0,j=0;
unsigned long long rows;
str = (char*) malloc(1*sizeof(char));
vars=(char*) malloc(1*sizeof(char));
result=(char**)malloc(1*sizeof(char));
char values[] = {'F', 'T'};
while ((c = getchar()) != EOF)
{
str[i++] = c;
str = (char*) realloc(str, (i+1) * sizeof(char));
if (c >= 'a' && c <= 'z')
{
vars[j++]=c;
vars=(char*) realloc(vars,(j+1)*sizeof(char));
count++;
}
}
rows=1ULL<<(count);
result=(char**)realloc(result,(rows+2)*sizeof(char));
for (i = 0; i < rows+1; i++)
{
result[i]=(char*)malloc(sizeof(char)*(count+1));
for (j = 0; j < count; j++)
{
if(i==0)
result[i][j]=vars[j];
else
result[i][j]=values[(i >> j) & 1];
}
}
result[0][count]='R';
for(i=0;i<rows+1;i++)
{
for(j=0;j<count+1;j++)
{
//do something
}
}
Now the problem is to evaluate the logic expression by substituting a,b, and c with corresponding values from the truth table.
Aside from the issues mentioned in the question's comments, substituting alone won't do the job to evaluate the logic expression. The following function for example substitutes the values while evaluating the expression. (You didn't specify the general syntax of your expressions, so I chose to support combinations of the used operators and lower case variables.)
#include <ctype.h>
#include <string.h>
int indx(char *s, char c) { return strchr(s, c)-s; }
char *gstr, *gvars, *vals; // expression string, variables, value combination
char eval()
{ // evaluate expression "gstr"
char or = 0; // neutral element of +
do
{
char and = 1; // neutral element of &
do
{
char c = *gstr++; // get next token
if (islower(c))
and &= indx("FT", vals[indx(gvars, c)]);
else
if (c == '(')
{ // evaluate subexpression
and &= eval();
c = *gstr++; // get next token
if (c != ')')
printf("error at '%c': expected ')'\n", c), exit(1);
}
else
printf("error at '%c'\n", c), exit(1);
} while (*gstr == '&' && ++gstr);
or |= and;
} while (*gstr == '+' && ++gstr);
return or;
}
It can be called from your main (inserted in your code, hence the inconsistent spacing)
result[0][count]='R';
gvars = vars; // make variable names globally accessible
for (i = 1; i <= rows; ++i)
{
gstr = str, vals = result[i], // globally accessible
result[i][count] = values[eval()];
while (isspace(*gstr)) ++gstr;
if (*gstr)
printf("error at '%c': expected end of input\n", *gstr), exit(1);
}
for(i=0;i<rows+1;i++)
{
for(j=0;j<count+1;j++)
{
putchar(result[i][j]);
}
putchar('\n');
}
(Don't forget to put str[i] = '\0'; after your getchar loop to make a null-terminated string.) Note that due to the given for loop counting, the order of the truth table entries is somewhat unusual in that the row with all variables F comes last.

Why I am getting a space character in my program in the place of third last character?

Why I am getting a space character in my program in the place of third last character?
Even if I change the string str variable I get the same result.
#include <stdio.h>
#include <string.h>
void parser(char array[])
{
int a, b;
for (int i = 0; i < strlen(array); i++) {
if (array[i] == '>') {
a = i;
break;
}
}
for (int j = a; j < strlen(array); j++) {
if (array[j] == '<') {
b = j;
}
}
for (int p = 0, q = a + 1; p < b - a - 1, q < b; p++, q++) {
array[p] = array[q];
array[b - a] = '\0';
printf("%c", array[p]);
}
}
int main()
{
char str[] = "<h1>hello there i am programmer.</h1>";
parser(str);
return 0;
}
There are many things that could be written better in the code but they do not affect the result.
The line that produces the unexpected outcome is:
array[b-a]='\0';
When this for loop starts...
for(int p=0,q=a+1;p<b-a-1,q<b;p++,q++){
array[p]=array[q];
array[b-a]='\0';
printf("%c",array[p]);
}
... the values of a and b are 3 and 32.
The statement array[b-a]='\0'; puts the NUL terminator character at position 29 in array.
The loop starts with p=0, q=4 (a+1) and repeats until p reaches 28 and q reaches 31 (q<b)*.
When p is 25, q is 29 and array[29] has been repeatedly set to '\0' on the previous iterations, therefore '\0' is copied at position 25 and printed on screen.
You should set the NUL terminator only once, after the loop. And the right position for it is b-a-1, not b-a; you expressed this correctly in the for initialization (p=0) and exit condition (p<b-a-1).
All in all, the code around the last for loop should be like this:
for(int p=0, q=a+1;q<b;p++,q++){
array[p]=array[q];
printf("%c",array[p]);
}
array[b-a-1]='\0';
*The condition p<b-a-1 is ignore because of the comma character. You probably want & between the conditions but they are equivalent, one of them is enough.

While loops and arrays causing very odd behaviour...maybe a memory mixup

I'm tired of this tom-foolery occurring during runtime , although I'm sure we all are, when our programs screw up at runtime in the most obscure ways.
Getting to the point, the entire source code is a bit large to place here, but still <200 lines, so that's here . Use it if running the program, since the code I will post below is just functions, where I think the error lies.
Context : This is a sort of shift cipher with 8 different shifts taken using an 8 digit pin.
The issue is strange. Basically, the encrypt() function works correctly always -I've matched it by doing the algorithm for myself on paper ; for example, ABC is correctly encoded to 3c 45 46 -6f when the Pin is 12345678.
The strange issues are with the decrypt() function.
When the program is run for the first time, trying to run decrypt() on a valid ciphertext-pin pair always returns nothing except a /n (newline) . When tried with a different valid pin-ciphertext pair, after a successful run of encrypt() is done first, the decrypt() function just returns either the same message which was just encrypted or some other random output from the previously encoded message.
Without further ado, the legendarily screwed up decrypt function which I have rebuilt thrice now -
void decrypt()
{
printf("\n");
int *digits = pin(); int d[8];
getchar();
for (int i=0;i<8;i++)
d[i] = *(digits + i); //puts each digit in a local array.
printf("\nEnter encoded message -\n\n");
getchar();
int j; char ch, msg[3002];
for(int i=0; i < 3000;i++)
{
scanf("%x",&j);
if(j==-111){
msg[i] = '\0'; //terminates string with \0
break;
}
else{
if(ctln(i)==1)
ch = j - d[2];
else if(fib(i)==1)
ch = j + d[4];
else if(luc(i)==1)
ch = j - d[0];
else if(pent(i)==1)
ch = j + d[6];
else if(hex(i)==1)
ch = j - d[3];
else if(prm(i)==1)
ch = j + d[7];
else {
if(i%2 == 0)
ch = j - d[1];
else
ch = j + d[5];
msg[i] = ch;
}
}
}
printf("\nDecrypted message -\n\n");
puts(msg);
}
For context, as well as finding the culprits here, do make sure to read the full code here , with the pin() returning a pointer to a static int array holding all 8 digits , as well as the ctln() , fib(), luc(), pent(), hex(), prm() [ which check if position value i of char in message is a part of Catalan, Fibonacci , Lucas, Pentagon, Hexagon, Prime number series. More here.
Edit 1
I have already tried keeping different variable names, and some other things I can't fully recall. Also, because it is very relevant, below is the pin() function:
int *pin()
{
int num,q=0; static int pins[8];
printf("Enter 8-digit PIN : ");
scanf("%d", &num);
for(register int i = 10000000 ; i >= 1 ; i = (i/10)) // i is position of digit.
{
int d = ((num - (num % i)) / i); // d stores 'digit' ( divides quotient of (num % i) by i)
pins[q] = d; q++;
num = (num - ( d * i ));
}
return pins ; // pointer to static array storing digits of PIN
}
Edit 2
I had wrongly assigned pins[6] rather than pins[8] in the original code, I have corrected it but am still facing the same errors.
Edit 3
After correcting the mistake pointed out by MikeCAT, it now ignores the first character when deciphering.
Edit 4
The getchar() before scanf() was to blame, removing it fixes the last issue too. Thanks #MikeCAT !
In your decrypt() function, msg[i] = ch; is executed only if none of the functions ctln, fib, luc, pent, hex, prm returned 1.
Therefore, uninitialized value of non-static local variable msg, which is indeterminate, may be used for printing and undefined behavior may be invoked.
The part
msg[i] = ch;
}
should be
}
msg[i] = ch;
as it is done in encrypt() function.

Variable returned by Reference from a function gives weird values

I've been working on a hangman game lately, and this function supposedly checks for a letter that the user inputs in an array (Containing a pre-defined word such as "BUILDING") and adds on a Counter (Count) if the letter exists or decreases the lives (Which start at 5 -defined in the main function-) if it doesn't exist.
Now the Count variable works fine but the Lives variable keeps decreasing anyways, even if the letter exists and it doesn't just decrease by 1 but by bigger amounts resulting in rather big negative numbers.
Here's the code, thanks in advance:
void Checkf(char X,int r,int Length,char *Hidden, int *Lives,int *Count)
{
int i;
for (i=0;i<Length;i++)
{
if (X==Words[r][i] && Hidden[i]=='*')
{
Hidden[i] = X;
*Count = *Count + 1;
}
else if (X!=Words[r][i] && Hidden[i]=='*')
*Lives = *Lives - 1;
}
}
That behavior occurs because you (optionally) decrease the value of Lives on each iteration of the loop.
You can add a variable that indicates whether the letter was found or not, and then decrease the value of Lives after the loop ends, like this:
void Checkf(char X,int r,int Length,char *Hidden, int *Lives,int *Count)
{
int i;
unsigned char found = 0;
for (i=0;i<Length;i++)
{
if (X==Words[r][i] && Hidden[i]=='*')
{
Hidden[i] = X;
*Count = *Count + 1;
found = 1;
}
}
if (!found)
{
*Lives -= 1;
}
}

What does a return statement within a loop do?

In the code below, str_replace_all replaces all occurrences of oldc with newc. str_replace_first is only supposed to replace the first occurrence of oldc with newc.
So str_replace_all loops through and it replaces all occurrences of oldc with newc, easy enough to understand. In the second function str_replace_first the code is identical, except for return 1 after finding and replacing the char. I don't exactly understand what return 1 does in this case. From what I am understanding it "breaks" the loop? I was hoping somebody could give me an explanation on how it replaces only the first occurrence.
size_t str_replace_all(char s[], int oldc, int newc)
{
size_t i;
size_t count = 0;
for (i=0; s[i]!='\0'; i++)
{
if (s[i] == (char)oldc)
{
s[i] = (char)newc;
count++;
}
}
return count;
}
int str_replace_first(char s[], int oldc, int newc)
{
size_t i;
for (i=0; s[i]!='\0'; i++)
{
if (s[i] == (char)oldc)
{
s[i] = (char)newc;
return 1; /* What exactly does this do? */
}
}
return 0;
}
return 1 escapes the function and returns a 1 to whatever has called it. return effectively escapes any function when it is called, this can be used in many applications to exit a function before it is 'complete'.
In this case:
int str_replace_first(char s[], int oldc, int newc)
{
size_t i;
for (i=0; s[i]!='\0'; i++)
{
if (s[i] == (char)oldc)
{
s[i] = (char)newc;
return 1; /* What exactly does this do? */
}
}
return 0;
}
The loop continues until it finds a character that matches oldc then replaces it with newc, then exits immediately before continuing on again. So as soon as it finds a match it will replace it then exit.
Here in str_replace_first when
s[i] == (char)oldc
condition becomes true in the loop, then then that character(old character) is replaced by the new one. and then return 1 returns the control to the calling function with a value of 1.(i.e neither the loop nor the function continues further).
1 was returned to mark that only 1 character was replaced.

Resources