PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > An interesting c program for beginners
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
An interesting c program for beginners

Réponse
 
LinkBack Outils de la discussion
Vieux 05/05/2008, 07h45   #1
apaticul@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut An interesting c program for beginners

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
  Réponse avec citation
Vieux 05/05/2008, 08h48   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An interesting c program for beginners

apaticul@gmail.com said:

> Hi all,
>
> O.K. here is an attempt to create a c program


<failed attempt snipped>

> So, my question is, how would I combine all these codes in order to
> get a proper working program?


The quickest course is the one I adopted, which is to delete the code and
start again, this time leaving the bugs out.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 05/05/2008, 09h26   #3
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An interesting c program for beginners


<apaticul@gmail.com> 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

  Réponse avec citation
Vieux 05/05/2008, 12h23   #4
apaticul@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An interesting c program for beginners

On May 5, 2:26am, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> <apati...@gmail.com> 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);



}


}
  Réponse avec citation
Vieux 05/05/2008, 13h18   #5
Niz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An interesting c program for beginners

On 2008-05-05 12:23:26 +0100, apaticul@gmail.com said:
>
> 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);


Use fgets() instead of gets() it is much safer.

  Réponse avec citation
Vieux 05/05/2008, 15h25   #6
Bill Leary
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An interesting c program for beginners

Small nit:

"Niz" <niz@nicetry.com> wrote in message
news:2008050513182216807-niz@nicetrycom...
> On 2008-05-05 12:23:26 +0100, apaticul@gmail.com said:
>> printf("Social security number (no hyphens, just 3 figures): ");


Three figures? All the SSN's I've seen are nine.

Did I miss something earlier in the thread where you wanted only three digit
portion of the SSN? If so, it seems the prompt should say so.

Also, I thought it wasn't guaranteed (assured? assumed?) that any output
would occur without a newline or an fflush. When I did stuff like this, I
used:

printf("Social security number (no hyphens, just 9 figures): ");
fflush(stdout);
scanf("%d", ssn);

In cases where I forgot the fflush, for some platforms it worked as desired
anyway. But on others, the prompts wouldn't appear until. In most of those
cases the prompt appeared after I hit Enter for the input value, like this:

123456789Social security number (no hyphens, just 9 figures
[]

That [] represents the cursor at the beginning of the next line.

But I also remember one system where NONE of the prompts appeared at all.
Then when I did a printf, which had a newline, all the prompts showed up at
the same time, followed by the printf text. The cause being how the
c-library and/or operating system did output buffering.

- Bill

  Réponse avec citation
Vieux 05/05/2008, 21h15   #7
William Pursell
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An interesting c program for beginners

On 5 May, 07:45, apati...@gmail.com wrote:

>
> /* 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");
>
>


Much preferred is:

char *path;
FILE *infile;

infile = fopen( path = "result.txt", "r");
if( infile == NULL )
perror( path );

This is preferred for 2 reasons (at least):
1) the error message goes to the error stream
where it belongs instead of the output stream
2) the error message includes the reason for
the failure. (Not on all systems, but on
most. Those systems on which fopen fails to
properly set errno on a failure are...deranged.)


  Réponse avec citation
Vieux 06/05/2008, 11h58   #8
Nick Keighley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: An interesting c program for beginners

On 5 May, 07:45, apati...@gmail.com 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h54.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20085 seconds with 16 queries