问题
ok so iffixed it up and me it presentable did some of the suggestions and i get the same error( im using codebloks btw ), the error of it just crashing without giving a reason. I got a second error with this. in the getinfo function after age is entered it prints the statement to get the gender then the statement to get the other persons name without letting me input(it seems to just skip that part
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void getinfo (char* nam[],int ag[], char gender[], int count){
int y;
for(y = 0; y < count; y++){
nam[y] = malloc(30);
printf ("What is the student's name?\t");
scanf ("%s", &nam[y]);
printf ("\nWhat is the students age?\t");
scanf ("%d", &ag[y]);
printf ("\nwhat is the students gender, M/F:\t");
scanf ("%c", &gender[y]);
}
}
void findeldest (char* nam[],int ag[], char* gender[], int count){
int largest = 0, y, eldest =0 ;
for(y = 0; y < count; y++){
if (ag[y] > eldest){
largest = ag[y];
eldest = y;
}
}
printf ("The eldest student is:\t%s", nam[eldest]);
printf ("\nGender:\t%c", gender[eldest]);
printf ("\nWith an age of:\t%d", ag[eldest]);
}
int main (){
int amount, y;
printf("How many students are you admitting?\t");
scanf ("%d", &amount);
if (amount > 50){
printf("Too many students!");
}else{
char *name[50];
int age[50];
char gender[50];
getinfo(name, age, gender, amount);
findeldest(name, age, gender, amount);
system("pause");
}
}
回答1:
The code provided isn't compilable and thus we can't diagnose the problem correctly. Please update your post with a compilable testcase that has only the bare essentials required to reproduce your problem.
if (ag[y] > eldest){
largest = ag[y];
eldest = y;
} /* <--- Note the inconsistent indentation.
* Do you want us to read your code and help you?
* If so, please fix your indentation.
*/
There's a comparison supposedly on ages, and then an assignment of an index to an age variable? Which does eldest store? Indexes, or ages? Take your pick, and make it consistent. Perhaps it might be a better idea to use more descriptive identifiers, such as "eldest_index" and "eldest_age". This would provide internal documentation that the "eldest_index" is the array index for the oldest person and the "eldest_age" is the age for the oldest person. As it seems largest stores ages, perhaps your code should look more like:
if (ag[y] > largest){
largest = ag[y];
eldest = y;
}
printf ("\nGender:\t%c", gender[eldest]); If eldest stores ages, not indexes, then wouldn't this cause your program to crash when eldest >= 50? I presume this statement will make more sense when you come up with better identifiers. Furthermore, gender[eldest] is a char *, where %c tells printf to expect an int with a character value.
edit: You wouldn't have encountered your second error if you had read this scanf manual before using scanf. In fact, using scanf before reading it's documentation is your error; Please stop.
If you place printf("The character entered for gender has an integer value of %d\n", (unsigned char) gender[y]); after scanf ("%c", &gender[y]);, what value do you get? Is '\n' a valid gender?
... so you've fixed the problem that occurs when the user presses enter between an integer read and a character read. Great! Without reading the manual, you've got a third problem. Supposing the user presses 'M' or 'F', followed by enter. This would result in either 'M' or 'F' being read from stdin by scanf ("%c", &gender[y]);, but the '\n' would remain on stdin. What is the effect of that? When the loop repeats, and you tell scanf to read up to but not including the next whitespace character (which is what '%s' indicates), you end up with a blank name! Ooops!
Assuming you expect the user to press enter before and after entering gender, I suggest changing scanf ("%c", &gender[y]); to:
int c;
for (c = getchar(); c >= 0 && c != '\n'; c = getchar()); /* Discard the remainder of the line,
* so that it doesn't interfere with
* the gender input.
*/
gender[y] = c;
for (c = getchar(); c >= 0 && c != '\n'; c = getchar()); /* Discard the remainder of the line,
* so that it doesn't interfere with
* the next scanf call.
*/
... and don't forget to read that scanf manual, carefully!
回答2:
You did not post the source code of the getinfo function, so the best we can do here is guess. The first place I would look is in getinfo, when it populates the 'nam' and 'gender' parameters. You are most likely using stack local strings for those. Once the 'getinfo' function returns, those strings no longer exists. The pointers to those locations on the stack still exist however, and whenever findeldest tries to use those pointers in printf, it prints garbage off the stack.
If you supply your 'getinfo' implementation this will be easier. Here is my implementation
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void findeldest (char* nam[],int ag[], char* gender[], int count){
int largest = 0, y, eldest =0 ;
for(y = 0; y < count; y++){
if (ag[y] > eldest){
largest = ag[y];
eldest = y;
}
}
printf ("The eldest student is:\t%s", nam[eldest]);
printf ("\nGender:\t%s", gender[eldest]);
printf ("\nWith an age of:\t%d", ag[eldest]);
}
void getinfo(char* nam[],int ag[], char* gender[], int count)
{
static const char * const MALE = "male";
static const char * const FEMALE = "female";
static const char * const BRIAN = "brian";
static const char * const SUE = "sue";
static const char * const DAVE = "dave";
static const char * const APRIL ="april";
for(int i = 0 ; i < count ; ++i)
{
ag[i] = i;
switch(i%4)
{
case 0:
nam[i] = BRIAN;
break;
case 1:
nam[i] = SUE;
break;
case 2:
nam[i] = DAVE;
break;
case 3:
nam[i] = APRIL;
break;
}
switch(i%2)
{
case 0:
gender[i] = MALE;
break;
case 1:
gender[i] = FEMALE;
break;
}
}
return;
}
int main (){
int amount, y;
printf("How many students are you admitting?\t");
scanf ("%d", &amount);
if (amount > 50){
printf("Too many students!");
} else {
char *name[50];
int age[50];
char *gender[50];
getinfo(name, age, gender, amount);
findeldest(name, age, gender, amount);
system("pause");
}
}
It works fine for me. Notice the correction to the second 'printf' statement in findeldest. You were using %c (single character) when you mean %s (null terminated string of characters).
Also, you are not using C90 compliant code. When posting, you should specify what compiler you are using and what options you need (if any) to make this compile. It appears to be close to valid C99 code. I get some compiler warnings about misuse of const in my 'getinfo' implementation.
来源:https://stackoverflow.com/questions/15307088/code-crashing-when-finding-the-eldest-age-edit