-
passing structures
Hi!
I have a structure defined as follows:
Code:
struct passenger
{
int flag; //Use as a boolean - 1 if seat taken, 0 if not taken
char first[NAME];
char last[NAME];
};
typedef struct passenger pass;
And I pass it to a function to modify the contents - name and last :
Code:
void option_c(pass cust[][LABEL])
{ char ans[3], label;
int row;
printf("Enter the seat number you wish to edit: ");
seat_check(&row, &label, ans);
if (cust[row][label].flag)
printf("blah blah balh");
else
{
printf("Enter first:");
scanf("%[^\n]", cust[row][label].first);
fflush(stdin);
printf("Enter second:");
scanf("%[^\n]", cust[row][label].last);
fflush(stdin);
}
}
The problem lies in the else. For some unknown reason - the program will crash at this point
-
*gasp*
Why fflush(stdin) is wrong
Where exactly does it crash? On the scanf()s? What are row and label set to by seat_check()? 12273849?
-
Code:
else
{
printf("Enter first:");
fgets(cust[row][label].first, NAME, stdin);
/*scanf("%[^\n]", cust[row][label].first);
fflush(stdin);*/
printf("Enter second:");
fgets(cust[row][label].first, NAME, stdin);
/*scanf("%[^\n]", cust[row][label].last);
fflush(stdin);*/
}
use fgets
ssharish2005
-
Don't forget to remove the newline. From http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char buf[BUFSIZ];
char *p;
printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
if (fgets(buf, sizeof(buf), stdin) != NULL)
{
printf ("Thank you, you entered >%s<\n", buf);
/*
* Now test for, and remove that newline character
*/
if ((p = strchr(buf, '\n')) != NULL)
*p = '\0';
printf ("And now it's >%s<\n", buf);
}
return 0;
}
/*
Program output:
Please enter a line of text, max 512 characters
this is a test
Thank you, you entered >this is a test
<
And now it's >this is a test<
*/
I seriously doubt that's the problem though. I bet the values of row or label are wacky.