Convert a string from argv to unsigned byte array - c

For example i need an
unsigned code[] {
0x0001,
0x0002,
0x0003,
.. and so on
}
from argv. I would like to input it as a stream like this:
000100020003 and so on

unsigned hextodigit(int hex)
{
unsigned result = 0;
switch(toupper(hex))
{
case '0':
return 0;
break;
case '1':
return 1;
break;
case '2':
return 2;
break;
case '3':
return 3;
break;
case '4':
return 4;
break;
case '5':
return 5;
break;
case '6':
return 6;
break;
case '7':
return 7;
break;
case '8':
return 8;
break;
case '9':
return 9;
break;
case 'A':
return 10;
break;
case 'B':
return 11;
break;
case 'C':
return 12;
break;
case 'D':
return 13;
break;
case 'E':
return 14;
break;
case 'F':
return 15;
break;
default:
break;
}
}
unsigned fromhex4chars(const char *str)
{
return hextodigit(*(str + 3)) + (hextodigit(*(str + 2)) << 4) + (hextodigit(*(str + 1)) << 8) + (hextodigit(*str) << 12);
}
void convert(const char *str, unsigned *buff)
{
size_t len = strlen(str) / 4;
while(len--)
{
*buff++ = fromhex4chars(str);
str += 4;
}
}
int main(int argc, char **argv)
{
unsigned x[100];
convert(argv[1], x);
return 0;
}

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;
...

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.

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

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);
}

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)

Converting normal numbers into roman numerals using switch statement in C

so i did dis little activity from school works fine with only if else's but then my professor told me to convert it to switch which is giving me a hard time coz when i run some numbers like 11 are out putted as only I instead of XI pls halp me :((((
#include<stdio.h>
#include<conio.h>
int main()
{
int R,M,D,C,L,X,V,I,H=0;
char roman_string= ' ';
printf("\n\nEnter number here: ");
scanf("%d",&R);
M=R/1000;
L=R%1000%500%100/50;
X=R%1000%500%100%50/10;
V=R%1000%500%100%50%10/5;
I=R%1000%500%100%50%10%5/1;
if(R>3000){
roman_string= 'A';
}
if(M==3){
roman_string= 'B';
}
if(M==2){
roman_string= 'C';
}
if(M==1){
roman_string= 'D';
}
D=R%1000/500;
if((D==1) && (C==4)){
roman_string= 'E';
}
if ((D==1) && (C!=4)){
roman_string='F';
}
if((D==0) && (C==4)){
roman_string= 'G';
}
C=R%1000%500/100;
if(C==3){
roman_string= 'H';
}
if(C==2){
roman_string= 'I';
}
if(C==1){
roman_string= 'J';
}
if((L==1) && (X==4)){
roman_string= 'K';
}
if((L==1) && (X!=4)){
roman_string='L';
}
if((L==0) && (X==4)){
roman_string='M';
}
if(X==1){
roman_string='N';
}
if(X==2){
roman_string='O';
}
if(X==3){
roman_string='P';
}
if((V==1) &&(I!=4)){
roman_string='Q';
}
if(I==1){
roman_string='R';
}
if(I==2) {
roman_string='S';
}
if(I==3) {
roman_string='T';
}
if((I==4) && (V%2==1)) {
roman_string='U';
}
if((I==4) && (V%2==0)){
roman_string='V';
}
switch (roman_string)
{
case 'A' :
printf("Invalid Input");
break;
case 'B':
printf("MMM");
break;
case 'C':
printf("MM");
break;
case 'D':
printf("M");
break;
case 'E':
printf("CM");
break;
case 'F':
printf("D");
break;
case 'G':
printf("CD");
break;
case 'H':
printf("CCC");
break;
case 'I':
printf("CC");
break;
case 'J':
printf("C");
break;
case 'K':
printf("XC");
break;
case 'L':
printf("L");
break;
case 'M':
printf("XL");
break;
case 'N':
printf("X");
break;
case'O':
printf("XX");
break;
case 'P':
printf("XXX");
break;
case 'Q':
printf("V");
break;
case 'R':
printf("I");
break;
case 'S':
printf("II");
break;
case 'T':
printf("III");
break;
case 'U':
printf("IX");
break;
case 'V':
printf("IV" );
break;
}
getch();
return 0;
}
Any time you see a bunch of if statements piled up that just compare the same variable to different integer constants like so:
if(x == 1) {
do_1();
}
else if(x == 2) {
do_2();
}
else if(x == 3) {
do_3();
}
/// etc.
else {
do_default();
}
You can change them into a switch statement:
switch(x) {
case 1:
do_1();
break;
case 2:
do_2();
break;
case 3:
do_3();
break;
/// etc.
default:
do_default();
}

Resources