2017-11-13 4 views
0

imはprint関数内のすべての国を印刷して、それを2番目のifステートメントに渡すことができますが、印刷されていないようです。構造体配列の名前の値をC言語のリファレンスとして渡すにはどうすればよいですか?

printf( "%s \ n"、ctryList [numCountries] .countryName);

部分ですが、何が問題なのか分かりません。

#include <stdio.h> 
#include <string.h> 
#include <stdbool.h> 

const int MAX_COUNTRY_NAME_LENGTH = 50; 

typedef struct CountryTvWatch_struct { 
    char countryName[50]; 
    int tvMinutes; 
} CountryTvWatch; 

void PrintCountryNames(CountryTvWatch ctryList[], int numCountries) 
{ 
    int i; 
    for(i = 0; i < numCountries; i++) 
    { 
    printf("%s\n", ctryList[numCountries].countryName); 
    } 
    return; 
} 

int main(void) { 
    // Source: www.statista.com, 2010 
    const int NUM_COUNTRIES = 4; 

    CountryTvWatch countryList[NUM_COUNTRIES]; 
    char countryToFind[MAX_COUNTRY_NAME_LENGTH]; 
    bool countryFound = false; 
    int i = 0; 

    strcpy(countryList[0].countryName, "Brazil"); 
    countryList[0].tvMinutes = 222; 
    strcpy(countryList[1].countryName, "India");  
    countryList[1].tvMinutes = 119; 
    strcpy(countryList[2].countryName, "U.K.");   
    countryList[2].tvMinutes = 242; 
    strcpy(countryList[3].countryName, "U.S.A.");  
    countryList[3].tvMinutes = 283; 

    printf("Enter country name: \n"); 
    scanf("%s", countryToFind); 

    countryFound = false; 
    for (i = 0; i < NUM_COUNTRIES; ++i) { // Find country's index 
     if (strcmp(countryList[i].countryName, countryToFind) == 0) { 
     countryFound = true; 
     printf("People in %s watch\n", countryToFind); 
     printf("%d minutes of TV daily.\n", countryList[i].tvMinutes); 
     } 
    } 
    if (!countryFound) { 
     printf("Country not found, try again.\n"); 
     printf("Valid countries:\n"); 
     PrintCountryNames(countryList, NUM_COUNTRIES); 
    } 

    return 0; 
} 
+3

'ctryListは[numCountries]'存在しません。配列 'ctryList'は' ctryList [0] '、' ctryList [1] '、...、' ctryList [numCountries-1] 'までしか持たない。 'ctryList [i] .countryName'を印刷したいと思うかもしれません。 – AlexP

+0

投稿されたコードにはいくつかの「魔法」の数字が含まれています。 「魔法」の数字は、基準のない数字です。 I. 50. 'magic'という数字は、コードを理解し、デバッグするのをはるかに難しくします。投稿されたコードは、 'magic'という数字を意味のある名前にするための '#define'ステートメントを持っていますが、コード。 – user3629249

+0

関数の 'scanf()'ファミリを呼び出すときは、1)操作が成功したことを保証するために、返された値(パラメータ値ではない)を常にチェックします。 2) '%s'形式指定子を使用する場合は、常に入力フィールドの長さよりも1小さいMAX_CHARACTERS修飾子を含める。a)バッファオーバーフローの可能性を回避し、結果として未定義の動作を回避する。b)NULバイトが入力バッファに追加されます。 – user3629249

答えて

0

次提案コード:

  1. が質問
  2. へのコメントを取り入れ、適切ユーザーが
  3. から選択すること使用可能な国を知ることができます
  4. I/Oエラーをチェックします
  5. は、読みやすさのために、水平方向と垂直方向の両方に適切に配置されています。
  6. は、各ヘッダファイルが

、現在提案されているコードが含まれている理由onality

  • はきれい
  • ドキュメントをコンパイル:

    #include <stdio.h>  // scanf(), printf() 
    #include <stdlib.h> // exit(), EXIT_FAILURE 
    #include <string.h> // strcmp() 
    #include <stdbool.h> // bool, true, false 
    
    #define MAX_COUNTRY_NAME_LENGTH 50 
    #define NUM_COUNTRIES 4 
    
    struct CountryTvWatch_struct 
    { 
        char countryName[ MAX_COUNTRY_NAME_LENGTH ]; 
        int tvMinutes; 
    }; 
    typedef struct CountryTvWatch_struct CountryTvWatch; 
    
    
    // prototypes 
    void PrintCountryNames(CountryTvWatch ctryList[], int numCountries); 
    
    
    int main(void) 
    { 
        // Source: www.statista.com, 2010 
    
        CountryTvWatch countryList[NUM_COUNTRIES]; 
        char countryToFind[ MAX_COUNTRY_NAME_LENGTH+1]; 
    
        strcpy(countryList[0].countryName, "Brazil"); 
        countryList[0].tvMinutes = 222; 
    
        strcpy(countryList[1].countryName, "India"); 
        countryList[1].tvMinutes = 119; 
    
        strcpy(countryList[2].countryName, "U.K."); 
        countryList[2].tvMinutes = 242; 
    
        strcpy(countryList[3].countryName, "U.S.A."); 
        countryList[3].tvMinutes = 283; 
    
        // let user know what countries are available and how they are spelled 
        PrintCountryNames(countryList, NUM_COUNTRIES); 
    
        printf("Enter country name: \n"); 
    
        // Note: following statement 
        //  checks for error 
        //  includes a MAX_CHAR modifier that is one less than 
        // the length of the input field 
        if(1 != scanf("%49s", countryToFind)) 
        { 
         perror("scanf failed"); 
         exit(EXIT_FAILURE); 
        } 
    
        // implied else, scanf successful 
    
    
        bool countryFound = false; 
    
        for (int i = 0; i < NUM_COUNTRIES; ++i) 
        { // Find country's index 
         if (strcmp(countryList[i].countryName, countryToFind) == 0) 
         { 
         countryFound = true; 
         printf("People in %s watch\n", countryToFind); 
         printf("%d minutes of TV daily.\n", countryList[i].tvMinutes); 
         break; // exit the search loop early 
         } 
        } 
    
        if (!countryFound) 
        { 
         printf("Country not found, try again.\n"); 
         printf("Valid countries:\n"); 
         PrintCountryNames(countryList, NUM_COUNTRIES); 
        } 
    
        return 0; 
    } 
    
    
    void PrintCountryNames(CountryTvWatch ctryList[], int numCountries) 
    { 
        for(int i = 0; i < numCountries; i++) 
        { 
         printf("%s\n", ctryList[ i ].countryName); 
        } 
    } 
    
  • 関連する問題