GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Apr-2007, 02:44
s1lang s1lang is offline
New Member
 
Join Date: Mar 2007
Posts: 6
s1lang is on a distinguished road

Inputting text (name) as opposed to an int (ID) - C programming


Here is my problem

I have a league system, however to input a teams score, I select the team via input of an int ID number (int) however I need to enter a the Team name (String) from the array.

I cannot get it to work.

Can someone please help me

Here is the part of the code that does this part:

CPP / C++ / C Code:
int take_team_input()
{
		char team[16];



		int ID = 0;
		int player1 = 0;

		int player2 = 0;

		int score1 = 0;

		int score2 = 0;

		int i;



		printf("\n%2s %-16s\n","ID","Team Name");




		//records home and away team ID and score

		for (i = 0; i < num_teams; ++i)

		{

				strcpy(team, arrdetails[i].team);

				ID = arrdetails[i].ID;

				printf("%2d %-16s\n", ID, team);

		}



				printf("\nInput Home Team ID\n>");

				scanf("%d", &player1);

				printf("\nEnter their Score\n>");

				scanf("%d", &score1);

				printf("\nInput Away Team ID\n>");

				scanf("%d", &player2);

				printf("\nEnter their Score\n>");

				scanf("%d", &score2);



		// points for a draw

		if (score1 == score2)

		{

		arrdetails[player1].played++;

		arrdetails[player2].played++;

		arrdetails[player1].goalsf = arrdetails[player1].goalsf + score1;

		arrdetails[player2].goalsf = arrdetails[player2].goalsf + score2;

		arrdetails[player1].goalsa = arrdetails[player1].goalsa + score2;

		arrdetails[player2].goalsa = arrdetails[player2].goalsa + score1;

		arrdetails[player1].drew++;

		arrdetails[player2].drew++;

		arrdetails[player1].points++;

		arrdetails[player2].points++;

		}

There is a lot more code to this however this is the relevant part to which I am extremely stuck on.

Any help or pointers would be greatly appreciated
Last edited by admin : 05-Apr-2007 at 05:41. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 05-Apr-2007, 08:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Inputting text (name) as opposed to an int (ID) - C programming


Quote:
Originally Posted by s1lang
I I need to enter a the Team name (String) from the array.

I cannot get it to work.

Can someone please help me
1. Show the struct definition.

2. Show the definition of the array of structs that you are using.

3. Show how you got something into a team name (or how you expect to get something there). In other words: show what you tried.

Quote:
Originally Posted by s1lang
this is the relevant part
CPP / C++ / C Code:
    strcpy(team, arrdetails[i].team);

No; this shows how you tried to retrieve the information. Since you say that it doesn't work, the relevant part is: how was the information supposed to get into the array in the first place? What is the nature of the struct and the array of structs?


Regards,

Dave
  #3  
Old 05-Apr-2007, 08:16
s1lang s1lang is offline
New Member
 
Join Date: Mar 2007
Posts: 6
s1lang is on a distinguished road

Re: Inputting text (name) as opposed to an int (ID) - C programming


CPP / C++ / C Code:
typedef struct
{

        int ID;
		char team[16];
        int played;
        int won;
		int drew;
		int lost;
		int goalsf;
        int goalsa;
        int points;
}

team_info;

team_info arrdetails[12];


int print_header();
void draw_table ();
int take_team_input();
int take_team_name();
int num_teams = 0;
void save_table();
void load_table();
void sort_table();



This is where the team info is entered into the array such as the team name

CPP / C++ / C Code:

int take_team_name()
{

         char teamd[16];

         printf("\nName:\n>");

         scanf("%s", teamd);

         strcpy(arrdetails[num_teams].team, teamd);

         arrdetails[num_teams].ID = num_teams;
		 
		 arrdetails[num_teams].played = 0;

         arrdetails[num_teams].won = 0;

		 arrdetails[num_teams].lost = 0;

		 arrdetails[num_teams].drew = 0;

         arrdetails[num_teams].goalsf = 0;

         arrdetails[num_teams].goalsa = 0;

         arrdetails[num_teams].points = 0;

		 num_teams++;

         return 0;

 }

Hope this gives you an idea of how I'm trying to get it to work Davekw
  #4  
Old 05-Apr-2007, 08:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Inputting text (name) as opposed to an int (ID) - C programming


Quote:
Originally Posted by s1lang
Hope this gives you an idea of how I'm trying to get it to work
OK; I get the idea.

Here is a suggestion:

Make a test program that just calls take_team_name a couple of times. Put in debug print statements that allow you to see what the program sees:

CPP / C++ / C Code:
int main()
{
    int i;
    for (i = 0; i < 2; i++) {
        take_team_name();
        printf("arrdetails[%d].ID = %d\n", i, arrdetails[i].ID);
        printf("arrdetails[%d].name = %s\n", i, arrdetails[i].team);
    }
    printf("\n");
    for (i = 0; i < 2; i++) {
        printf("arrdetails[%d].ID = %d\n", i, arrdetails[i].ID);
        printf("arrdetails[%d].name = %s\n", i, arrdetails[i].team);
    }
    return 0;
}

You could put print statements in your function also:
CPP / C++ / C Code:
int take_team_name()
{
    char teamd[16];

    printf("Entering take_team_name: num_teams = %d\n", num_teams);
    printf("\nName:\n>");
    scanf("%s", teamd);
    printf("You entered %s\n", teamd);
    strcpy(arrdetails[num_teams].team, teamd);
    printf("arrdetails[%d].team = <%s>\n", num_teams, arrdetails[num_teams].team);
    arrdetails[num_teams].ID = num_teams;
    printf("ID = %d\n", arrdetails[num_teams].ID);
.
.
.}

My output:

Code:
Entering take_team_name: num_teams = 0 Name: >Lions You entered Lions arrdetails[0].team = <Lions> ID = 0 Leaving take_team_name: num_teams = 1 arrdetails[0].ID = 0 arrdetails[0].name = Lions Entering take_team_name: num_teams = 1 Name: >Tigers You entered Tigers arrdetails[1].team = <Tigers> ID = 1 Leaving take_team_name: num_teams = 2 arrdetails[1].ID = 1 arrdetails[1].name = Tigers arrdetails[0].ID = 0 arrdetails[0].name = Lions arrdetails[1].ID = 1 arrdetails[1].name = Tigers

So, your basic concept seems usable. If you get past this point with a couple of teams, then try calling take_team_input().

You can do something similar in that function to see what the heck is happening with your real program. Does it work up to a point and then crash or what? Just saying that "you cannot get it to work", doesn't give us much of a clue.


Regards,

Dave


Foototes: Use of scanf("%ss") is dangerous, since it doesn't protect against a user entering more characters than can be held by the array.

Also scanf("%s") doesn't take into account that teams may have spaces in their names. Maybe you don't have any such teams, so that is not a problem with this particular league. But what if some wants to use this program with a league that has "Bad News Bears" or "White Sox", or whatever?

Many (most?) experienced programmers would probly recommend fgets() here. You have to do a little more work, but you can make it absolutely safe, and it will accommodate team names with spaces, should that ever become important.
  #5  
Old 05-Apr-2007, 10:09
s1lang s1lang is offline
New Member
 
Join Date: Mar 2007
Posts: 6
s1lang is on a distinguished road

Re: Inputting text (name) as opposed to an int (ID) - C programming


Thank you Dave.
I'm going to try and test all that you mentioned this evening.

Once again Thank you
  #6  
Old 05-Apr-2007, 11:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Inputting text (name) as opposed to an int (ID) - C programming


Also, please consider you code formatting. See this for ideas to format your code so it's more readable.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #7  
Old 12-Apr-2007, 02:21
s1lang s1lang is offline
New Member
 
Join Date: Mar 2007
Posts: 6
s1lang is on a distinguished road

Re: Inputting text (name) as opposed to an int (ID) - C programming


Thank you again guys
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
justifying text with dynamic programming twigboy C++ Forum 6 03-Feb-2005 14:30
saving html text dopee MySQL / PHP Forum 1 17-Jan-2005 04:15
Inputting program results into a text file? shouyan C Programming Language 2 15-Jul-2004 14:04
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:29.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.