0

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

3
  • Where is your initialize function? Commented Mar 3, 2017 at 5:59
  • oh sorry.. i will add it... Commented Mar 3, 2017 at 8:20
  • have you tried to put a breakpoint to y=index to see if it ever got assigned a value? Also after being assigned you should break the loop or next one can reassign y to -1. Commented Mar 3, 2017 at 8:28

2 Answers 2

1

The problem was that you never exited the loop when you found the matching record.

int isValid(char inputUN[], account acount[] ){ int index; for(index = 0; index < 10; index++){ if (strcmp(acount[index].unList, inputUN ) == 0){ return index; // return directly } } return -1; } 
Sign up to request clarification or add additional context in comments.

Comments

0

Once you find the match you should break out of the for loop by adding a break; statement. Otherwise unless it matches the last option it will return -1. Like this:

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; break; }else{ y= -1; } } return y; } 

or You can initialize y to -1 and test for it, like this:

int isValid(char inputUN[], account acount[] ){ int index; int y = -1; for(index = 0; index < 10 && y == -1; index++){ if (strcmp(acount[index].unList, inputUN ) == 0){ y = index; }else{ y= -1; } } return y; } 

3 Comments

Your solution still doesn't work because it has the else clause still
@ChrisTurner it's definitely redundant, but should not have any negative effect it you break out before the next iteration.
Sorry - hadn't spotted you'd altered the for exit condition. It's just not as clearly readable as the other solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.