Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: An interest...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 8 Topic 26065 of 26977
Post > Topic >>

Re: An interesting c program for beginners

by Nick Keighley <nick_keighley_nospam@[EMAIL PROTECTED] > May 6, 2008 at 03:58 AM

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
 




 8 Posts in Topic:
An interesting c program for beginners
apaticul@[EMAIL PROTECTED  2008-05-04 23:45:50 
Re: An interesting c program for beginners
Richard Heathfield <rj  2008-05-05 07:48:07 
Re: An interesting c program for beginners
"Malcolm McLean"  2008-05-05 09:26:35 
Re: An interesting c program for beginners
apaticul@[EMAIL PROTECTED  2008-05-05 04:23:26 
Re: An interesting c program for beginners
Niz <niz@[EMAIL PROTEC  2008-05-05 13:18:22 
Re: An interesting c program for beginners
"Bill Leary" &l  2008-05-05 10:25:32 
Re: An interesting c program for beginners
William Pursell <bill.  2008-05-05 13:15:45 
Re: An interesting c program for beginners
Nick Keighley <nick_ke  2008-05-06 03:58:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:38:22 CDT 2008.