我应该在什么时候使用scanf,地址为'&';安培钥匙?

我应该在什么时候使用scanf,地址为'&';安培钥匙?,c,arrays,function,struct,C,Arrays,Function,Struct,我必须读入一个有名字和数字的文本文件。这些名字代表虚拟选举中的候选人(总共7人),数字代表选民。如果选民号码不在7名候选人的范围内,它将被抛出,但仍被存储。最后,我必须打印出谁赢得选举的结果以及有多少张被破坏的选票 这是我的文本文件: Robert Bloom John Brown Michelle Dawn Michael Hall Sean O’Rielly Arthur Smith Carl White 3 8 1 3 1 6 12 9 6 5 0 2 8 4 6 6 8

我必须读入一个有名字和数字的文本文件。这些名字代表虚拟选举中的候选人(总共7人),数字代表选民。如果选民号码不在7名候选人的范围内,它将被抛出,但仍被存储。最后,我必须打印出谁赢得选举的结果以及有多少张被破坏的选票

这是我的文本文件:

Robert Bloom 
John Brown 
Michelle Dawn 
Michael Hall 
Sean O’Rielly 
Arthur Smith 
Carl White 

3 8 1 3 1 6 12 9 6 5 0 2 8 4 
6 6 8 3 2 8 0 12 6 1 8 3 2 2 
3 2 5 7 4 11 8 6 11 12 11 7 5 5 
8 9 10 12 1 3 12 12 9 11 7 9 3 1 
2 10 12 7 11 9 6 6 0 1 10 7 11 2 
8   0 12 8 10 11 2 2 8 4 2 12 3 2 
9   1 4 8 8 7 7 4 12 2 10 10 9 4 
12 9 3 12 0 4 8 0 6 5 9 0 5 3 
11  6   0   3   0 
这就是我被困在如何正确扫描这些文件的地方

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

FILE * data;
int spoilt=0;

typedef struct
{
 int votes;
 char name[20];
}candidates;

void initialize( candidates *electionCandidates, FILE *data )
{
    int i;
    for( i=0; i<7; i++ )
    {
        fscanf( data, "%[^\n]%*c", electionCandidates[i].name );
        printf( "%s\n", electionCandidates[i].name );
        electionCandidates[i].votes=0;
    }

}

int processVotes( candidates *electionCandidates, FILE *data )
{
    int i;                                           //tallying votes
    int voter;
    for ( i = 0; i< 365; i++ )
    {
       fscanf( data, "%d", voter );
       if ( voter <= 7&& voter > 0 )
        electionCandidates[voter-1].votes++;
       else
        spoilt++;
    }

                                                    //catcher to grab winner
    int maxValue, winner=0;

    maxValue = electionCandidates[0].votes;
    for( i = 0; i < 7; i++ )
    {
        if( maxValue < electionCandidates[i].votes )
        {
            maxValue = electionCandidates[i].votes;
            electionCandidates[winner] = electionCandidates[i];
        }

    }

    return electionCandidates[winner], maxValue;


}

void printResults( candidates *electionCandidates )
{
    printf("%s won the election with a total of %d votes.\n There was a total of %d spoilt"
            electionCandidates[winner].name, maxValue, spoilt);

}


int main() {
    data = fopen( "elections.txt","r" );
    candidates electionCandidates[7];

    initialize( electionCandidates, data );
    processVotes( electionCandidates, data );
    printResults( electionCandidates );


    fclose( data );
    return 0;
}
#包括
#包括
#包括
文件*数据;
int-spartt=0;
类型定义结构
{
整数票;
字符名[20];
}候选人;
无效初始化(候选项*选举候选项,文件*数据)
{
int i;

对于(i=0;i使用
scanf
时,必须提供要将结果扫描到的变量的地址。使用
运算符提供地址。此外,最好检查
scanf
的结果,以确保它成功扫描了您要求的内容。
scanf
将始终返回e成功扫描的元素数,除非发生I/O错误,在这种情况下,它将返回负数

以下是您的程序的固定、带注释的版本:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
 int votes;
 char name[20];
}candidates;

// specify a new type to hold the election result data
typedef struct
{
  int winner;
  int maxVotes;
  int spoilt;
} electionResult;

void initialize( candidates *electionCandidates, FILE *data )
{
    int i;
    for( i=0; i<7; i++ )
    {
        fscanf( data, "%[^\n]%*c", electionCandidates[i].name );
        printf( "%s\n", electionCandidates[i].name );
        electionCandidates[i].votes=0;
    }

}

// This function can now return more than one value, because we've wrapped
// the relevant info into a structure called "electionResult"
electionResult processVotes( candidates *electionCandidates, FILE *data )
{
    // declare the election result struct here (which we fill with data)
    // we initially set all values to 0

    electionResult er = {0, 0, 0};
    int i;                                           //tallying votes
    int voter;
    for ( i = 0; i< 365; i++ )
    {
       // scan the vote by providing the address of voter (using &)
       int result = fscanf( data, "%d", &voter );
       if (result == 1)
       {
          if ( voter <= 7&& voter > 0 )
             electionCandidates[voter-1].votes++;
          else
             er.spoilt++;
       }
    }

    er.maxVotes = electionCandidates[0].votes;
    for( i = 0; i < 7; i++ )
    {
        if( er.maxVotes < electionCandidates[i].votes )
        {
            // update the values in the election result struct
            er.maxVotes = electionCandidates[i].votes;
            er.winner = i;
        }
    }

    return er;
}

// this function now prints the result of the election by accepting an "electionResult" struct
void printResults( candidates *electionCandidates, electionResult er )
{
    printf("%s won the election with a total of %d votes.\n There was a total of %d spoilt",
            electionCandidates[er.winner].name, er.maxVotes, er.spoilt);

}


int main() {
    FILE *data = fopen( "elections.txt","r" );
    candidates electionCandidates[7];
    electionResult er;

    initialize( electionCandidates, data );
    er = processVotes( electionCandidates, data );
    printResults( electionCandidates, er );


    fclose( data );
    return 0;
}
#包括
#包括
#包括
类型定义结构
{
整数票;
字符名[20];
}候选人;
//指定新类型以保存选举结果数据
类型定义结构
{
int优胜者;
整数最大投票数;
被宠坏的;
}选举结果;
无效初始化(候选项*选举候选项,文件*数据)
{
int i;

对于(i=0;iIs它甚至可以编译吗?你用这个
返回选举候选人[winner]做什么,maxValue;
?您是否试图返回2个值?我想这就是我搞砸的地方。我试图找到选举的获胜者,并返回结构数组中位置的值只需返回获胜者的索引。这样,您就可以直接从数组中访问名称和投票数
void initialize(候选* *,考生*,文件*)<编译> >你使用C++编译器吗?@ WielpLase:为什么不编译?用C++编译(用CLAN)。即使我尝试使用这样的GETFEL循环:GETCHARE(),也不能让它编译:返回0;{桌面\MikEdtudio\MaFiFiel.Win [构建错误][theElection.o]错误1@user3498869:一定有比这更具描述性的错误…它对我来说编译得很好(我使用的是clang而不是dev-c++,但我启用了所有警告和迂腐模式,但仍然没有收到任何错误)。你确定你的源代码符合上述要求吗?哇,我又回到我的问题上来了,其中一些问题我问得很尴尬。@Fappycow从不觉得尴尬,而是把它作为你学到多少的参考点。