while循环用于将用户输入表扫描到数组中

时间:2016-02-17 19:18:17

标签: c

我是一名超级不直观的初学者编程学生,从事家庭作业。该计划是采取曲棍球队和游戏统计数据的输入表,计算“点数”,然后输出以不同方式组织的表格,其中包括点和游戏的附加列。

到目前为止,我还没有通过扫描输入。使用while循环,我首先要确定字符串是会议名称还是团队名称,然后相应地扫描并存储后续表值。我只是试图在此时打印出相同的表格,但是当我复制/粘贴给定的输入输入表时,我没有输出,当我手动输入时,它就会变得非常奇怪。 / p>

输入表如下所示:

Conference1_Name 3(参加会议的团队)

Team_1_Name 31 15 2 2(胜利,亏损,损失,枪战损失)

Team_2_Name 24 21 1 0

Team_3_Name 27 19 0 2

Conference2_Name 4

Team_4_Name 30 15 1 1

...

aaand到目前为止这是我的代码......

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

int main(void){
    int n=0, N=25, nE=0, nW=0, i, w[n], l[n], otl[n], sol[n], gp[n], pts[n];
    char input[30], team[n][30];

    printf("Enter conference data\n");
    setvbuf(stdout,NULL,_IONBF,0);

    while(n<=N){
        scanf("%s", input);
        if (0==strcmp(input, "Eastern_Conference")) {
            scanf("%d", &nE);
            continue; //does not count eastern conference as a team, but stores number of teams, and does not increment n
        }

        if (0==strcmp(input, "Western_Conference")) {
            scanf("%d", &nW);
            continue; //does not count western conference as a team, but stores number of teams, and does not increment n
        }

        if (0==strcmp(input, "EOD")) break; 

    //originally i had a statement like: if (nE>0 && nW>0){N=nE+nW;) so that the loop condition n<N will break the loop on it's own but thought maybe EOD would simplify it and fix my problem//


        strcpy(team[n], input);
        scanf("%d%d%d%d", &w[n], &l[n], &otl[n], &sol[n]);

        gp[n]=w[n]+l[n];
        pts[n]=(2*w[n])+otl[n]+sol[n];

        n++;

    }   //so we have input the data and calculated points

    printf("\n%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "WHL", "GP", "W", "L", "OTL", "SL", "PTS");
    for(i=0; i<n; i++){
        printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\n", team[i], gp[i], w[i], l[i], otl[i], sol[i], pts[i]);
    }

    return EXIT_SUCCESS;
}

0 个答案:

没有答案