An interesting c program for beginners

Discussion in 'C Programming' started by apaticul@gmail.com, May 5, 2008.

  1. Guest

    Hi all,

    O.K. here 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, saved 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!"

    the following is the I/O file
    that would be included with the persoalinfo.c:
    {

    int x;
    FILE *infile;

    /* Always check to make sure that you succeeded in opening the file.
    */

    infile = fopen("result.txt", "r");
    if (infile == NULL)
    printf("Unable to open result.txt\n");

    else
    {
    while (fscanf(infile, "%d", &x) != EOF)
    printf("%d\n", x);

    fclose(infile);
    }

    this above code is supposed to pop-up in the command prompt afterwards
    you've entered your personal data.


    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. Everything
    is returned though the parameters, which are passed by reference.
    */

    void readPerson (char *first, char *last,
    int *age, int *ssn, float *salary) {
    printf("First name: ");
    gets(first);
    printf("Last Name: ");
    gets(last);
    printf("Age: ");
    scanf("%d", age);
    printf("Social security number (no hyphens, just 3 figures): ");
    scanf("%d", ssn);
    printf("Salary: ");
    scanf("%f", salary);
    }


    /* This writes out information about a person. */

    void writePerson (char *first, char *last,
    int age, int ssn, float salary) {
    printf("%s %s, %d years old, SSN %d, salary of $%9.2f\n",
    first, last, age, ssn, salary);
    }


    /* Reads in and writes out information about a person. */

    void main () {
    char first[100];
    char last[100];
    int age;
    int ssn;
    float salary;

    readPerson(first, last, &age, &ssn, &salary);
    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?
    TIA
     
    , May 5, 2008
    #1
    1. Advertising

  2. Guest

    On May 5, 2:26 am, "Malcolm McLean" <> wrote:
    > <> wrote in message
    > > void main () {
    > >  char first[100];
    > >  char last[100];
    > >  int age;
    > >  int ssn;
    > >  float salary;

    >
    > >  readPerson(first, last, &age, &ssn, &salary);
    > >  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?
    > > TIA

    >
    > Comment out everything and add printf("hello world\n") to the body of
    > main(). That tells you your compiler is set up correctly.
    >
    > Once hello world works, comment in the smaller function, and pass it dummy
    > data. See if it prints the dummy data. Fiddle with it until correct.
    > Then turn your attention to the larger fucntion. Try just reading in one
    > variable at first. Add variables and continue fiddling with it until it
    > works.
    > Eventually you end up with a working program.
    >
    > --
    > Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm- Hide quoted text -
    >
    > - Show quoted text -



    Good advice, really, thanks Malcolm!
    so far I could come up with a workable code,
    I will have to improvise more, obviously


    I'll have to figure out a way to use that "for loop" in the case of
    "salary" variable. I'm just using an example from 1 to 5 to ilustrate
    a
    possible shortcut to get to the issue.


    #include <stdio.h>
    #include <ctype.h>
    void readPerson (char *first, char *last,
    int *age, int *ssn, float *salary) {
    printf("First name: ");
    gets(first);
    printf("Last Name: ");
    gets(last);
    printf("Age: ");
    scanf("%d", age);
    printf("Social security number (no hyphens, just 3 figures): ");
    scanf("%d", ssn);
    printf("Salary: ");
    scanf("%f", salary);
    }
    /* This writes out information about a person. */

    void writePerson (char *first, char *last,
    int age, int ssn, float salary) {
    printf("%s %s, %d years old, SSN %d, salary of $%9.2f\n",
    first, last, age, ssn, salary);



    }





    int main(void)
    {



    int i;
    FILE *fp, *fs;
    fs = fopen("ak4.bin", "wb");
    for (i = 0; i < 5; i++) {
    fputc(i, fs);
    }
    fclose(fs);
    fs = fopen("ak4.bin", "rb");
    fp = fopen("ak5.txt", "w");
    while ((i = fgetc(fs)) != EOF) {
    printf("%d\n", i);
    fprintf(fp, "%c", isprint(i) ? i : '.');
    fprintf(fp, " %d\n", i);



    }
    {
    char first[100];
    char last[100];
    int age;
    int ssn;
    float salary;


    readPerson(first, last, &age, &ssn, &salary);
    writePerson(first, last, age, ssn, salary);



    }


    }
     
    , May 5, 2008
    #2
    1. Advertising

  3. On 5 May, 07:45, wrote:

    > O.K.  here 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,  saved 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:
    > {
    >
    >   int x;
    >   FILE *infile;
    >
    >   /* Always check to make sure that you succeeded in opening the file.
    > */


    useless comment

    >   infile = fopen("result.txt", "r");


    you're going to *read* the results file?


    >   if (infile == NULL)
    >     printf("Unable to open result.txt\n");
    >
    >   else
    >     {
    >       while (fscanf(infile, "%d", &x) != EOF)
    >         printf("%d\n", x);
    >
    >       fclose(infile);
    >     }
    >
    > 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.  Everything
    >    is returned though the parameters, which are passed by reference.
    > */
    >
    > void readPerson (char *first, char *last,
    >                  int *age, int *ssn, float *salary) {
    >   printf("First name: ");
    >   gets(first);


    *never* use gets() (google for it)

    <snip>

    >
    > /* This writes out information about a person. */
    >
    > void writePerson (char *first, char *last,
    >                   int age, int ssn, float salary) {
    >   printf("%s %s, %d years old, SSN %d, salary of $%9.2f\n",
    >          first, last, age, ssn, salary);
    >
    > }
    >
    > /* Reads in and writes out information about a person. */
    >
    > void main () {


    INT INT INT!!!!!!!!!
    it's "int main (void)"


    >   char first[100];
    >   char last[100];
    >   int age;
    >   int ssn;
    >   float salary;
    >
    >   readPerson(first, last, &age, &ssn, &salary);
    >   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] = "", c;
    int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
    if (rc == 1) fscanf("%*[^\n]%*c);
    if (rc == 0) getc(fp);

    Dan Pop
     
    Nick Keighley, May 6, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clemens Bosch

    Beginners questions for addition

    Clemens Bosch, Dec 2, 2004, in forum: VHDL
    Replies:
    0
    Views:
    578
    Clemens Bosch
    Dec 2, 2004
  2. beginners program

    , Mar 30, 2005, in forum: C++
    Replies:
    6
    Views:
    467
    Boboo
    Apr 6, 2005
  3. Replies:
    3
    Views:
    423
  4. prady

    interesting C program

    prady, Nov 20, 2007, in forum: C Programming
    Replies:
    66
    Views:
    3,380
  5. Lenny Challis

    Beginners Program

    Lenny Challis, Dec 10, 2003, in forum: Perl Misc
    Replies:
    17
    Views:
    225
    Matt Garrish
    Dec 11, 2003
Loading...

Share This Page