I got an assignment to make program that asks for a number of teams, their names, wins and losses. I have got two problems with the code.
- How do I get the team names with spaces in them correctly?
- And how to sort the teams by total value (total=wins-losses) before printing?
#include <stdio.h>
#include <string.h>
#define NAME_LENGTH 25
typedef struct {
char name[NAME_LENGTH];
int wins;
int losses;
int total;
} Total;
Total readTeamInfo(void);
void printInfo(Total tt, int);
int main(void) {
int i, teams;
printf("Number of teams > ");
scanf("%d", &teams);
Total data[teams];
for (i = 0; i < teams; i++) {
data[i] = readTeamInfo();
data[i].total = data[i].wins - data[i].losses;
}
for (i = 0; i < teams; i++) {
printInfo(data[i], i);
}
return (0);
}
Total readTeamInfo(void) {
Total tt;
printf("Team name > ");
scanf("%s", tt.name);
printf("Wins > ");
scanf("%d", &tt.wins);
printf("Losses > ");
scanf("%d", &tt.losses);
return (tt);
}
void printInfo(Total tt, int i) {
printf("Team #%d %s: %d wins and %d loses\n", i + 1, tt.name, tt.wins, tt.losses);
}