On 5 May, 07:45, apati...@[EMAIL PROTECTED]
wrote:
> O.K. =A0here is an attempt to create a c program
> which consists of
> a structure, where you're supposed to enter some personal information.
> Now, it's pretty obvious some folks would enter a larger ammount of
> salary lets say a 6 figure.
> My attempt is to have a few files, called result1.txt, result2.txt,
> and result3.txt, =A0saved in the same folder as the c program, lets call
> it
> "personalinfo.c"
>
> this result.txt* files, would be as such:
> result1.txt: "6 figure salary? Yeah, right! Try again (and cut a few
> 0's ;-) )
> result2.txt: "5 figure salary? Good for you!"
> result3.txt: "4 figure salary? Right on!"
a general point. I know you are only writing a toy
program (well I *hope* you are!). But in a professional
program I make it a rule never to put "smart" remarks
in anything a user (or manager) might see. I extend
that to comments as well.
Above all never imply the user is dumb. Especially
if you believe he is! Remember, if he becomes none dumb
he might decide to dispense with your services!
> the following is the I/O file
> that would be included with the persoalinfo.c:
> {
>
> =A0 int x;
> =A0 FILE *infile;
>
> =A0 /* Always check to make sure that you succeeded in opening the file.
> */
useless comment
> =A0 infile =3D fopen("result.txt", "r");
you're going to *read* the results file?
> =A0 if (infile =3D=3D NULL)
> =A0 =A0 printf("Unable to open result.txt\n");
>
> =A0 else
> =A0 =A0 {
> =A0 =A0 =A0 while (fscanf(infile, "%d", &x) !=3D EOF)
> =A0 =A0 =A0 =A0 printf("%d\n", x);
>
> =A0 =A0 =A0 fclose(infile);
> =A0 =A0 }
>
> this above code is supposed to pop-up in the command prompt afterwards
> you've entered your personal data.
so why didn't you write code that did that?
> Next, the following is the personalinfo.c (will require stuff from the
> above mentioned files)
>
> #include <stdio.h>
>
> /* This prompts for and reads information about a person. =A0Everything
> =A0 =A0is returned though the parameters, which are passed by reference.
> */
>
> void readPerson (char *first, char *last,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0int *age, int *ssn, float *salary) {
> =A0 printf("First name: ");
> =A0 gets(first);
*never* use gets() (google for it)
<snip>
>
> /* This writes out information about a person. */
>
> void writePerson (char *first, char *last,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int age, int ssn, float salary) {
> =A0 printf("%s %s, %d years old, SSN %d, salary of $%9.2f\n",
> =A0 =A0 =A0 =A0 =A0first, last, age, ssn, salary);
>
> }
>
> /* Reads in and writes out information about a person. */
>
> void main () {
INT INT INT!!!!!!!!!
it's "int main (void)"
> =A0 char first[100];
> =A0 char last[100];
> =A0 int age;
> =A0 int ssn;
> =A0 float salary;
>
> =A0 readPerson(first, last, &age, &ssn, &salary);
> =A0 writePerson(first, last, age, ssn, salary);
>
> }
>
> So, my question is, how would I combine all these codes in order to
> get a proper working program?
get the bits working correctly first. Then link them together.
--
Nick Keighley
The fscanf equivalent of fgets is so simple
that it can be used inline whenever needed:-
char s[NN + 1] =3D "", c;
int rc =3D fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
if (rc =3D=3D 1) fscanf("%*[^\n]%*c);
if (rc =3D=3D 0) getc(fp);
Dan Pop


|