2015-11-07 202 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