SOMEONE HELP ME, HELP ME, HELP ME PLEASE...
Author Message
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

assignment (I'm so stressed out) that is a Currency Conversion program
that is supposed to read in this "myfile1.txt" file:

1. French francs            7.725860
2. Belgian francs           47.51230
3. Swiss francs             1.778600
4. Hong Kong dollars        7.800310
7. Chile pesos              567.5370
8. Mexican pesos            9.532890
9. Egyptian pounds          3.699590
10. British pounds          0.691600
11. Cyprus pounds           0.675584
12. Israel shekels          4.106780
13. Russian rubles          27.85980
14. Danish kroner           8.771930
15. Irish punts             0.927593
16. Brazilian real          1.869860
17. Japanese yen            108.2800
18. Greek drachmas          399.5210
19. European euros          1.177800
20. U.S. dollars            1.000000

What do you wish to convert from (enter a value from 1 to 20)? 20
What do you wish to convert to (enter a value from 1 to 20)? 17
Then amount to covert: \$1.00

(It then is supposed to print a table and grow each time the user loops
through.)

From            To                From Amt        To Amt

U.S. dollars    Japanese yen      1.00            108.2800

Do you want to contine -> Y or N

Anyway this is what I have so far:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TABLE_LEN 50
#define COUNTRY_LEN 28
#define RATE_LEN 10
#define REC_LEN 80

int i;
int num_country;
char rate_str[RATE_LEN];
char recbuf[REC_LEN];
char country [COUNTRY_LEN + 1] [TABLE_LEN];
double rate [TABLE_LEN];

{
for (i = 0; i < num_country; i++)
{
printf("%s", country[i]);
}
printf("\n\n\t\tPlease enter a value from 1 to 20:: ");

Quote:
}

{
FILE *fp;
int i=0;
if ((fp = fopen("myfile1.txt", "r+")) == NULL)
{
printf ("Error opening file.");
return 0;
}
while (fgets(recbuf, sizeof(recbuf), fp));
{
strncpy (&country [i] [0], COUNTRY_LEN);
strncpy (rate_str, &recbuf[COUNTRY_LEN], RATE_LEN);
country [i] [COUNTRY_LEN] = 0;
rate[i] = atof (rate_str);
i++;;
}
return i;

Quote:
}

I will forever be indebted to the one who helps me out of my pain and
misery....  I really respect programmers, this stuff is NOT easy. I am a
beginner so any help would be greatly appreciated!!!

-Michele

Sat, 12 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...
[snip description of homework assignment]

Quote:
> Anyway this is what I have so far:

> #include <stdio.h>
> #include <conio.h>

teaching C, he's a liar. If that's an option, consider dropping the
course and learing C from a good book or a good teacher.

Quote:
> #include <stdlib.h>
> #include <string.h>
> #include <ctype.h>

> #define TABLE_LEN 50
> #define COUNTRY_LEN 28
> #define RATE_LEN 10
> #define REC_LEN 80

> int i;
> int num_country;
> char rate_str[RATE_LEN];
> char recbuf[REC_LEN];
> char country [COUNTRY_LEN + 1] [TABLE_LEN];
> double rate [TABLE_LEN];

File scope ("global") variables should only be used when really
necessary.

Quote:
> {
>     FILE *fp;
>     int i=0;
>     if ((fp = fopen("myfile1.txt", "r+")) == NULL)
>     {
>     printf ("Error opening file.");

Error messages usually go to stderr. Also consider indenting this
block to make clear that it belongs to the if statement.

Quote:
>     return 0;

Does the caller test for this return value and react appropriately?

Quote:
>     }
>         while (fgets(recbuf, sizeof(recbuf), fp));

That ';' doesn't belong there.

Quote:
>         {
>         strncpy (&country [i] [0], COUNTRY_LEN);

This shouldn't compile. strncpy() takes three arguments.

Quote:
>         strncpy (rate_str, &recbuf[COUNTRY_LEN], RATE_LEN);
>         country [i] [COUNTRY_LEN] = 0;

This doesn't make sense to me.

Quote:
>         rate[i] = atof (rate_str);

atof() is a dangerous function. Try using strtod() instead.

Quote:
>         i++;;

Superfluous ;

Quote:
>         }
>     return i;
> }

> I will forever be indebted to the one who helps me out of my pain and
> misery....  I really respect programmers, this stuff is NOT easy. I am a
> beginner so any help would be greatly appreciated!!!

Well, there's some help above, but the whole program is quite a
complex thing for a total beginner. Of course you must understand
that we won't do it for you; we will help if you have specific
problems you can't figure out, but if you want to know C, *you* have
to learn it.
The brand new page at http://users.powernet.co.uk/eton/clc/cbooks.html
lists a lot of books that this group's regulars recommend. Consider
getting one to complement or replace your class.
Good luck,

Gergo

--
To err is human.
To blame someone else for your mistakes is even more human.

Sat, 12 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...
here we go...

On Tue, 24 Oct 2000 12:53:18 -0700, Michele Kendall

Quote:

>#include <stdio.h>
>#include <conio.h>
>#include <stdlib.h>
>#include <string.h>
>#include <ctype.h>

>#define TABLE_LEN 50
>#define COUNTRY_LEN 28
>#define RATE_LEN 10
>#define REC_LEN 80

>int i;

don't make this global.  it's bad practice.  move this declaration to

Quote:
>int num_country;
>char rate_str[RATE_LEN];
>char recbuf[REC_LEN];
>char country [COUNTRY_LEN + 1] [TABLE_LEN];

i believe you've got your rows and columns mixed up here.

Quote:
>double rate [TABLE_LEN];

>{
>    for (i = 0; i < num_country; i++)

you never initialize num_country.

Quote:
>    {
>        printf("%s", country[i]);

you may want a newline in the format string.

Quote:
>    }
>    printf("\n\n\t\tPlease enter a value from 1 to 20:: ");
>}

>{
>    FILE *fp;
>    int i=0;
>    if ((fp = fopen("myfile1.txt", "r+")) == NULL)

why "r+"?  are you writing back to the file somewhere?

Quote:
>    {
>    printf ("Error opening file.");
>    return 0;
>    }
>        while (fgets(recbuf, sizeof(recbuf), fp));

that semicolon doesn't look like it should be there...what follows
isn't a loop block.

Quote:
>        {
>        strncpy (&country [i] [0], COUNTRY_LEN);

strncpy() takes 3 arguments.  and &country[i][0] is the same as
country[i].

Quote:
>        strncpy (rate_str, &recbuf[COUNTRY_LEN], RATE_LEN);
>        country [i] [COUNTRY_LEN] = 0;
>        rate[i] = atof (rate_str);

rate_str doesn't have the right format; read up on atof().

Quote:
>        i++;;
>        }
>    return i;
>}

finally, if this is meant to be a complete program, it won't compile
without a main().  and work on your indentation.

-ck
--
char*t="hello world\n";main(v,c){return(v++||write(v,t++,c))&&main(!v,!!*t);}

Sat, 12 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...
THANK YOU--You are a lifesaver!!! I might have some more questions for you
tomorrow. I'm going to work on it tonight.

--Michele

Quote:

> Michele Kendall a crit dans le message

> >assignment (I'm so stressed out) that is a Currency Conversion program
> >that is supposed to read in this "myfile1.txt" file:

> >1. French francs            7.725860
> >2. Belgian francs           47.51230
> >3. Swiss francs             1.778600
> >4. Hong Kong dollars        7.800310
> >7. Chile pesos              567.5370
> >8. Mexican pesos            9.532890
> >9. Egyptian pounds          3.699590
> >10. British pounds          0.691600
> >11. Cyprus pounds           0.675584
> >12. Israel shekels          4.106780
> >13. Russian rubles          27.85980
> >14. Danish kroner           8.771930
> >15. Irish punts             0.927593
> >16. Brazilian real          1.869860
> >17. Japanese yen            108.2800
> >18. Greek drachmas          399.5210
> >19. European euros          1.177800
> >20. U.S. dollars            1.000000

> >What do you wish to convert from (enter a value from 1 to 20)? 20
> >What do you wish to convert to (enter a value from 1 to 20)? 17
> >Then amount to covert: \$1.00

> >(It then is supposed to print a table and grow each time the user loops
> >through.)

> >From            To                From Amt        To Amt

> >U.S. dollars    Japanese yen      1.00            108.2800

> >Do you want to contine -> Y or N

> >Anyway this is what I have so far:

> >#include <stdio.h>
> >#include <conio.h>
> >#include <stdlib.h>
> >#include <string.h>
> >#include <ctype.h>

> >#define TABLE_LEN 50
> >#define COUNTRY_LEN 28
> >#define RATE_LEN 10
> >#define REC_LEN 80

> >int i;
> >int num_country;
> >char rate_str[RATE_LEN];
> >char recbuf[REC_LEN];
> >char country [COUNTRY_LEN + 1] [TABLE_LEN];
> >double rate [TABLE_LEN];

> First of all, you should work on data organization. What you want is to read
> a file where each record is a rank number (can be ignored), a string with
> the name of a currency (or a country) and a rate.

> record
>    string currency
>    number rate
> record end

> You want a certain size (say 20), and the exact number. Note that better
> design should be to count the records, and then allocate the exact amount of
> required data. But a fixed number is fine for a beginner's program.

> data
>    array of 20 records
>    integer number of records
> data end

> Once the data is designed, you can organize the code around. The functions
> are nothing but data processors.

> Here is a working example of your code (revised)

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <ctype.h>

> #define TABLE_LEN 50
> #define COUNTRY_LEN 28
> #define REC_LEN 80

> #define FNAME "currency.txt"

> typedef struct
> {
>    char country[COUNTRY_LEN + 1];
>    double rate;
> }
> record_t;

> typedef struct
> {
>    int num_country;
>    record_t records[TABLE_LEN];
> }
> data_t;

> {
> int ret = 0;
> char s[4];

>    fgets(s, sizeof s, stdin);
>    {
>    long const n = strtol(s, NULL, 10);
>       ret = (int) n;
>    }
>    return ret;
> }

> void displaymenu (data_t * pdata)
> {
>    int i;
>    for (i = 0; i < pdata->num_country; i++)
>    {
>       printf ("%2d - %s\n", i + 1, pdata->records[i].country);
>    }
>    printf ("\nPlease enter a value from 1 to %d: "
>           , pdata->num_country);
>    fflush(stdout);
>    /* To be continued. It is your job now...  */
> }

> int readfile (data_t * pdata)
> {
>    FILE *const fp = fopen (FNAME, "r+");
>    int i = 0;
>    if (fp == NULL)
>    {
>       printf ("Error opening '%s'.\n", FNAME);
>    }
>    else
>    {
>       char recbuf[REC_LEN];
>       while (fgets (recbuf, sizeof recbuf, fp) != NULL)
>       {
>       char*pstart=NULL;
>       char *p = strchr(recbuf, '.');
>       record_t *const precord = &pdata->records[i];

>          if (p)
>          {
>             p++;
>             while (isspace(*p))
>             {
>                p++;
>             }
>             pstart=p;

>             while (!isdigit(*p))
>             {
>                p++;
>             }
>             p--;
>             *p=0;

>             strcpy (precord->country, pstart);
>             p++;
>             precord->rate = strtod (p, NULL);
>          }
>          i++;;
>       }
>       pdata->num_country = i;
>    }
>    return i;
> }

> int main (void)
> {
>    data_t data = {0};

>    {
>    }
>    return 0;
> }

> Feel free to ask more for details.

> --
> -hs-    Tabs out, spaces in.
> CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
> ISO-C Library: http://www.dinkum.com/htm_cl
> FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc

Sat, 12 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...
Michele Kendall a crit dans le message

Quote:
>assignment (I'm so stressed out) that is a Currency Conversion program
>that is supposed to read in this "myfile1.txt" file:

>1. French francs            7.725860
>2. Belgian francs           47.51230
>3. Swiss francs             1.778600
>4. Hong Kong dollars        7.800310
>7. Chile pesos              567.5370
>8. Mexican pesos            9.532890
>9. Egyptian pounds          3.699590
>10. British pounds          0.691600
>11. Cyprus pounds           0.675584
>12. Israel shekels          4.106780
>13. Russian rubles          27.85980
>14. Danish kroner           8.771930
>15. Irish punts             0.927593
>16. Brazilian real          1.869860
>17. Japanese yen            108.2800
>18. Greek drachmas          399.5210
>19. European euros          1.177800
>20. U.S. dollars            1.000000

>What do you wish to convert from (enter a value from 1 to 20)? 20
>What do you wish to convert to (enter a value from 1 to 20)? 17
>Then amount to covert: \$1.00

>(It then is supposed to print a table and grow each time the user loops
>through.)

>From            To                From Amt        To Amt

>U.S. dollars    Japanese yen      1.00            108.2800

>Do you want to contine -> Y or N

>Anyway this is what I have so far:

>#include <stdio.h>
>#include <conio.h>
>#include <stdlib.h>
>#include <string.h>
>#include <ctype.h>

>#define TABLE_LEN 50
>#define COUNTRY_LEN 28
>#define RATE_LEN 10
>#define REC_LEN 80

>int i;
>int num_country;
>char rate_str[RATE_LEN];
>char recbuf[REC_LEN];
>char country [COUNTRY_LEN + 1] [TABLE_LEN];
>double rate [TABLE_LEN];

First of all, you should work on data organization. What you want is to read
a file where each record is a rank number (can be ignored), a string with
the name of a currency (or a country) and a rate.

record
string currency
number rate
record end

You want a certain size (say 20), and the exact number. Note that better
design should be to count the records, and then allocate the exact amount of
required data. But a fixed number is fine for a beginner's program.

data
array of 20 records
integer number of records
data end

Once the data is designed, you can organize the code around. The functions
are nothing but data processors.

Here is a working example of your code (revised)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TABLE_LEN 50
#define COUNTRY_LEN 28
#define REC_LEN 80

#define FNAME "currency.txt"

typedef struct
{
char country[COUNTRY_LEN + 1];
double rate;

Quote:
}

record_t;

typedef struct
{
int num_country;
record_t records[TABLE_LEN];

Quote:
}

data_t;

{
int ret = 0;
char s[4];

fgets(s, sizeof s, stdin);
{
long const n = strtol(s, NULL, 10);
ret = (int) n;
}
return ret;

Quote:
}

{
int i;
for (i = 0; i < pdata->num_country; i++)
{
printf ("%2d - %s\n", i + 1, pdata->records[i].country);
}
printf ("\nPlease enter a value from 1 to %d: "
, pdata->num_country);
fflush(stdout);
/* To be continued. It is your job now...  */

Quote:
}

{
FILE *const fp = fopen (FNAME, "r+");
int i = 0;
if (fp == NULL)
{
printf ("Error opening '%s'.\n", FNAME);
}
else
{
char recbuf[REC_LEN];
while (fgets (recbuf, sizeof recbuf, fp) != NULL)
{
char*pstart=NULL;
char *p = strchr(recbuf, '.');
record_t *const precord = &pdata->records[i];

if (p)
{
p++;
while (isspace(*p))
{
p++;
}
pstart=p;

while (!isdigit(*p))
{
p++;
}
p--;
*p=0;

strcpy (precord->country, pstart);
p++;
precord->rate = strtod (p, NULL);
}
i++;;
}
pdata->num_country = i;
}
return i;

Quote:
}

int main (void)
{
data_t data = {0};

{
}
return 0;

Quote:
}

Feel free to ask more for details.

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc

Sun, 13 Apr 2003 06:07:47 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

Quote:
> Here is a working example of your code (revised)

Now this is no way to help a student. Please don't be so eager.

~~

v/973.386.8598

Sun, 13 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...
David L Rubin schrieb:

Quote:

> > Here is a working example of your code (revised)

> Now this is no way to help a student. Please don't be so eager.

> ~~

> v/973.386.8598

Why not, David,
It is a good piece of code, and if the student takes the time, a
de{*filter*} and a good c-book to go through it and to really understand
what is going on she/he will afterwards have a _much_ better
knowledge of the language.

And I will do the same now and hopefully learn something.

And if I don't understand one or the other point I will come back
here and get a good answer if I ask a good question :)

Regards
Robert

Sun, 13 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

Quote:

<snip>

> {
> int ret = 0;
> char s[4];

>    fgets(s, sizeof s, stdin);
>    {
>    long const n = strtol(s, NULL, 10);
>       ret = (int) n;
>    }
>    return ret;
> }

Haven't you forgotten something? fgets() returns a value, whose purpose
is more than merely decorative.

<snip>

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Sun, 13 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

Quote:

> David L Rubin schrieb:

> > > Here is a working example of your code (revised)

> > Now this is no way to help a student. Please don't be so eager.
[snip]
> Why not, David,
> It is a good piece of code, and if the student takes the time, a
> de{*filter*} and a good c-book to go through it and to really understand
> what is going on she/he will afterwards have a _much_ better
> knowledge of the language.

student along with more and more specific hints than to present an
actual solution -- even if the student has some code -- before the
professor posts a solution. The student will be more likely to run to
the ng if she knows that someone will post a solution than if she knows
that she will only get (vague) hints. Lastly, the ng is not a place to
solve homework problems (before they are due). This undermines the
professor and the TA, and cheats the other students in the class.

assignment (I'm so stressed out)..."

It does not sound like the assignment was past due.

david

~~

v/973.386.8598

Sun, 13 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...
David L Rubin schrieb:

Quote:

> > David L Rubin schrieb:

> > > > Here is a working example of your code (revised)

> > > Now this is no way to help a student. Please don't be so eager.
> [snip]
> > Why not, David,
> > It is a good piece of code, and if the student takes the time, a
> > de{*filter*} and a good c-book to go through it and to really understand
> > what is going on she/he will afterwards have a _much_ better
> > knowledge of the language.

> Firstly, an alert professor will monitor this ng, and copied answers
> student along with more and more specific hints than to present an
> actual solution -- even if the student has some code -- before the
> professor posts a solution. The student will be more likely to run to
> the ng if she knows that someone will post a solution than if she knows
> that she will only get (vague) hints. Lastly, the ng is not a place to
> solve homework problems (before they are due). This undermines the
> professor and the TA, and cheats the other students in the class.

> assignment (I'm so stressed out)..."

> It does not sound like the assignment was past due.

>         david

> ~~

> v/973.386.8598

I agree with what you are saying above, David, maybe I try too much

I just saw, that the OP posted some (pretty helpless) attempt and
the code posted by -hs- was incomplete and stopped at exactly the
same point as the OP's attempt. -hs-, clever and sophisticated as he
is, even left the users response in the local variable i, which
means, the OP _must_ find out how the code works and how the data
are organised in it before he/she (?) can continue. So I did not
feel -hs- did the homework for the OP. And befor the code he gave
good hints about how to approach such a problem.

Kind regards
Robert

Sun, 13 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

Quote:

> Firstly, an alert professor will monitor this ng,

This ng, and comp.programming, and unix.programmer, and any other
programming-related news group, mailing list, etc?
Fine if s/he does, but that's above and beyond the call of duty.

I don't know that it's possible to catch all the cheats.

Dan

Sun, 13 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

Quote:
>I don't know that it's possible to catch all the cheats.

<laugh>
How is your screen after you have prounouced this sentence ten times high
and loud ?
</laugh>

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc

Sun, 13 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

Quote:

> > David L Rubin schrieb:
> > > > Here is a working example of your code (revised)
> > > Now this is no way to help a student. Please don't be so eager.
> [snip]
> > Why not, David,
> > It is a good piece of code, and if the student takes the time, a
> > de{*filter*} and a good c-book to go through it and to really understand
> > what is going on she/he will afterwards have a _much_ better
> > knowledge of the language.

> Firstly, an alert professor will monitor this ng, ...

Maybe an idea for a Product?   Prof just submits students' submissions
to the NetMatchCop utility, and gets a probability factor indicating
degree of plagiarism.    The Deluxe Version provides URLs of plagiarized
sources....

Assignment due by Friday:  Code NetMatchCop in C, compile it and then
apply it to itself.

- Larry Weiss

Sun, 13 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

Quote:

> Maybe an idea for a Product?   Prof just submits students' submissions
> to the NetMatchCop utility, and gets a probability factor indicating
> degree of plagiarism.

Such programs (or services, at least) already exist, believe it
or not.  I don't have a URL, but here at MSU I am told that such
a tool is used for certain of the low-level programming classes.

I don't think that there are any freely available versions of
such utilities, though.  After all, if you know how it works,
then you could just write a companion program to permute a
program to the point where it gets a low plagiarism score against
the original.

Sun, 13 Apr 2003 03:00:00 GMT
SOMEONE HELP ME, HELP ME, HELP ME PLEASE...

Quote:

>Maybe an idea for a Product?   Prof just submits students' submissions
>to the NetMatchCop utility, and gets a probability factor indicating
>degree of plagiarism.    The Deluxe Version provides URLs of plagiarized
>sources....

>Assignment due by Friday:  Code NetMatchCop in C, compile it and then
>apply it to itself.

You mean: rip NetMatchCop from someone else, hand it in and wait. :)

Sun, 13 Apr 2003 03:00:00 GMT

 Page 1 of 2 [ 25 post ] Go to page: [1] [2]

Relevant Pages