C 尝试编写从用户处获取名称的函数

C 尝试编写从用户处获取名称的函数,c,C,我试着写一个函数,得到10到60000之间的候选数, 并为每个候选人取一个名字。。。 这是我写的: /********** Headers **********/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> /********** Consts **********/ const number_candidates; /********

我试着写一个函数,得到10到60000之间的候选数, 并为每个候选人取一个名字。。。 这是我写的:

/********** Headers **********/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
/********** Consts **********/
const number_candidates;

/********** Functions **********/
void get_candidates(char *arr[256][256])
{
    int counter = 1, i = 0, j =0;
    int temp = 0;

    printf ("Please enter the number of candidates: \n");
    scanf ("%d", &temp);
    while (temp < 10 && temp > 60000)
    {
        printf ("Please enter the number of candidates: \n");
        scanf ("%d", temp);
    }
    const number_candidates = temp;
    while (counter < number_candidates + 1)
    {
        printf ("Please enter the %d name: \n", counter);
        fgets (arr, 256, stdin);
        counter++;
    }
}

int main(int argc, char *argv[])
{
    int i = 0;
    char can_names[256][256];

    get_candidates(&can_names);

    system("PAUSE");
    return 0;
}
/*******标题**********/
#包括
#包括
#包括
#包括
/**********常数**********/
const number_候选人;
/**********功能**********/
void get_候选者(char*arr[256][256])
{
int计数器=1,i=0,j=0;
内部温度=0;
printf(“请输入候选人数:\n”);
scanf(“%d”、&temp);
而(温度<10和温度>60000)
{
printf(“请输入候选人数:\n”);
扫描频率(“%d”,温度);
}
const number_候选者=临时;
while(计数器<候选数量+1)
{
printf(“请输入%d名称:\n”,计数器);
fgets(arr,256,标准DIN);
计数器++;
}
}
int main(int argc,char*argv[])
{
int i=0;
字符可以存储名称[256][256];
获取\u候选人(&can\u姓名);
系统(“暂停”);
返回0;
}
将名称放入arr中时出错。

请查看哪一项指示变量需要作为指针传递,就像第一次调用scanf时一样。然后看看你第二次打给scanf的电话

您当前只能一遍又一遍地为数组中的第一个字符串指定名称。看看while循环,尤其是如何传入'arr'变量。寻找一些灵感

要打印出需要在数组上循环的所有名称。您可以找到一些打印字符串和字符串列表的示例。

您应该调用:
计数器=0
..
fgets(arr[计数器],256,标准输入)


您需要为每个循环走一步。

您应该避免使用像这样的参数:
char*arr[256][256]
。。。这有什么意义?你应该考虑一下你的功能会做什么。你想让它载入候选人的名字,对吗?因此,您可以使用属性
name
定义struct candidate

typedef struct candidate{
    char name[256];
} Candidate;
另一件事:为什么要将数组的地址传递给此函数?您只希望数组中填充数据,而不需要处理数组本身,因此只需要传递数组,而不需要传递数组的地址

然后,可以将函数的原型更改为
void get_candidates(Candidate*candidates)
,这样更容易阅读。看看这个函数的使用会变得多么简单:

Candidate candidates[256];
get_candidates(candidates);
最后一件事:在编写这样的函数之前,请先尝试一些更简单的方法(看看那里发生了什么)。 下面是一个例子:

#include <stdio.h>

typedef struct candidate{
    char name[256];
} Candidate;

void get_candidates(Candidate* candidates){
    scanf("%255s", candidates[4].name);
}

int main(int argc, char *argv[]){
    Candidate candidates[256];
    get_candidates(candidates);
    printf("%s\n", candidates[4].name);
    return 0;
}

有几件事是错误的:

首先,您需要为60000个名称分配足够的空间,但您只分配了256个名称。好的,我们换衣服

char can_names[256][256];

然后得到。。。可能是分段错误。这是因为数组使用了太多的堆栈空间。换成

static char can_names[60000][256];
所以它不在堆栈上


其次,不需要获取数组的地址——它已经作为指针传递了。您的函数调用更改为

get_candidates(can_names);
函数签名是

void get_candidates(char arr[60000][256])

第三,您需要一个循环来一次读取一个条目。
for
循环更易于阅读:

for (counter = 0; counter < number_candidates; counter++)
{
    printf ("Please enter the %d name: \n", counter);
    fgets (arr[counter], 256, stdin);
}

不知道这是否会有帮助,但看看下面的代码。 它是动态工作的,即为所需数量的候选对象分配内存,而不是假设其中60000个

/*
 * Write a function, that get a number of candidates betwen 10 to 60,000, and gets a     name for each candidate 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256

int main(int argc, char*argv[]){

int i,n;
char **s;

printf("Enter the total number of candidates\n");
scanf("%d",&n);

//error condition
if(n<10 || n>60000){        
    printf("Sorry number of candidates should be between 10 to 60,000\n");
    return -1;      
}

//allocate memory
s = malloc(sizeof(char*)*n);

//get the data
for(i=0;i<n;i++){

    s[i] = calloc(MAX,sizeof(char));
    printf("Enter the candidate number %d's name:\n",i+1);
    //fgets(s[i],MAX,stdin);
    scanf("%s",s[i]);

}

//Display the data
printf("\nDetails of all the Candidates\n\n");
for(i=0;i<n;i++){
    printf("Candidate number %d's name:%s\n",i+1,s[i]);
}

//Free the memory
for(i=0;i<n;i++){
    free(s[i]);
}
free(s);

return 0;
/*
*编写一个函数,得到10到60000之间的候选数,并得到每个候选数的名称
*/
#包括
#包括
#包括
#定义最大值256
int main(int argc,char*argv[]){
inti,n;
字符**s;
printf(“输入候选人的总数\n”);
scanf(“%d”和“&n”);
//错误条件
如果(n60000){
printf(“对不起,候选人数应该在10到60000之间\n”);
返回-1;
}
//分配内存
s=malloc(sizeof(char*)*n);
//获取数据

对于(i=0;i
while(temp<10&&temp>60000)
错误到底是什么?是编译时还是运行时?如果是运行时,你能给出一个测试用例吗?这个“错误”是一个bug,还是来自操作系统的实际错误(即segfault)?真的吗?你打算如何测试这段代码:)?没有错误,但它没有做它支持做的事情…在我主要调用函数后,我写了:printf(“%s”,can_names);它只向我打印我输入的姓氏,而不是X名…以及…为什么arr声明为256个“字符串”?要填充60000-10个名称(每个名称256字节),arr必须是:arr[59990][256]结构是一个很好的主意!但是当我不知道需要制作多少结构时,我如何才能制作两个结构呢?还有,scanf中的%255s是什么?(顺便说一句,我们的老师不允许我们使用scanf作为字符串,我们只能使用fgets)…@AmitSegal:我已经更新了我的答案。
%255s
指定此字符串的宽度,使其最大长度为255个字符。@AmitSegal:使用
fgets(候选者[4].name,255,stdin);
而不是
scanf(“%255s”,候选者[4].name);
那么为什么选择[4]?还有,如果我不知道我会有多少候选人,我该怎么做?@AmitSegal:这只是一个例子,展示了一些想法和解决问题的不同方法。你要完成这项作业:)非常感谢你的评论!几条注释:*第四个条件是| |,因为当我把它从&&改为| |时,它确实存在我的工作不好(我想和你一样,但不知道为什么会这样)…*第五个-我还有更多的东西要添加,我需要这些标题…:)如果你像这里提到的那样修复你的
scanf
调用,它应该可以工作。你甚至可以删除第一次读取的候选数量。由于你将
temp
初始化为0,循环将至少运行一次。非常感谢你的评论!我也有同样的问题我使用fgets,解决方案是放置getchar()…它并不总是有效的,但是当它是时,它非常棒:)我有几个问题:
for (counter = 0; counter < number_candidates; counter++)
{
    printf ("Please enter the %d name: \n", counter);
    fgets (arr[counter], 256, stdin);
}
while (temp < 10 && temp > 60000)
while (temp < 10 || temp > 60000)
scanf ("%d", &temp);
/*
 * Write a function, that get a number of candidates betwen 10 to 60,000, and gets a     name for each candidate 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256

int main(int argc, char*argv[]){

int i,n;
char **s;

printf("Enter the total number of candidates\n");
scanf("%d",&n);

//error condition
if(n<10 || n>60000){        
    printf("Sorry number of candidates should be between 10 to 60,000\n");
    return -1;      
}

//allocate memory
s = malloc(sizeof(char*)*n);

//get the data
for(i=0;i<n;i++){

    s[i] = calloc(MAX,sizeof(char));
    printf("Enter the candidate number %d's name:\n",i+1);
    //fgets(s[i],MAX,stdin);
    scanf("%s",s[i]);

}

//Display the data
printf("\nDetails of all the Candidates\n\n");
for(i=0;i<n;i++){
    printf("Candidate number %d's name:%s\n",i+1,s[i]);
}

//Free the memory
for(i=0;i<n;i++){
    free(s[i]);
}
free(s);

return 0;