An interesting c program for beginners

A

apaticul

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
 
A

apaticul

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.


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);



}


}
 
N

Nick Keighley

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)

/* 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,001
Latest member
Kendra00E1

Latest Threads

Top