2015-11-07 200 views
2

我使用visualstudio 2015在C中製作控制檯應用程序,以便用戶可以輸入他們希望製作的糖果數量和甜點的名稱,但是存在問題,問題是,當程序試圖從數組中讀取一個字符串時,程序崩潰並且不會打印出任何內容,但是如果將其更改爲使用%c打印單個字符,它將打印出字符串的第一個字符,例如,如果我輸入2個糖果和字符串'Jawbreaker'和'Lollipop'如果我使用%s作爲字符串,它會崩潰,但是如果我使用%c作爲字符,它將完成它的工作並分別在不同的字符上打印'J'和'L'線,任何想法如何能得到這與%s說明符的字符串工作?C語言打印循環中的數組字符串

的代碼如下:

#include <stdio.h> 
/*Declares the variable needed*/ 
int sweet_number; 
char sweet_name[999]; 


int main(void) { 

/*Prompts the user to enter the number of sweets and saves it to sweet_number*/ 
printf("Please enter the number of sweets:\n"); 
scanf("%d", &sweet_number); 

/*for loop to enter the name of the sweet into the array*/ 
for (int i = 0; sweet_number > i; i++) {     
    printf("What is the name of the sweet?\n"); 
    scanf("%s", &sweet_name[i]); 
    } 

/*Prints array to screen*/ 
for (int j = 0; sweet_number > j; j++){ /* <- This is where code fails to run*/ 
    printf("%s\n", sweet_name[j]); 
} 

return 0; 

} 
+0

你想要甜的名字的第一個字符或全名嗎? – Haris

回答

3

你必須使用一個2維陣列。 sweet_name是一個數組(1-D),每個索引最多可以存儲一個字符而不是一個字符串。下面一行

char sweet_name[999]; 

更改爲

char sweet_name[999][100]; 
+0

非常感謝您解決我的問題!這正是我想要的:D – Henry

+0

我很高興它可以幫助你。 :) – BeingMIAkashs

+0

@Henry不要忘記將'scanf(「%s」,&sweet_name [i]);''改爲'scanf(「%s」,sweet_name [i]);' –

1

OK,我會建議你做一些更有效的,並且是使用雙指針。通過這樣做,您可以解決2D陣列版本中存在的一些問題。
第一個問題是,如果用戶想要插入超過999個糖果,你會怎麼做。你的數組不能容納它們。
其次,如果用戶輸入大於100個字符的名稱,你會怎麼做。再一次,你的二維數組不能容納它。而且,儘管用戶可能輸入大於100個字符的名稱,但大多數用戶的輸入會比這個少得多,現在對於每個字符串,當您可能只需要大約50個時,就會分配100個位置。
因此,讓我們來處理這些問題。 我可能會做這樣的事情:

#include <stdio.h> 
#include <string.h> // for strcpy(), strlen() 
#include <stdlib.h> // for malloc() 

int main(void) {  

char **sweet_names; // make a double pointer(or you might see that as array of pointers 
char reader[300]; // this variable will help us read every name into the sweet_names 
int sweet_number; 
int i, j; 

// Your code here to get the number of sweet_names 
/*Prompts the user to enter the number of sweets and saves it to sweet_number*/ 
printf("Please enter the number of sweets:\n"); 
scanf("%d", &sweet_number); 

// Now that you know the sweet_number, allocate as much memory as you need. 
// And that can be more than 999 sweet names 
sweet_names = (char **) malloc(sweet_number * sizeof(char *)); 
// i.e. make a character pointer to sweet_number character pointers. 

// Again, some of your code here 
for (i = 0; sweet_number > i; i++) {     
    printf("What is the name of the sweet?\n"); 
    scanf("%s", reader); // read into the reader 
    sweet_names[i] = (char *) malloc(strlen(reader) + 1); // allocate as much memory as you need for the current string, the one just read into reader 
    strcpy(sweet_names[i], reader); // copy contents of reader to sweet_names[i] 
} 

// Again, your code to print the names 
for (j = 0; sweet_number > j; j++){ 
    printf("%s\n", sweet_names[j]); 
    free(sweet_names[j]); // free every string you allocated, it is good practice 
} 

// Finally, free the sweet_names pointers. Generally, when you allocate 
// dynamically, which is what we do with malloc(), it is a good practice 
// to free what you allocate becuase otherwise stays in memory and then 
// memory leaks are created. There is a lot to say about C and memory 
// but anyway, remember to free 
free(sweet_names); 

return 0; 

} 

最後,現在程序再次有限制只讀名最多,因爲reader 300個字符,但這個東西,你也可以處理,而這是爲讀者分配一個瘋狂的內存量,如1000000 個字符,然後釋放它。

+0

感謝您提供的信息,有點超出了我的項目範圍,但我將其保存以供將來參考! – Henry

+0

歡迎您!類似的字符串操作方法,在C中是一個很大的概念,你會經常看到。對於您的項目,BeingMIAkashs的答案可能更好,因爲它的簡單性,但我認爲在字符串操作中顯示一些替代方案是個好主意。 – zuko32