2013-03-21 69 views
-1

我正在努力與結構,並有幾個問題,我不知道該怎麼做。 首先,我不得不定義一個結構調用部分包含int變量part_number,我和一個25個字符的字符數組。 第二我必須定義部分是結構部分的同義詞。 將part_number和part_name從鍵盤讀入變量a的各個成員。結構,並定義一個變量作爲同義詞

include <stdio.h> 

int main(void) 
{ 
struct part_containg 

{ 
    int  part_number,i; 
char part_name[25] !='\0'; 
    // set up array of 25 characters and include terminating null character 
}; 

struct part_containg part_number,part_number[i]; 

for(part_number=0; part_number<25;++part_number) // part_number goes up 1 for each part_name 
{ 
    printf("Intersert Part Name"\n); 
    scanf("%c", &part_name[i]);   // scans for part_name 
} 
return 0; 
} 

回答

0

你有很多的語法錯誤。你應該參考一本介紹性的教科書:他們真的很有幫助!我已經讀了半打,我對自己的「C技能連續體」評價很低〜

無論如何,我們都是從底層開始的。下面是可替代的形式有很多的評論:

#include <stdio.h> /* you need a hash sign to include libraries...!  */ 
#include <string.h> /* i use this library to copy input rather than loop */ 

int main() {      // main does not take an argument 

    struct part_containing { 
    int part_number;    // what is i for? I removed it 
    char part_name[25];   // do the null later, this is not the time! 
    }; 

    struct part_containing name; // only declare the struct, not its variables 
            // p.s. name is an awful name-choice :P 

    char input[25];     // make a local var for input 
    puts("Enter part name:\n");  // prompt user for input 
    scanf("%s", input);    // capture local var 
    strcpy(name.part_name, input); // copy it into the struct 
    name.part_name[25] = '\0';  // null just to be sure! 

    puts("Enter number:\n");  // same for the number now 
    scanf("%i", &name.part_number); // notice the syntax: &name.part_number...! 

    printf("%s and %i", name.part_name, name.part_number); 
    // simple error check 
    // did we get the expected input? It can be helpful to check in test runs! 

    return 0; 
} 

它沒有回答所有的在分配的問題,但它應該足以讓你開始。

希望這有助於!如果你在工作並且有更多的問題,那就問問!

+0

好吧,我寫了一些代碼,我正在努力與結構和包裹我的頭圍繞它完全,但這是與我的小代碼的問題。 – Pwoods 2013-03-21 02:06:36

+0

@ParkerWoods當然,更新問題,我會盡力幫助。 – d0rmLife 2013-03-21 02:16:17

+0

現在應該更好,謝謝! – Pwoods 2013-03-21 02:30:40