2016-10-15 38 views
0

該程序的主要目標是顯示數組內的值,名爲「channels」,但我似乎無法獲得任何內容顯示。顯示屏顯示後,我需要提示用戶選擇四個通道中的一個並顯示所選的「通道」值。這是我迄今爲止。我也不能使用任何循環。請幫忙。如何顯示數組的內容並提示用戶進行選擇{C}

#include <stdio.h> 

//我使用的是結構到所有的值存儲陣列

typedef struct 

{ 
    char* name; 
    double n; //roughness 
    double slope; 
    double width; 
    double depth; 

} CHANNEL; 


main() 
{ 

    CHANNEL channels [4] = { 
    {"Channel1", 0.035, 0.0001, 10.0, 2.0}, 
    {"Channel2", 0.020, 0.0002, 8.0, 1.0}, 
    {"Channel3", 0.015, 0.0010, 20.0, 1.5}, 
    {"Channel4", 0.030, 0.0007, 24.0, 3.0} 
    }; 

內//我想在這裏顯示所有頻道和他們的價值觀......我知道我必須使用printf,但我需要使用指針嗎?

printf("Please note:\n 0 = Channel 1 \n 1 = Channel 2 \n 2 = Channel 3 \n 3 = Channel 4); 

//這部分只是針對所選擇的通道

printf(Pick a channel from 0-3\n"); 
    int c = 0; 
    scanf("%i", &c); 
    CHANNEL chosen = channels [c]; 

} 
+1

您不打印任何內容,或要求輸入任何用戶信息。此代碼甚至不會編譯。你真的嘗試過什麼? – UnholySheep

+0

爲什麼你不能使用循環?這似乎是自然的做法。 – usr2564301

+0

你的struct的'name'成員只有一個'char'。 –

回答

0

首先,進行Daniel Litvak建議的更改。然後,從用戶那裏得到的信息,你應該這樣做:

int main(void) { 

    // ... 
    printf("Pick a channel from 0-3\n"); 
    int c = 0; 
    scanf("%i ", &c); 

    CHANNEL chosen = channels[c]; 

    printf ("The channel chosen is %s, n = %f, slope = %f and the depth = %f", chosen.name, chosen.n, chosen.slope, chosen.depth); 
} 

這將提示用於索引數組中表示該通道的指數用戶。如果您願意,您還可以首先打印出所有的頻道選項。

爲了演示目的,我將所選頻道留在變量chosen中,您可以按需要繼續。

編輯:沒有進行錯誤檢查以確保c在範圍內。這是爲了避免顯示任何額外的,令人困惑的代碼。

+0

如何打印出所選通道的值: – aaa

+0

printf(「所選通道爲%s,n =%d,斜率=%d,深度=%d」); – aaa

+0

@AjAlmero:我已經更新了我的答案。另外,請注意,用'%f'完成'double'打印。 – Charles

0

的問題可能是你在一個地方一個字符的字符串,一個簡單的解決辦法是改變結構來:

typedef struct{ 
char* name; 
double n; 
double slope; 
double depth; 
} CHANNEL;