Comments(/*...*/) longer than one line - c

I am supposed to count the /*...*/ comments in a file and I was wondering if this code is right or am I missing something out?
My Code
void commentsLongerThanOneLine(FILE* inputStream, FILE* outputStream) {
int count = 0, c, state = 1;
while ((c = fgetc(inputStream) != EOF)) {
switch (state) {
case 1: switch (c) {
case '/': state = 2; break;
}
break;
case 2: switch (c) {
case '/': state = 3; break;
case '*': state = 4; break;
default: state = 1;
}
break;
case 3: switch (c) {
case '\n': state = 1; break;
default: state = 4; count++;
}
break;
case 4:switch (c) {
case'*': if (c == '\n') count++; break;
default: state = 4; if (c == '\n') count++;
}
}
}
fprintf(outputStream, "Comments longer than one line: %d\n", count);
}

Related

C - "if" statement will never be executed [-Wswitch-unreachable] in a switch statement

So i am trying to make an switch statement that checks if the user has not already entered that switch statement before and I am trying to do this with an if statement inside the switch, but it always ignores the if statement and always just completes the case which is inside it, disregarding the other arguments, but what is also interesting is that it changes a to bijis, but doesnt do the actual condition check:
char bijis = '*';
char izv;
printf("\nChoose the next station ");
scanf("%d",&izv);
switch(izv) {
if (a != bijis) {
case 'a': case 'A':
a = bijis;
jaut_a();
break;
}
if (b != bijis) {
case 'b': case 'B':
b = bijis;
jaut_b();
break;
}
if (c != bijis) {
case 'c': case 'C':
c = bijis;
jaut_c();
break;
}
if (d != bijis) {
case 'd': case 'D':
d = bijis;
jaut_d();
break;
}
if (e != bijis) {
case 'e': case 'E':
e = bijis;
jaut_e();
break;
}
if (k1 != bijis) {
case '1':
k1 = bijis;
jaut_k1();
break;
}
if (k2 != bijis) {
case '2':
k2 = bijis;
jaut_k2();
break;
}
if (k3 != bijis) {
case '3':
k3 = bijis;
jaut_k3();
break;
}
if (k4 != bijis) {
case '4':
k4 = bijis;
jaut_k4();
break;
}
if (k5 != bijis) {
case '5':
k5 = bijis;
jaut_k5();
break;
}
if (k6 != bijis) {
case '6':
k6 = bijis;
jaut_k6();
break;
}
if (k7 != bijis) {
case '7':
k7 = bijis;
jaut_k7();
break;
}
if (k8 != bijis) {
case '8' :
k8 = bijis;
jaut_k8();
break;
}
if (k9 != bijis) {
case '9' :
k9 = bijis;
jaut_k9();
break;
}
default:
clearscr();
printf("Kļūdaina ievade, šajā lauciņā jau esi bijis! Ieraksti jebkuru burtu vai skaitli,kas rādīts tabulā.\n (Zvaigznītes parāda vietas kur jau esi bijis.");
sleep(3.5);
karte_plans();
break;
} ;
Your case labels are inside of the if blocks. The switch jumps directly to the relevant label, so the condition is jumped over.
Put the condition after the label.
switch(izv) {
case 'a': case 'A':
if (a != bijis) {
a = bijis;
jaut_a();
}
break;
case 'b': case 'B':
if (b != bijis) {
b = bijis;
jaut_b();
}
break;
...

How to do switching between two diffrent swich statements using C?

I have written a c code for switching between two different switch statements,
but I am looking for a more generic solution, meaning without using state variable and while loop, or jump from one switch statement to another switch statement.
I want some suggestion or key input from you.
Thank you and please excuse my poor English.
#include <stdio.h>
int main()
{
unsigned char state,state1;
enum
{
Add,Sub,Mul,Div,START,STOP
}Arith_Op;
enum
{
Add1,Sub1,Mul1,Div1,START1,STOP1
}Arith_Op1;
Arith_Op = Add;
Arith_Op1 = Add1;
state = START;
while (state != STOP)
{
switch(Arith_Op)
{
case Add :
printf("Add\n");
Arith_Op = Sub;
break;
case Sub :
printf("sub\n");
Arith_Op = Mul;
break;
case Mul :
printf("mul\n");
Arith_Op = Div;
break;
case Div :
printf("div\n");
Arith_Op = STOP;
break;
default :
printf("default\n");
state = STOP;
state1 = START1;
break;
}
}
printf(" state out of while and Switch case ");
while (state1 != STOP1)
{
switch(Arith_Op1)
{
case Add1 :
printf("Add1\n");
Arith_Op1 = Sub1;
break;
case Sub1 :
printf("sub1\n");
Arith_Op1 = Mul1;
break;
case Mul1 :
printf("mul1\n");
Arith_Op1 = Div;
break;
case Div1 :
printf("div1\n");
Arith_Op1 = STOP1;
break;
default :
printf("default1\n");
state1 = STOP1;
break;
}
}
printf(" state 1 out of while and Switch case ");
return 0;
}

Struct/Malloc or Other? SIGSEGV Error

I have debugged and watched my "watch window" to get an idea where the problem was. It does not even finish the code though. During the test sequence to find the user inputted word, it fails. It has a SIGSEGV fault at the found function.
The code is meant to take in a word puzzle and then allow the user to find the words.
puzzleInput.txt:
M,N,O,S,L,I,W,E,R,E,L,Y,T,L,E,A,G,N
A,H,O,O,V,E,R,T,A,Y,L,O,R,V,E,N,N,A
D,F,D,R,O,O,S,E,V,E,L,T,O,N,O,M,I,M
I,N,T,P,M,H,I,E,G,D,I,L,O,O,C,O,D,U
S,O,N,L,I,J,Q,A,D,A,M,S,S,R,N,N,R,R
O,X,L,O,G,E,F,F,M,O,I,R,E,E,G,R,A,T
N,I,B,T,S,O,R,A,O,R,M,O,V,A,W,O,H,N
F,N,H,U,R,R,B,C,R,N,L,R,E,G,B,E,W,E
R,O,S,D,C,O,E,A,E,I,O,N,L,A,U,A,J,R
K,W,U,N,L,H,H,F,N,I,O,S,T,N,S,W,A,U
E,R,B,A,J,B,A,C,F,S,S,M,N,H,H,R,D,B
N,E,W,L,O,T,O,N,K,E,C,E,I,H,T,H,A,N
N,T,H,E,H,L,A,C,A,K,J,N,N,H,O,I,M,A
E,R,G,V,N,C,A,F,I,N,G,H,U,H,A,J,S,V
D,A,R,E,S,J,C,N,T,T,A,R,N,B,O,Y,A,E
Y,C,A,L,O,D,L,N,O,S,I,R,R,A,H,W,E,O
N,E,N,C,N,E,T,N,N,O,T,N,I,L,C,O,E,S
D,A,T,Y,Y,P,O,L,K,G,A,R,F,I,E,L,D,R
enter code here
code:
/* This program reads a puzzle from a file
and allows the user to define a word which
they want to find in the word puzzle.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void fileNam(char[]);
char showMenu();
int fileData(char*);
void alphaArray();
void getUser();
void fileError(char*);
void findUserFirst();
int createString();
void found(int);
void notFound(char*);
void goToExit();
void display();
struct alphabet
{
int *A;int *B; int *C; int *D; int *E; int *F;
int *G; int *H; int *I; int *J; int *K; int *L; int *M;
int *N; int *O; int *P; int *Q; int *R; int *S;
int *T; int *U; int *V; int *W; int *X; int *Y; int *Z;
int a; int b; int c; int d; int e; int f; int g; int h; int i; int j; int k; int l; int m;
int n; int o; int p; int q; int r; int s; int t; int u; int v; int w; int x; int y; int z;
}fill;
struct user
{
struct alphabet* ptr;
int *attempt;
int testNum;
int columns;
int rows;
int userSize;
int puzzleSize;
char *puzzleArray;
char userWord[45];
}test;
int main()
{
int wordsFound = 0;
int fileErr = 0;
int wordF = 0;
char option;
char fileLoc[100] = {0};
do
{
fflush(stdin);
option = showMenu();
fflush(stdin);
if(fileErr == -1)
{
fileError(fileLoc);
}
switch(option)
{
case 'a':;
fileNam(fileLoc);
strcat(fileLoc,"\\puzzleInput.txt");
fileErr = fileData(fileLoc);
break;
case 'b':
display();
break;
case 'c':
getUser();
findUserFirst();
wordF = 0;
do{
wordF = createString();
if(wordF > 0)
{
found(wordF);
wordsFound++;
}
else
{
test.ptr;
}
}while(*test.attempt <= test.testNum);
if(wordF < -1)
notFound(fileLoc);
break;
case 'd':
goToExit(wordsFound);
break;
default:
printf("Invalid selection.\nPlease choose again.\n");
break;
}
}while(option != 'd');
return 0;
}
char showMenu()
{
char option;
printf("Choose one of the following:\n");
printf("\ta. Enter file location(full path)\n");
printf("\tb. Display the puzzle\n");
printf("\tc. Find a word\n");
printf("\td. Exit\n");
option = getchar();
return option;
}
void goToExit(int found)
{
printf("\nYou found %d words!\n", found);
if(found > 5)
{
printf("Awesome job! Come back to play again!");
}
else{
printf("Better luck next time!");
}
printf("Press any key to close window.");
getchar();
free(fill.A);free(fill.B); free(fill.C); free(fill.D); free(fill.E); free(fill.F);
free(fill.G); free(fill.H); free(fill.I); free(fill.J); free(fill.K); free(fill.L); free(fill.M);
free(fill.N); free(fill.O); free(fill.P); free(fill.Q); free(fill.R); free(fill.S);
free(fill.T); free(fill.U); free(fill.V); free(fill.W); free(fill.X); free(fill.Y); free(fill.Z);
free(test.puzzleArray);
}
void display()
{
int i, j, k = 0;
for(i=0; i <= test.columns; i++)
{
for(j=0; j <= test.rows; j++)
{
printf("%c, ", test.puzzleArray[k]);
k++;
}
printf("\n");
}
}
void fileNam(char fileLoc[])
{
printf("When entering file location, do not \n");
printf("include the file name.\nLOCATION:\n");
gets(fileLoc);
}
void fileError(char* fileName)
{
printf("There was an error with the file location");
printf(" that you typed in.\nPlease make sure file ");
printf("location and name are correct.\nIf name is wrong, ");
printf("please change the name of your file to match.\n");
printf("FILE LOCATION AND NAME: \"%s\"\n\n", fileName);
}
int fileData(char* fname)
{
int i = 0;
char curr;
FILE * fPtr;
fPtr = fopen(fname, "r");
char buffer[1000] = {0};
if(fPtr == NULL)//test if file opened
{
printf("There has been an error in opening %s!\n", fname);
return -1;
}
while (!feof(fPtr))
{
fgets(buffer, 1000, fPtr);
test.rows++;
}
rewind(fPtr);
while(fgetc(fPtr) >= 65 && fgetc(fPtr) <= 90)
{
test.puzzleSize++;
}
test.puzzleArray = (char*) malloc((test.puzzleSize) * sizeof(char));
printf("%d", test.puzzleSize);
rewind(fPtr);
i = 0;
while ((curr = fgetc(fPtr)) != EOF)
{
if(curr == ',' || curr == '\n')
{
if(curr == '\n')
test.columns++;
}
if(curr >= 65 && curr <= 90)
{
test.puzzleArray[i] = curr;
i++;
}
}
fclose(fPtr);
alphaArray();
return 0;
}
void alphaArray()
{
int i = 0;
char current = 0;
while(test.puzzleArray[i] != '\0')
{
current = test.puzzleArray[i];
switch(tolower(current))//accidentally coded lowercase, quick fix
{
case 'a':
fill.a++;
break;
case 'b':
fill.b++;
break;
case 'c':
fill.c++;
break;
case 'd':
fill.d++;
break;
case 'e':
fill.e++;
break;
case 'f':
fill.f++;
break;
case 'g':
fill.g++;
break;
case 'h':
fill.h++;
break;
case 'i':
fill.i++;
break;
case 'j':
fill.j++;
break;
case 'k':
fill.k++;
break;
case 'l':
fill.l++;
break;
case 'm':
fill.m++;
break;
case 'n':
fill.n++;
break;
case 'o':
fill.o++;
break;
case 'p':
fill.p++;
break;
case 'q':
fill.q++;
break;
case 'r':
fill.r++;
break;
case 's':
fill.s++;
break;
case 't':
fill.t++;
break;
case 'u':
fill.u++;
break;
case 'v':
fill.v++;
break;
case 'w':
fill.w++;
break;
case 'x':
fill.x++;
break;
case 'y':
fill.y++;
break;
case 'z':
fill.z++;
break;
default: printf("\n");
break;
}
i++;
}
fill.A = (int *)malloc(sizeof(int)*fill.a); fill.B = (int *)malloc(sizeof(int)*fill.b);
fill.C = (int *)malloc(sizeof(int)*fill.c); fill.D = (int *)malloc(sizeof(int)*fill.d);
fill.E = (int *)malloc(sizeof(int)*fill.e); fill.F = (int *)malloc(sizeof(int)*fill.f);
fill.G = (int *)malloc(sizeof(int)*fill.g); fill.H = (int *)malloc(sizeof(int)*fill.h);
fill.I = (int *)malloc(sizeof(int)*fill.i); fill.J = (int *)malloc(sizeof(int)*fill.j);
fill.K = (int *)malloc(sizeof(int)*fill.k); fill.L = (int *)malloc(sizeof(int)*fill.l);
fill.M = (int *)malloc(sizeof(int)*fill.m); fill.N = (int *)malloc(sizeof(int)*fill.n);
fill.O = (int *)malloc(sizeof(int)*fill.o); fill.P = (int *)malloc(sizeof(int)*fill.p);
fill.Q = (int *)malloc(sizeof(int)*fill.q); fill.R = (int *)malloc(sizeof(int)*fill.r);
fill.S = (int *)malloc(sizeof(int)*fill.s); fill.T = (int *)malloc(sizeof(int)*fill.t);
fill.U = (int *)malloc(sizeof(int)*fill.u); fill.V = (int *)malloc(sizeof(int)*fill.v);
fill.W = (int *)malloc(sizeof(int)*fill.w); fill.X = (int *)malloc(sizeof(int)*fill.x);
fill.Y = (int *)malloc(sizeof(int)*fill.y); fill.Z = (int *)malloc(sizeof(int)*fill.z);
fill.a = 0;fill.b =0; fill.c =0; fill.d =0; fill.e =0; fill.f =0;
fill.g =0; fill.k =0; fill.o =0; fill.s =0; fill.w =0;
fill.h =0; fill.l =0; fill.p =0; fill.t =0; fill.x =0;
fill.i =0; fill.m =0; fill.q =0; fill.u =0; fill.y =0;
fill.j =0; fill.n =0; fill.r =0; fill.v =0; fill.z =0;
i = 0;
while(test.puzzleArray[i] != '\0')
{
current = test.puzzleArray[i];
switch(tolower(current))//accidentally coded lowercase, quick fix
{
case 'a':
fill.A[fill.a] = i;
fill.a++;
break;
case 'b':
fill.B[fill.b] = i;
fill.b++;
break;
case 'c':
fill.C[fill.c] = i;
fill.c++;
break;
case 'd':
fill.D[fill.d] = i;
fill.d++;
break;
case 'e':
fill.E[fill.e] = i;
fill.e++;
break;
case 'f':
fill.F[fill.f] = i;
fill.f++;
break;
case 'g':
fill.G[fill.g] = i;
fill.g++;
break;
case 'h':
fill.H[fill.h] = i;
fill.h++;
break;
case 'i':
fill.I[fill.i] = i;
fill.i++;
break;
case 'j':
fill.J[fill.j] = i;
fill.j++;
break;
case 'k':
fill.K[fill.k] = i;
fill.k++;
break;
case 'l':
fill.L[fill.l] = i;
fill.l++;
break;
case 'm':
fill.M[fill.m] = i;
fill.m++;
break;
case 'n':
fill.N[fill.n] = i;
fill.n++;
break;
case 'o':
fill.O[fill.o] = i;
fill.o++;
break;
case 'p':
fill.P[fill.p] = i;
fill.p++;
break;
case 'q':
fill.Q[fill.q] = i;
fill.q++;
break;
case 'r':
fill.R[fill.r] = i;
fill.r++;
break;
case 's':
fill.S[fill.s] = i;
fill.s++;
break;
case 't':
fill.T[fill.t] = i;
fill.t++;
break;
case 'u':
fill.U[fill.u] = i;
fill.u++;
break;
case 'v':
fill.V[fill.v] = i;
fill.v++;
break;
case 'w':
fill.W[fill.w] = i;
fill.w++;
break;
case 'x':
fill.X[fill.x] = i;
fill.x++;
break;
case 'y':
fill.Y[fill.y] = i;
fill.y++;
break;
case 'z':
fill.Z[fill.z] = i;
fill.z++;
break;
default: printf("\n");
break;
}
i++;
}
}
void getUser()
{
test.testNum = 0;
test.userWord[45] = '\0';
test.ptr = 0;
printf("\nWhat word would you like to search for?\n");
scanf("%s", test.userWord);
test.userSize = strlen(test.userWord);
}
void findUserFirst()
{
switch(tolower(test.userWord[0]))
{
case 'a':
test.attempt = &fill.A[0];
test.testNum = fill.a;
break;
case 'b':
test.attempt = &fill.B[0];
test.testNum = fill.b;
break;
case 'c':
test.attempt = &fill.C[0];
test.testNum = fill.c;
break;
case 'd':
test.attempt = &fill.D[0];
test.testNum = fill.d;
break;
case 'e':
test.attempt = &fill.E[0];
test.testNum = fill.e;
break;
case 'f':
test.attempt = &fill.F[0];
test.testNum = fill.f;
break;
case 'g':
test.attempt = &fill.G[0];
test.testNum = fill.g;
break;
case 'h':
test.attempt = &fill.H[0];
test.testNum = fill.h;
break;
case 'i':
test.attempt = &fill.I[0];
test.testNum = fill.i;
break;
case 'j':
test.attempt = &fill.J[0];
test.testNum = fill.j;
break;
case 'k':
test.attempt = &fill.K[0];
test.testNum = fill.k;
break;
case 'l':
test.attempt = &fill.L[0];
test.testNum = fill.l;
break;
case 'm':
test.attempt = &fill.M[0];
test.testNum = fill.m;
break;
case 'n':
test.attempt = &fill.N[0];
test.testNum = fill.n;
break;
case 'o':
test.attempt = &fill.O[0];
test.testNum = fill.o;
break;
case 'p':
test.attempt = &fill.P[0];
test.testNum = fill.p;
break;
case 'q':
test.attempt = &fill.Q[0];
test.testNum = fill.q;
break;
case 'r':
test.attempt = &fill.R[0];
test.testNum = fill.r;
break;
case 's':
test.attempt = &fill.S[0];
test.testNum = fill.s;
break;
case 't':
test.attempt = &fill.T[0];
test.testNum = fill.t;
break;
case 'u':
test.attempt = &fill.U[0];
test.testNum = fill.u;
break;
case 'v':
test.attempt = &fill.V[0];
test.testNum = fill.v;
break;
case 'w':
test.attempt = &fill.W[0];
test.testNum = fill.w;
break;
case 'x':
test.attempt = &fill.X[0];
test.testNum = fill.x;
break;
case 'y':
test.attempt = &fill.Y[0];
test.testNum = fill.y;
break;
case 'z':
test.attempt = &fill.Z[0];
test.testNum = fill.z;
break;
default: printf("\n");
}
}
int createString()
{
int i = 0, upRoom = 0, rightRoom = 0;
int leftRoom = 0, downRoom = 0;
int columnPos = 0, rowPos = 0;
int arPos = 0;
int moveup = 0, moveupr = 0;
int moveupl = 0, mover = 0;
int movel = 0, moved = 0;
int movedr = 0, movedl = 0;
char up[45];
char upRight[45];
char right[45];
char downRight[45];
char down[45];
char downLeft[45];
char left[45];
char upLeft[45];
arPos = *test.attempt;
columnPos = arPos / test.columns;
rowPos = arPos % test.rows;
upRoom = test.columns/2 - columnPos;
downRoom = test.columns/2 - columnPos;
rightRoom = test.rows/2 - rowPos;
leftRoom = test.rows/2 - rowPos;
moveup = -(test.rows);
moveupr = (test.rows - 1) * (-1),
moveupl = -(test.rows + 1);
mover = 1;
movel = 1;
moved = test.rows,
movedr = 1 * (test.rows + 1);
movedl = 1 * (test.rows - 1);
for(i=0; i < test.userSize; i++)
{
if(rightRoom >= 0)
{
right[i] = test.puzzleArray[arPos + i*mover];
if(upRoom >= 0)
upRight[i] = test.puzzleArray[arPos + i*moveupr];
if(downRoom >= 0)
downRight[i] = test.puzzleArray[arPos + i*movedr];
}
if(leftRoom >= 0)
{
left[i] = test.puzzleArray[arPos - movel];
if(upRoom >= 0)
upLeft[i] = test.puzzleArray[arPos + i*moveupl];
if(downRoom >= 0)
downLeft[i] = test.puzzleArray[arPos + i*movedl];
}
if(upRoom >= 0)
up[i] = test.puzzleArray[arPos + i*moveup];
if(downRoom >= 0)
down[i] = test.puzzleArray[arPos + i*moved];
}
for(i = 0; i < test.userSize; i++)
{
if(strcmp(up, test.userWord) == 0)
return 1;
if(strcmp(upRight, test.userWord) == 0)
return 2;
if(strcmp(right, test.userWord) == 0)
return 3;
if(strcmp(downRight, test.userWord) == 0)
return 4;
if(strcmp(down, test.userWord) == 0)
return 5;
if(strcmp(downLeft, test.userWord) == 0)
return 6;
if(strcmp(left, test.userWord) == 0)
return 7;
if(strcmp(upLeft, test.userWord) == 0)
return 8;
else
return -1;
}
return -1;
}
void found(int style)
{
int pos = 0, movement = 0;
int i, j, k = 0;
char *cpyPuzzle;
cpyPuzzle = (char*) malloc((test.puzzleSize) * sizeof(char));
strcpy(cpyPuzzle, test.puzzleArray);
switch(style){
case 1:
movement = test.rows;
break;
case 2:
movement = test.rows - 1;
break;
case 3:
movement = 1;
break;
case 4:
movement = test.rows + 1;
break;
case 5:
movement = test.rows;
break;
case 6:
movement = test.rows - 1;
break;
case 7:
movement = 1;
break;
case 8:
movement = test.rows + 1;
break;
}
for(i = 0; i < test.puzzleSize; i++)
{
cpyPuzzle[i] = '~';
}
for(i=0; i < test.userSize; i++)
{
pos = test.attempt + (i*movement);
cpyPuzzle[pos] = test.userWord[i];
}
for(i=0; i < test.columns; i++)
{
for(j=0; j < test.rows; j++)
{
printf("%c, ", cpyPuzzle[k]);
k++;
}
printf("\n");
}
free(cpyPuzzle);
}
void notFound(char *fileN)
{
printf("In file %s %c was not found.\n", test.userWord, fileN);
printf("You can try finding another word");
printf(" by pressing the option to show puzzle and\n");
printf("then the option to find a word, once you've found one.\n");
}
(Posted on behalf of the OP).
SOLVED! - the error was I was not dereferencing and then referencing a pointer.

Variable is printed properly right before an if else loop, but does not retain that value one in the if else loop

I've written a program that reads an account value from a text file and then assigns these values to a variable for each account. Once this is done, reading from the same text file, an action to be done to each account value is identified in the format "Account# Actiontype ActionModifier". All the correct values are being scanned and the proper values are being stored when I insert printfs to check, but immediately after the correct value is printed, the value is put through an else if loop. When doing this for some reason the value is being read as 1 no matter what and is executing the actions in the else if loop as if it were equal to 1.
loopState = 1;
while(loopState != 0)
{
for(i = 1;i < 100 ;i++)
{
if(i < 6)// This executes fine
{
fscanf(bankfile,"%f",&accValue);
switch(i)
{
case 1:
acc1 = accValue;
break;
case 2:
acc2 = accValue;
break;
case 3:
acc3 = accValue;
break;
case 4:
acc4 = accValue;
break;
case 5:
acc5 = accValue;
printf("Done module one\n");
break;
}
}
else // scan for the the first value, than the second, then the third
{
fscanf(bankfile,"%d",&accNum);
if(accNum != 0)
{
fscanf(bankfile,"%c",&accAction);
if(accAction == 'W' || accAction == 'D')
{
fscanf(bankfile,"%f",&actValue);
printf("%d %c %.2f\n",accNum,accAction,actValue);//this will be correct
printf("%d\n",accNum); //this will print say "2"
if(accNum = 1)//but this if statement will be run
{//thus printing the below printf
printf("If accNum = 1 this will print\n");
switch(accAction)
{
case 'W':
final1 = withdrawl(acc1,actValue);
break;
case 'D':
final1 = deposit(acc1,actValue);
break;
}
}
else if(accNum = 2)
{
switch(accAction)
{
case 'W':
final2 = withdrawl(acc2,actValue);
break;
case 'D':
final2 = deposit(acc2,actValue);
break;
}
}
else if(accNum = 3)
{
switch(accAction)
{
case 'W':
final3 = withdrawl(acc3,actValue);
break;
case 'D':
final3 = deposit(acc3,actValue);
break;
}
}
else if(accNum = 4)
{
switch(accAction)
{
case 'W':
final4 = withdrawl(acc4,actValue);
break;
case 'D':
final4 = deposit(acc4,actValue);
break;
}
}
else if(accNum = 5)
{
switch(accAction)
{
case 'W':
final5 = withdrawl(acc5,actValue);
break;
case 'D':
final5 = deposit(acc5,actValue);
break;
}
}
}
else if(accAction == 'B' || accAction == 'U')
{
printf("%d %c\n",accNum,accAction);//this will be correct
printf("%d\n",accNum);//this will print say "4"
if(accNum = 1)//but this if statement will be run
{//thus printing this printf below
printf("If accNum = 1 this will print\n");
switch(accAction)
{
case 'B':
final1 = balance(acc1);
break;
case 'U':
final1 = update(acc1);
break;
}
}
else if(accNum = 2)
{
switch(accAction)
{
case 'B':
final2 = balance(acc2);
break;
case 'U':
final2 = update(acc2);
break;
}
}
else if(accNum = 3)
{
switch(accAction)
{
case 'B':
final3 = balance(acc3);
break;
case 'U':
final3 = update(acc3);
break;
}
}
else if(accNum = 4)
{
switch(accAction)
{
case 'B':
final4 = balance(acc4);
break;
case 'U':
final4 = update(acc4);
break;
}
}
else if(accNum = 5)
{
switch(accAction)
{
case 'B':
final5 = withdrawl(acc5,actValue);
break;
case 'U':
final5 = deposit(acc5,actValue);
break;
}
}
}
}
else
loopState = 0;
}
}
}
printf("Exited Loop");
}
From my understanding I can't see where the value is being altered if just the line before it is being seen as the correct value
Your conditional statements are doing assignment.
else if (accNum = 3)
should be
else if (accNum == 3)

C SDL Keyboard Events SDL_KEYUP Triggering When Key Is Down

I am using SDL in C, and I'm trying to make a character move on the screen when a key is pressed, and stop when it is released, but it seems as if the KEYUP event is triggering when the key is still being held down. If I remove the KEYUP section in pressed(), the characters will slide across the screen, but obviously won't stop on their own. If I leave the KEYUP section in, I have to press the key repeatedly to move them across the screen.
Any idea what I'm doing wrong?
Here's what I have:
...
int done = 0;
while (done==0)
{
pressed();
if (kenter == 1)
{
state = 1;
subscreen();
}
if (kescape == 1)
{
done = 1;
}
if (kup == 1)
{
playery += -2;
}
if (kdown == 1)
{
playery += 2;
}
if (kright == 1)
{
playerx += 2;
}
if (kleft == 1)
{
playerx += - 2;
}
...
int pressed ()
{
SDL_Event keyevent;
while (SDL_PollEvent(&keyevent))
{
switch(keyevent.type)
{
case SDL_KEYDOWN:
switch(keyevent.key.keysym.sym)
{
case SDLK_RETURN:
{
kenter = 1;
break;
}
case SDLK_ESCAPE:
{
kescape = 1;
break;
}
case SDLK_a:
{
ka = 1;
break;
}
case SDLK_s:
{
ks = 1;
break;
}
case SDLK_UP:
{
kup = 1;
break;
}
case SDLK_DOWN:
{
kdown = 1;
break;
}
case SDLK_RIGHT:
{
kright = 1;
break;
}
case SDLK_LEFT:
{
kleft = 1;
break;
}
default:
break;
}
}
switch(keyevent.type)
{
case SDL_KEYUP:
switch(keyevent.key.keysym.sym)
{
case SDLK_RETURN:
{
kenter = 0;
break;
}
case SDLK_ESCAPE:
{
kescape = 0;
break;
}
case SDLK_a:
{
ka = 0;
break;
}
case SDLK_s:
{
ks = 0;
break;
}
case SDLK_UP:
{
kup = 0;
break;
}
case SDLK_DOWN:
{
kdown = 0;
break;
}
case SDLK_RIGHT:
{
kright = 0;
break;
}
case SDLK_LEFT:
{
kleft = 0;
break;
}
default:
break;
}
}
}
return 0;
}
I think your switch statements are broken.
Use this less confusing way
int pressed ()
{
SDL_Event event;
while(SDL_PollEvent(&event) )
{
if(event.type == SDLK_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_RETURN:
doStuff = 1
break;
default:
break;
}
}
if(event.type == SDLK_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_RETURN:
doStuff = 0;
break;
default:
break;
}
}
}
}
Also important:
SDL Tutorials: Practical Keyboard Input
Oh and avoid using global variables!
Your use of two switch statements is odd and confusing, you should probably fix that.
The issues you're seeing are likely due to keyboard repeat, see the SDL_EnableKeyRepeat() call for how to disable it.
don't use switch. I experienced same thing, but I resolved into using 'if' like this. Maybe switch has 'else if'.
if(event.type == SDL_KEYDOWN){
....
}
if(event.type == SDL_KEYUP){
....
}

Resources