저는 지금 C를 몇 주 동안 연습 해 왔으며 코드에서 잘못했을 수도있는 것을 파악하려고합니다.함수가 예상 값을 반환하지 않습니다.
#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;
}
내가 정말이 프로그램을 수행하려고하면 프로그램이 사용자 이름 입력을 요청하고 모두 구조에 후 일부 금액을 표시하는 경우 핀 다음이 검사,하지만 난이를 ommitted 점이다 사용자 이름이 구조에있는 경우 내 문제는 isValid()
기능에 있기 때문에 코드의 나머지 ...이 예상되는이 기능에
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;
}
다른이 -1
가 반환되는 요소의 인덱스를 돌려줍니다. else if
문에 주석을 달면 잘 작동합니다. 하지만 그렇지 않다면 올바른 요소를 입력 했더라도 항상 -1
을 반환합니다.
내가 잘못했을 수도 있습니다.
P. 죄송합니다. 질문이 너무 길어서 스택 오버플로가 매우 어려웠습니다.
'초기화'기능은 어디에 있습니까? – haccks
오, 미안 .. 내가 추가 할 것입니다 ... –
y = index에 중단 점을 넣으려고 했습니까? 또한 할당 된 후에는 루프를 깨뜨리거나 다음을 -1로 재 할당 할 수 있습니다. – koksalb