1

I want to make a football group with points and goals. And the name of each group must be a capital letter, so i use isupper function to check that. But it seems that my loop is wrong in somewhere. Here is my code

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
struct Team{
    char TeamName[20];
    int Point;
    int Goals;
};
typedef struct TeamCup {
    char GroupID;
    struct Team team[4];
}Group;
void AddNgroup(Group g[], int *index){
    int number = 0;
    printf("Enter the numbers of group you want to add: ");
    scanf("%d", &number);
    *index += number;
    int a;
    for ( a = *index - number; a < *index ; a++){
        do{
            printf("Enter GroupID: ");
            scanf("%c", &g[a].GroupID); getchar();
        } while ( isupper(g[a].GroupID));
        printf("Enter 4 team: \n");
        
        for (int i = 0; i < 4; i++){
            fflush(stdin);
            printf("\t Team %d", i+1);
            printf("\nEnter Team %d's name: ", i+1);
            fgets(g[a].team[i].TeamName, sizeof(g[a].team[i].TeamName), stdin);
            printf("\nEnter Team %d's Point: ", i+1);
            scanf("%d", &g[a].team[i].Point);
            printf("\nEnter Goals Difference: ");
            scanf("%d", &g[a].team[i].Goals);
        }         
    }
}
int main(){
    Group group[8];
    int *p;
    int number = 0;
    *p = number;
    AddNgroup(group, p);
    return 0;
}

Actually, I'm making a menu about adding, printing and sorting group. Here is just the adding part. The output makes me confused.

Enter the numbers of group you want to add: 2
Enter GroupID: a
Enter GroupID: A
Enter GroupID: A
Enter GroupID: F
Enter GroupID: 

I tried to use

(int)g[a].GroupID < 65 || (int)g[a].GroupID > 90)

but it didn't work too.

NewbieBoy
  • 73
  • 4
  • 1
    You read a number and the newline is left behind. You don’t skip white space before reading a character so you get the newline. Test that inputs worked. Print the value read so you know the computer saw what you thought it would see. – Jonathan Leffler Jan 16 '22 at 15:46
  • NewbieBoy, Hint: What is the value of pointer `p` when `*p = number;` is executed? – chux - Reinstate Monica Jan 16 '22 at 15:48

0 Answers0