2017-03-03 8 views
0

私はかなりの数週間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; 
} 

私が本当にこのプログラムでやろうとしていますと、プログラムがユーザー名の入力を求めているとの両方が構造であり、それはいくつかの量を示した場合にピンが、それはチェックしますが、私は省略されているということです私の問題は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.S.私の質問が長すぎた場合、あなたはbreak;ステートメントを追加することにより、forループから抜け出す必要があり、一致が見つかったら申し訳ありませんが、私は、スタックオーバーフロー

+0

あなたの 'initialize'関数はどこにありますか? – haccks

+0

ああ申し訳ありません..私はそれを追加します... –

+0

y = indexにブレークポイントを設定して値を割り当てられているかどうか確認しましたか?また割り当てられた後、ループを破るか、次のものを-1に再割り当てすることができます。 – koksalb

答えて

0

に非常に新しいです。それ以外の場合は、最後のオプションと一致しない限り-1を返します。このように:

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

またはあなたはそれのために-1とテストにyを初期化することができ、このように:

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; 
} 
+0

あなたの解決策はまだelse節があるので動作しません –

+0

@ChrisTurnerそれは間違いなく冗長ですが、次の反復の前に壊れてしまってはいけません。 – MotKohn

+0

申し訳ありません - 終了条件を変更していることがわかりませんでした。それは他の解決策と同じくらいはっきりと読めるものではありません。 –

1

問題は、あなたが一致するレコードを見つけたときにループを終了したことがないということでした。

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; 
} 
関連する問題