Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Unseen error
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Barry Schwarz, post: 1693995"] Assuming you copied it correctly, the book has several errors. Any chance it was written by Schildt? While older compilers still accept this, the new language standard has done away with implied return types for functions. Get in the habit of doing it right: int main(void) Interesting that your book uses a comment style allowed only in C99 yet uses other features (such as above) that are disallowed in C99. Any time your interactive output does not end with a '\n', you run the risk of it not being displayed to the user due to buffering considerations. Not an error but most recommend an explicit return from main(). Since this is not main and since you don't return a value, the implied return type of int here is not acceptable. You must specify void. While still legal, this construction is very obsolete. (When was the book written?) The "modern" construction combines the two lines to void init_player(struct football player[]) This has the additional benefit of allowing you to place function prototypes in scope before you call the function. I believe this is a requirement in the new language standard but is a good idea even if not since it allows the compiler to check that your arguments to the function have the correct type. [i][i] You do realize that this serves no purpose? [i] Neither does this. system is declared in stdlib.h which you did not #include. Why on earth would you want to clear the screen right after accepting user input? [i][i] This invokes undefined behavior. fflush is defined for output streams only and has never been defined for input streams. [i][i][i][i] This is a major problem. more is a single char. %s will accept a string which is guaranteed to be longer than one char. This invokes undefined behavior and overwrites whatever is in memory following more. One possible solution is to use %c. What if the user types 'Y'? [i][i] When i is 28, the while is true and you loop one final time to accept alpha[29] ...[i] and you then overlay alpha[29] with a nul. It is bad form to destroy a user's input without telling him. If you coded 28 in the while, he would at least know that you didn't let him enter the last character. These titles and underscores are not wide enough for the 30 character entries that follow. [i][i][i] This is the source of the error you asked about. Did you mean 99 here? 2 makes no sense at all. This looks like a transcription error. Even with 99, there is a logic error. In init_player(), you initialize name and team to blanks with no terminating '\0'. In player_input(), you allow for the possibility of less than 100 players. This loop does not test for this and will attempt to print names and teams that are not strings with the %s format. This invokes undefined behavior. Another attempt to read a string into a single char. Is this really what the book says? [i][i] Even though it produces the correct result in most cases, this is a poor construct. To see why, let's reformat it so the news reader does not break up the line. The only changes I am making are removing the /n> that indicates quoted material, inserting a space before each &&, and inserting a \n after each &&. while ((player[i].goals!=-1) && (i<100) && (match=strcmp(name,player[i].name)!=0)) The first problem is the second test needs to be first. You cannot check player[i].goals if i is 100 or more. That variable does not exist and attempting to do so invokes undefined behavior. The && operator has a short-circuit evaluation so if you rearrange the tests and the first one fails, the remaining are never evaluated. Additionally, last expression is "broken." Here it is again with some addition (and suggestive) white space. (match = strcmp(name,player[i].name) != 0) Since != has higher precedence than =, this expression is evaluated as ( match = (strcmp(name,player[i].name) != 0) ) which means "assign to match the value 1 or 0 depending on whether the return value from strcmp is different from or the same as 0, respectively." The recommended construction is ( (match = strcmp(name,player[i].name)) != 0 ) which means "assign the return value from strcmp to match and then determine if it is different from or the same as 0." The reason yours works in this case is because you are only interested in equality or inequality and match will be 0 if and only if strcmp returns 0. However, for example, if your array were sorted, you would be interested in two of the three possible return values from strcmp. Your construction would not give you that. Both negative and positive returns from strcmp would be combined into a single value for match. Don't you think this would be better inside the following if? Why ask for input if you know you cannot use it? [i][i] If you give us the name of the book and author and date, we can add it to the list of books not to use. <<Remove the del for email>>[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Unseen error
Top