标题:字符串对比问题,大神们帮忙查查错 谢谢了
只看楼主
yuanda199312
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2018-4-12
结帖率:50%
已结贴  问题点数:20 回复次数:1 
字符串对比问题,大神们帮忙查查错 谢谢了
// Assignment2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>



int main()
{
    typedef struct
    {
        char TeamName[16];
        char Group[2];
        int GamesPlayed;
        int GamesWon;
        int GamesDrawn;
        int GamesLost;
        int GoalsFor;
        int GoalAgainst;
        int Points;
    }team_t;

    team_t record[32];

    FILE    *stream, *stream2;     // variable name
    char teamname[32][16];
    char group[32][2];
    char team1[16];
    char team2[16];
    int score1;
    int score2;
    int i=0;


    errno_t    err;
    err = fopen_s(&stream, "C:\\Users\\User\\Desktop\\2018\\engg1003\\c3270010PI2\\c3270010PI2\\team.dat", "r"); //oprn the read file and check the file open or not
    if (err == 0)
    {
        printf("The    file    team.dat    was    opened\n");
    }
    else
    {
        printf("The    file    team.dat    wasn’t    opened\n");
    }

   
    while (!feof(stream))
    {
        fscanf_s(stream, "%s  %s", record[i].TeamName,16,record[i].Group,2); //scan and print the value of teamname and group from read file
        printf_s("%s %s\n", record[i].TeamName, record[i].Group);
        i++;
        
        
        
    }
   
   

    if (stream)
    {
        err = fclose(stream);      
        if (err == 0)
        {
            printf("The file team.dat was closed\n");
        }
        else
        {
            printf("The file team.dat wasn’t closed\n");
        }
    }
   
   
    err = fopen_s(&stream2, "C:\\Users\\User\\Desktop\\2018\\engg1003\\c3270010PI2\\c3270010PI2\\match.dat", "r");    //oprn the read file and check the file open or not
    if (err == 0)
    {
        printf("The    file    match.dat    was    opened\n");
    }
    else
    {
        printf("The    file    match.dat    wasn’t    opened\n");
    }

    while (!feof(stream2))
    {
        fscanf_s(stream2, "%s %d %s %d\n", team1, 16, &score1, team2, 16, &score2); //scan and print the value of teamname and group from read file
        //printf_s("%s %d %s %d\n", team1, score1, team2, score2);
        
        if (score1 < 0 || score2 < 0)
        {
            printf_s("%s %d %s %d is invaild\n", team1, score1, team2, score2);

        };

        if (strcmp(team1, record[i].TeamName) != 0)
        {
            printf_s("the name is not in teamlist\n");
            i++;
        }
        



    }

当我查询team1里的字符串是否存在于结构体里面的字符串。。。为什么没显示啊。。。明明都存在的啊。
搜索更多相关主题的帖子: the int FILE dat and 
2018-05-17 07:56
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
得分:20 
程序代码:
        if (strcmp(team1, record[i].TeamName) != 0)
        {
            printf_s("the name is not in teamlist\n");
            i++;
        }


i 没有重置
你这里比较
i要从0开始

https://zh.
2018-05-17 09:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-486999-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035650 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved