I've been practicing C for quite a few weeks now and I'm tryin to figure out what I might have done wrong in my code.
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct accounts{ char unList[32]; int pinList; float amtList; }account; int isValid(char inputUN[], account acount[]); void initialize(account acount[], char unList[][10], int pinList[], float amtList[], int size); int main(int argc, char *argv[]) { int size = 10; account newAccs[size]; char unList[][10] = {"franklin", "woods", "phillips", "gomez", "burns", "porter", "griffin", "spencer", "hanson", "johnson"}; char inputUN[32]; int index; initialize(newAccs, unList, pinList, amtList, size); printf("Enter Username: "); scanf("%s", inputUN); index = isValid(inputUN, newAccs); printf("%d\n", index); return 0; } void initialize(account acount[], char unList[][10], int pinList[], float amtList[], int size){ int index; for(index = 0; index < size; index++){ strcpy(acount[index].unList, unList[index]); acount[index].pinList = pinList[index]; acount[index].amtList = amtList[index]; } } int isValid(char inputUN[], account acount[] ){ int index; int y; for(index = 0; index < 10; index++){ if (strcmp(acount[index].unList, inputUN ) == 0){ y = index; }else{ y= -1; } } return y; } What I am really trying to do in this program is that the program is asking for a username input and Pin then it checks if both are in the structure and then it shows some amount, but i have ommitted the rest of the code since my problem is in the isValid() function...
int isValid(char inputUN[], account acount[] ){ int index; int y; for(index = 0; index < 10; index++){ if (strcmp(acount[index].unList, inputUN ) == 0){ y = index; }else{ y= -1; } } return y; } in this function it is supposed return the index of the element if the username is in the structure, else it returns -1. It works well if I placed comments in the else if statement. But if not, it always returns -1 even if I have input a correct element.
What might I have done wrong?
P.S. Sorry If my question was too long, I'm quite new to Stacks Overflow
initializefunction?