Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array troubles : (

Status
Not open for further replies.

SuperSharp

Programmer
Mar 14, 2005
9
GB
Hello!
I have been given an assignment to create a league table in C. Basically a series of match results are to be entered by the user and an updated league table is to be produced. Which looks something like this:

P W D L F A T
Galatasaray 2 1 1 0 4 3 4
Juventus 2 0 2 0 3 3 2
Rosenborg 2 0 2 0 2 2 2
Atlentico 2 0 1 1 2 3 1

I am uber new to this whole programming thing, my teacher here at uni is terrible, and I am really confused. I have spent hours trying to figure this out but so far all I have come up with is the main menus etc (which i am dead proud of!!!). I have also made an array for the team names which I can now add one at a time, and I figured that I have to make a 2d array for the scores part. I have given this a go, but I do not know how to display only the amount thats currently in the array and not the blank spaces after. At this point we don't have to worry about sorting them in any particular order, just trying to get it to display ^like that is hard enough.
Does anyone have any ideas how this could be done, and how I could display this table. Any help would be really appreciated as I am going nuts and have my first demo soon
Thanks very much for taking the time to read this.

Edd

P.S: The code below is what I have done so far for 2 of the fuctions, in the 'print_teams' function, I thought about trying to get it to read the length of the array and only diaplaying how many teams are entered, as right now it prints the teams and lots of black spaces and what ever was last in the memory location.
Code:
/**********************************************************/
/*                    Stores a new team                   */
/**********************************************************/
void read_teams(char s[][21], int n)
{
   static int t;
   printf("\nPlease enter a new team\n");
   {
	   gets(s[t]);
	   t++;
   }
}
/**********************************************************/
/*                  Displays the current teams            */
/**********************************************************/
void print_teams(char list[][21], int n)
{
	int i = 0;
	printf("\nThe current Teams are:\n\n");
	while (i != 11)
	{
	  //print each row list [i] as string
		printf("%s\n", list[i]);
		i++;
	}
}
 
When you read in the array , you can terminate the last character by an NULL ('\0');

Sample code can be

for (x=0;x<MAX_ARR_SIZE;x++)
{
/* check if it is anumber or a character */
if (list[x]<48 || list[x]>90)
list[x]='\0';

}


you can check for numbers or alphabets separately too.
That should stop printf from displaying any character after that.
 
Thanks for replying to me, I have spent a while trying to implement your suggestion, without much luck. These are the errors I am getting:

> cc HL5.c
cc: Warning: HL5.c, line 88: In this statement, "48" of type "int", is being con
verted to "pointer to char". (cvtdiftypes)
if(list[x]<48 || list[x]>90)
--------------^
cc: Warning: HL5.c, line 88: In this statement, "90" of type "int", is being con
verted to "pointer to char". (cvtdiftypes)
if(list[x]<48 || list[x]>90)
----------------------------^
cc: Error: HL5.c, line 89: In this statement, "list[x]" is not an lvalue, but oc
curs in a context that requires one. (needlvalue)
list[x]='\0';
-----------^

I tried to implement it by itself and with the loop:

Code:
       int i = 0;
        printf("\nThe current Teams are:\n\n");
        while (i != 11)
        {
        for (x=0;x<12;x++)
        {
          //checks if it is a number or a char
           if(list[x]<48 || list[x]>90)
           list[x]='\0';
           printf("&s\n", list[i]);
           i++;
        }
        }

Any suggestions?

Edd
 
Maybe this would do you better:
Code:
#define MAX_TEAMS 10
#define MAX_TEAM_NAME_LEN 100

int main(void)
{
  int CurrentCount = 0;
  char list[MAX_TEAMS][MAX_TEAM_NAME_LEN];
}
In this code, you know the max size of the array is 10 teams, and the current number of teams in the array is 0. At some point in another function, you can the do something like:
Code:
for (i = 0; i < CurrentCount; i++)
{
  printf....
}
Just increment the counter everytime you add a new team name, up to MAX_TEAMS.

Avoid using gets() to read from the keyboard, instead use fgets(). Used properly, this will ensure that the user does overfill the buffer and force the end of one team name to overwrite the beginning of the next array element.

If you really do want to check for what is or isn't a character, use the proper functions, not ASCII decimal values (
 

SuperSharp,
Here is some sample code , I don't know whether it will meet with your teacher's approval or not.
Hammer is right about the use of proper library functions but while you are learning it is not bad to experiment a little.
You will learn more about buffers , overflow etc only when you point to the wrong place or use more characters.
Library functions take out all the fun.


#include <stdio.h>

#define MAX_TEAMS 5
#define MAX_CHARS_IN_TEAM 50


char team_info[MAX_TEAMS][MAX_CHARS_IN_TEAM];



GetTeamInfo(char * team )
{
scanf("%s",team);

}


PrintTeamInfo(char * team )
{
unsigned int x;
/* for (x=0;x<MAX_CHARS_IN_TEAM;x++)
{
if (*(team+x)<48 || *(team+x)>90)
*(team+x)='\0';
}*/
printf("%s \n",team);

}



void main (void)

{
unsigned int x;

printf (" enter the name of teams \n");
for (x=0;x<MAX_TEAMS;x++)
GetTeamInfo(&team_info[x][0]);

printf (" printing the name of teams \n");
for (x=0;x<MAX_TEAMS;x++)
PrintTeamInfo(&team_info[x][0]);

getchar();

}

The output will look like this
enter the name of teams
WESTBROM
CHELSEA
MAN_U
ARSENAL
NEWCASTLE
printing the name of teams
WESTBROM
CHELSEA
MAN
ARSENAL
NEWCASTLE
Press any key to continue

If you take out the commented code in the printTeamInfo function it will get rid of all the spurious characters for you (even the spaces) . You can check for spaces (decimal val 32) if you want to keep them.
 
Thank you both for your help with this, I have just about got it working now. Its starting to make a bit more sense. I should be able to finish the rest off now and get it all done in time, thanks again for your help.

Edd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top