I am not sure what this means and I get it all the time. Can someone help me fix this code?
[ char add_account(account_t a[])
7 {
8 int i, N=1000000;
9 char ch;
10 char str1, str2;
11 int m=20;
12
13 for(i=0;i<N;i++)
14 {
15 if(a.number==0)
16 {
17 printf("Enter first name\n");
18 fgets(str1, m, stdin);
19 strcpy(a.first, str1);
20 printf("Enter middle initial\n");
21 getchar();
22 ch=a.mid;
23 printf("Enter last name\n");
24 fgets(str2, m, stdin);
25 strcpy(a.last, str2);
26 printf("Enter new account number\n");
27 scanf("%d", &a.number);
28 printf("Enter opening balance\n");
29 scanf("%f", &a.balance);
30 }
31 else printf("No more new accounts accepted\n");
32 }
33 return a;
34 }
]

I am not sure what this means and I get it all the time.

Really and what would that be?

Can someone help me fix this code?

What's wrong with it beside not having code tags?

Edited 6 Years Ago by gerard4143: n/a

Really and what would that be?

What's wrong with it beside not having code tags?

It says that the argument I am passing to fgets makes a pointer to an integer without a cast and then my strcpy won't work since fgets isn't getting the string.

because your first arguments to fgets are all single characters.

the argument is supposed to be a STRING. not a single character. a single character is actually an 8-bit integer, so it thinks you are passing in an integer when it expects the starting position of a string (aka character array).

define your character arrays (ie, strings) thusly:

char str1[32];
char str2[99];
char str3[MAX_STR_LEN];

//etc.

or whatever size you need.

and PLEASE use code tags next time. look at your code in the first post. is that even readable to you? its not to me.

Edited 6 Years Ago by jephthah: n/a

Thanks for your help. Sorry about the code. This is the first time I've ever posted anything. I'll do better next time. Thanks again!

I have one more problem. How do I get fgets to take from user input instead of stdin?

it takes user input (from keyboard) by using stdin, like so:

char inputStr[MAX_STR_LEN];
fgets(inputStr, sizeof(inputStr)-1), stdin);

... is this what youre asking?

if you want to take input via a file, then check out the example given for fgets()

Edited 6 Years Ago by jephthah: n/a

I just changed it all to scanf and that worked perfectly. Thanks for all your help. You made my life much easier.

i recommend that you use fgets() instead of scanf().

it's up to you, of course, but best to learn the correct way sooner rather than later.

it takes user input (from keyboard) by using stdin, like so:

char inputStr[MAX_STR_LEN];
fgets(inputStr, sizeof(inputStr)-1), stdin);

... is this what youre asking?

A little note concerning sizeof(inputStr)-1 There's not need to make room (-1) for the nul terminator. fgets() does that already. It will always accommodate the '\0' fgets(inputStr, sizeof(inputStr)-1), stdin); There's an extra closing parenthesis.

Edited 6 Years Ago by Aia: n/a

Comments
thanks, good catch.

yes, thanks Aia, you're right. i often forget about that. and good catch on the extra parenthesis. that was a typo.

sorry for the confusion.

just a side note, sometimes it's important that a precise maximum number of characters be allowed to be entered by the user, and this value may be defined similar to #define MAX_STR_LEN ... since fgets() always makes room for the null, you would have to add a space to the character buffer

char inputStr[MAX_STR_LEN + 1];

fgets(inputStr, sizeof(inputStr), stdin);

this is where i sometimes get mixed up if i'm not paying attention. it's definitely important to remember when the size of your user's input is critical.


.

Edited 6 Years Ago by jephthah: n/a

This question has already been answered. Start a new discussion instead.