Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > An interesting c program for beginners

Reply
Thread Tools

An interesting c program for beginners

 
 
apaticul@gmail.com
Guest
Posts: n/a
 
      05-05-2008
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
 
Reply With Quote
 
 
 
 
apaticul@gmail.com
Guest
Posts: n/a
 
      05-05-2008
On May 5, 2:26*am, "Malcolm McLean" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> 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);



}


}
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      05-06-2008
On 5 May, 07:45, (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
interesting C program prady C Programming 66 12-03-2007 08:35 AM
veRified: Interesting FACTORISE program, by trial division. + Fermat s method.Casio fx-82 MS schools sci-calc. don.lotto@paradise.net.nz NZ Computing 7 07-26-2007 06:26 AM
beginners program robertnielsen@cfu.net C++ 6 04-06-2005 02:44 PM
Beginners Program Lenny Challis Perl Misc 17 12-11-2003 04:08 AM
Beginners help with password program! tjland@iserv.net Python 3 07-27-2003 03:38 PM



Advertisments