Multi-word entry in C

tsj

tsj

Permabanned
Joined
7 May 2004
Posts
1,051
Location
United Kingdom
Code:
/** Enters Team Name **/
int initialise(team_info * arrdetails,
               const int numofteams)
{

  /** if not reading from file... **/
  int i = 0;
  char rtn = 0;
  int j;

  int num_teams = 0;

  for(i = 0; i < numofteams; i++)
  {
    char teamd[16];

    printf("\nPlease enter team name:\n>");
    scanf("%s%c", teamd, &rtn);


    for(j = 0; j < i; ++j)
    {
      int match = strcmp(teamd, arrdetails[j].team) == 0;

      if(match)
      {
        printf("Sorry, this team already exists. Please try again!");
        break;
      }

      else
      {
        strcpy(arrdetails[i].team, teamd);
        arrdetails[i].played = 0;
        arrdetails[i].won = 0;
        arrdetails[i].lost = 0;
        arrdetails[i].draw = 0;
        arrdetails[i].points = 0;
        arrdetails[i].goalsfor = 0;
        arrdetails[i].goalsagainst = 0;
        arrdetails[i].goalsd = 0;
        arrdetails[i].team_id = num_teams;
        num_teams++;
      }
    }

  }
  return num_teams;
}

At the moment my application will only allow me to enter team names such as:

'ManchesterUtd' instead of 'Manchester Utd'
'BlackburnRovers' instead of 'Blackburn Rovers'

Any ideas on how I can enhance my application to allow a multi-worded entry?

Thanks
 
Could you read in 1 string then the next and combine them?

(sorry if this is not possible, i dont know c)
 
touch said:
Could you read in 1 string then the next and combine them?

(sorry if this is not possible, i dont know c)

I'm not sure mate will try it tomorrow, and hope for some other replies before then.

I'm off to bed now though. Thanks for your input though.
 
you should use fgets(string,string_size,stdin); this way you can get a whole string of char. You shouldnt use gets this function is not safe as it does not look for overflow of the character string. Also this method will most certainly get you a string with '\n' at the end, so you should proceed the string to get it out because if you print the name afterward it will be buggy :p something like that would do the job i guess:

string[strlen(string)] = '\0';
 
Leodido said:
something like that would do the job i guess:

string[strlen(string)] = '\0';

No it wouldnt - strlen looks for the null terminator, so it wont work. In any case the code is unnecessary - fgets will always produce null terminated strings (assuming that the string fits within your buffer. If it doesnt fgets returns NULL.
 
Chrisss said:
Or just do

Code:
char readin[10];
char readin1[10];
scanf("%s %s",&readin,&readin1);


Ummm i will try this i guess?

But thing is... later on I will be outputting and then reading back from a file etc.

An example file is :

Code:
4     bolton 30 14 5 11 35 38 47
10 blackburnr 30 12 4 14 36 41 40
5    everton 30 12 10 8 38 26 46
8  middlesbo 30 9 9 12 32 36 36
2    arsenal 29 16 7 6 51 24 55
11 portsmouth 30 11 9 10 36 31 42
1    chelsea 30 21 6 3 54 19 69
7    reading 30 13 5 12 43 38 44
3  liverpool 30 16 6 8 44 20 54
0       manu 30 24 3 3 70 20 75
6  tottenham 30 13 6 11 43 44 45
9  newcastle 30 10 7 13 34 39 37

Would it be possible to read back from such a file if it contained two worded-team names?
 
tsj said:
Ummm i will try this i guess?

But thing is... later on I will be outputting and then reading back from a file etc.

An example file is :

Code:
4     bolton 30 14 5 11 35 38 47
10 blackburnr 30 12 4 14 36 41 40
5    everton 30 12 10 8 38 26 46
8  middlesbo 30 9 9 12 32 36 36
2    arsenal 29 16 7 6 51 24 55
11 portsmouth 30 11 9 10 36 31 42
1    chelsea 30 21 6 3 54 19 69
7    reading 30 13 5 12 43 38 44
3  liverpool 30 16 6 8 44 20 54
0       manu 30 24 3 3 70 20 75
6  tottenham 30 13 6 11 43 44 45
9  newcastle 30 10 7 13 34 39 37

Would it be possible to read back from such a file if it contained two worded-team names?


I will also be reading results in the format:

Code:
  scanf("%s %d %d %s", &playername1, &score1, &score2, &playername2);
  printf("\nYou inputted: %s %d %d %s\n", &playername1, score1, score2, &playername2);
 
Visage said:
No it wouldnt - strlen looks for the null terminator, so it wont work. In any case the code is unnecessary - fgets will always produce null terminated strings (assuming that the string fits within your buffer. If it doesnt fgets returns NULL.
I think his concern was not that the string wouldn't be terminated, but that the last character of the string (before the terminating NULL) would be a '\n'.

I wasn't quite sure what the answer is, and interestingly, a quick test on my machine tells me that gets() produces a string without a '\n' on the end and fgets() produces a string with a '\n'. Which surprises me; I don't think I've ever used gets() (for the obvious buffer overflow reasons), so I wasn't aware of the difference.

I've generally steered clear of scanf in favour of rolling my own parser (better control of errors etc), so I'm not sure whether you can persuade it to do what tsj is asking for. I don't think it will be easy, at any rate.
 
DaveF said:
I think his concern was not that the string wouldn't be terminated, but that the last character of the string (before the terminating NULL) would be a '\n'.

I wasn't quite sure what the answer is, and interestingly, a quick test on my machine tells me that gets() produces a string without a '\n' on the end and fgets() produces a string with a '\n'. Which surprises me; I don't think I've ever used gets() (for the obvious buffer overflow reasons), so I wasn't aware of the difference.

I've generally steered clear of scanf in favour of rolling my own parser (better control of errors etc), so I'm not sure whether you can persuade it to do what tsj is asking for. I don't think it will be easy, at any rate.

Would it work using fgets or gets?
 
tsj said:
Would it work using fgets or gets?
If you use fgets you'll just get one long string for each line of text. Which is fine when it's just the user entering the team name, but not so good for reading in a file like you said you want to.

In that case if you used fgets() you'd then have to write code yourself to split that string into the various parts (id, team name, score, etc).

In your position, what I would do is store the team names with a special character, (e.g. "_") replacing spaces, so Blackburn Rovers would be Blackburn_Rovers, say.

Then when the user enters the name of a team, you need to search through the string, replacing each space with '_'; conversely, when printing the name of a team, you need to print ' ' instead of '_'.

This is a slight pain, but still a fair bit easier than parsing the files yourself.
 
DaveF said:
If you use fgets you'll just get one long string for each line of text. Which is fine when it's just the user entering the team name, but not so good for reading in a file like you said you want to.

In that case if you used fgets() you'd then have to write code yourself to split that string into the various parts (id, team name, score, etc).

In your position, what I would do is store the team names with a special character, (e.g. "_") replacing spaces, so Blackburn Rovers would be Blackburn_Rovers, say.

Then when the user enters the name of a team, you need to search through the string, replacing each space with '_'; conversely, when printing the name of a team, you need to print ' ' instead of '_'.

This is a slight pain, but still a fair bit easier than parsing the files yourself.


Nice idea mate :D

Will try this now and come back if i need some help.

Thanks
 
Back
Top Bottom