Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 更新结构数组中的字段_C_Arrays_Windows_Data Structures - Fatal编程技术网

C 更新结构数组中的字段

C 更新结构数组中的字段,c,arrays,windows,data-structures,C,Arrays,Windows,Data Structures,在这个项目中,我的任务是创建一个排行榜项目。遗憾的是,只有4/6的主要功能按预期工作,即fillplayer、addplayer和displaystats功能。我似乎没有掌握在结构中更新分数以及按降序排序的逻辑 使用updatescore函数,它只识别第一个用户的ID并更新他们的分数,该函数会将其他ID视为不存在 通过playerranking功能,它只对分数进行排序,而不对所述分数的各自持有者进行排序 我做错了什么?我很快就要完成这个项目了 #include <stdio.h> #

在这个项目中,我的任务是创建一个排行榜项目。遗憾的是,只有4/6的主要功能按预期工作,即fillplayer、addplayer和displaystats功能。我似乎没有掌握在结构中更新分数以及按降序排序的逻辑

使用updatescore函数,它只识别第一个用户的ID并更新他们的分数,该函数会将其他ID视为不存在

通过playerranking功能,它只对分数进行排序,而不对所述分数的各自持有者进行排序

我做错了什么?我很快就要完成这个项目了

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

int number,i,key,newscore,a;
struct playerdata
{
  char init[3];
  int id,score;
}playerstats[6];


void welcome ();

void fillplayer()                                    //read player info//
{

    for(int i=0;i<5;i++)
    {
        printf("\nPlease enter your Initials (3 Letters).");
        scanf("%s", &playerstats[i].init);
        printf("\n Please enter your score");
        scanf("%d",&playerstats[i].score);
        playerstats[i].id = ( rand()%2000 ) + 1;
    }
    welcome();
}


void displaystats()                              //Output player info//
{
    printf("\nInitials\tID\t\tScore\t");
    for(int i=0;i<5;i++)
    {
        printf("\n%s\t\t%d\t\t%d",playerstats[i].init,playerstats[i].id,playerstats[i].score);

    }   
    printf("\n\n");
    welcome();
}

void addplayer()                                    //read one player's info//
{
        printf("\n(This may overwrite the player who is at the bottom of the Leaderboard.)" );
        printf("\n\nPlease enter your Initials (3 Letters).");
        scanf("%s", &playerstats[i+4].init);
        printf("\n Please enter your score");
        scanf("%d",&playerstats[i+4].score);
        playerstats[i+4].id = ( rand()%2000 ) + 1;
        welcome();


}

int searchplayer(void)
{
    for (i=0;i<5;i++)
    {
        printf("\nEnter your player ID");
         scanf("%d", &key);

              if (playerstats[i].id==key)
              {
                  return a;
              }
              else
              {
                return  -1;
              }
    }
}   

void updatescore() 
{
   for (i=0;i<5;i++)
   {

         a=searchplayer();
        if (playerstats[i].id==key)
        {
           printf ("\nYour ID is%d", playerstats[i].id);
           printf("\nEnter your new high score");
           scanf("%d",&newscore);
           if (newscore>playerstats[i].score)
           {
               playerstats[i].score=newscore;
               printf("Score successfully changed to %d", newscore);
           }
               else 
               {
                    printf("\nThis score is less than your current highscore, the high score shall not be changed");
               }
        }       

                else
                {
                printf("\nInvalid ID");
                }
                printf("\n\n");



    }   
    welcome(); 
}








void playerranking()
{
    int temp, swapped;
    while(1)
    {
        swapped=0;
        for (i=0; i<5-1; i++)
        {
             if(playerstats[i].score<playerstats[i+1].score)
             {
                 temp = playerstats[i].score;
                 playerstats[i].score = playerstats[i+1].score;
                 playerstats[i+1].score= temp;
                 swapped = 1;
             }
        }
        if(swapped==0)
        {
            break;
        }


    }
    printf("\n\n");
    displaystats();
    welcome();
}


void exitfunct()
{
    while(1)
    {
        break;
    }
}

void filltest()
{
    for(int i=0;i<2;i++)
    {
        printf("\n Please enter your Initials");
        scanf("%s",&playerstats[i].init);
        printf("\n Please enter your score");
        scanf("%d",&playerstats[i].score);
        playerstats[i].id = ( rand()%2000 ) + 1;
    }
    printf("\n\n");
    welcome();
}





void welcome()
{    
    printf("\nWelcome to the leaderboard, challenger!");
    printf("\nPlease select an option:");
    printf("\n1-Add 5 players at a time\n2-Display all stats\n3-Update score\n4-Show player ranking\n5-Add 1 player\n6-Exit Program");
    printf("\n\n");
    scanf("%d",&number);
    switch(number)
    {
        case 1:
        fillplayer();
        break;

        case 2:
        displaystats();
        break;

        case 3:
        updatescore();
        break;

        case 4:
        playerranking();
        break;

        case 5:
        addplayer();
        break;

        case 6:
        exitfunct();
        break;

        case 7:
        filltest();
        break;

        default:
        printf("\nThis option is invalid");

    }


}





int main()
{
    welcome();    
      return 0;
}
#包括
#包括
#包括
#包括
#包括
整数,i,key,newscore,a;
结构播放数据
{
charinit[3];
int-id,分数;
}玩家状态[6];
无效欢迎();
void fillplayer()//读取播放器信息//
{

对于(int i=0;i您应该避免使用全局变量,如
key
。将其作为参数传递:
void updatescore(int key){…}
。修复
searchplayer()
后,使用它来获取要传递的参数

const int NUM_PLAYERS = 5;

int searchplayer(void)
{
    int key;
    printf("\nEnter your player ID");
    if (1 == scanf("%d", &key)) {
        for (int i = 0; i < NUM_PLAYERS; i++) {
            if (playerstats[i].id == key) {
                return i;
            }
         }
         printf("Player ID not found: %d\n", key);
     }
     else {
         puts("Invalid entry.")
     }
     return -1;
}

void updatescore(int key) 
{
    if (key < 0 || key >= NUM_PLAYERS) return;

    int newscore;
    printf ("\nYour ID is %d", playerstats[key].id);
    printf("\nEnter your new high score");
    if (1 == scanf("%d",&newscore) {
        if (newscore > playerstats[key].score) {
            playerstats[key].score = newscore;
            printf("Score successfully changed to %d", newscore);
        }
        else  {
            printf("\nThis score is less than your current highscore, the high score shall not be changed");
        }
    }
    else {
        puts("Invalid entry.")
    }
}

void welcome()
{    
    // Code removed for brevity
        case 3:
        updatescore(searchplayer());
        break;

const int NUM_PLAYERS=5;
int searchplayer(无效)
{
int键;
printf(“\n输入玩家ID”);
如果(1==scanf(“%d”,&key)){
对于(int i=0;i=NUM_玩家)返回;
国际新闻核心;
printf(“\n您的ID是%d”,playerstats[key].ID);
printf(“\n输入新的高分”);
if(1==scanf(“%d”,&newscore){
if(newscore>playerstats[key].score){
playerstats[key].score=newscore;
printf(“分数成功更改为%d”,新闻核心);
}
否则{
printf(“\n此分数低于您当前的高分,高分不得更改”);
}
}
否则{
puts(“无效条目”)
}
}
欢迎光临
{    
//为简洁起见,删除了代码
案例3:
updatescore(searchplayer());
打破

至于
更新核心功能
问题来自
搜索播放器功能
同样,当您搜索时,您不应该在
更新核心功能
中再次使用
。搜索后,您需要在
更新核心功能
中使用此
如果其他

for (i = 0; i < 5; i++)
    {

        int num = searchplayer();
        if (num == 1)
        {
         statement;
        }
        else
       {
        statement;
        }
}

通过对
struct
的一个成员进行排序,它不会自动对其他成员进行排序,您应该自己进行排序。

非常感谢!!!我不敢相信我把代码搞得这么糟,它的工作原理与预期的一样!!@Rekader请放心。不需要感谢。如果它真的帮助您接受答案的话。
int searchplayer(void)
{

printf("\nEnter your player ID");
    scanf("%d", &key);

    for (i = 0; i < 5; i++)
    {
        if (playerstats[i].id == key)
        {
            return 1;
        }
    }

        return  -1;
}
void playerranking()
{
    char inittmp[3];
    int  idtmp;
    int temp, swapped;
    while (1)
    {
        swapped = 0;
        for (i = 0; i < 5 - 1; i++)
        {
            if (playerstats[i].score < playerstats[i + 1].score)
            {
                temp = playerstats[i].score;
                playerstats[i].score = playerstats[i + 1].score;
                playerstats[i + 1].score = temp;

                idtmp = playerstats[i].id;
                playerstats[i].id = playerstats[i + 1].id;
                playerstats[i + 1].id = idtmp;

                strcpy(inittmp , playerstats[i].init);
                strcpy(playerstats[i].init , playerstats[i + 1].init);
                strcpy(playerstats[i + 1].init , inittmp);
                swapped = 1;
            }
        }
        if (swapped == 0)
        {
            break;
        }


    }
    printf("\n\n");
    displaystats();
    welcome();
}