Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Help with my source code (http://www.velocityreviews.com/forums/t314792-re-help-with-my-source-code.html)

Malcolm 08-16-2003 04:33 PM

Re: Help with my source code
 

"Jonny" <jonny.keogh@btinternet.com> wrote in message
>
> Why doesnt this work, please?
>
> #include <stdio.h>
> #include <conio.h>
>

conio.h is non-standard. This doesn't mean that you shouldn't use it, but
its contents are off-topic here.
>
> #include <stdlib.h>
> #include <ctype.h>
>
> #define TRUE 1
> #define FALSE !TRUE
>

This is a bad idea. Leave basic things like TRUE FALSE BOOL etc for library
vendors or standards bodies to define. Defining TRUE yourself is pretty much
guaranteed to collide with someone else's definition sooner or later.
>
> void drawborder1(char *name,float cost,char *key);
> void draw_border2(void);
>
> float total = 0;
>
> FILE *order;
>
> float lo = 0;
>

Globals are a bad idea as well.
>
> void main()
>

It's int main(void) and returns 0 on success, EXIT_FAILURE on failure.
>
> {
> int done;
> char c;
>
> done=FALSE;
> while(!done)
> {
> c=toupper(getch());
> switch(c)
> {
> case 'A': // Pizza type 1
> drawborder1("P1",5,"A");
> break;
>
> case 'B': // Pizza type 2
> drawborder1("P2",6,"B");
> break;
>
> case 'F': // Finish order
> draw_border2();
> done=TRUE;
>
> default: // Other
> drawborder1("Unknown",0,"Unknown");
> break;
> }
> }
> }
>

This code looks OK.
>
> void drawborder1(char *name,float cost,char *key)
>

The basic idea of writing a drawborder1() function is good. However it seems
to be opening an order file as well as drawing a border, which is illogical.
>
> {
> order=fopen("orders.dat","r");
> gotoxy(1,1);
>

printf() is a standard libray function, whilst gotoxy() is not. Don't expect
gotoxy() to affect printf()'s operation.
>
>

printf("旼컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴 컴컴컴컴컴컴컴컴컴컴컴컴컴
> 컴컴컴컴컴커");
> printf(" JKS Shop 1.0.0
> ");
>
>

printf("쳐컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴 컴컴컴컴컴컴컴컴컴컴컴컴컴
> 컴컴컴컴컴캑");
> printf("
> ");
> printf(" Item name:
> ");
> printf(" Item cost: o
> ");
> printf(" Item key :
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
>
>

printf("읕컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴 컴컴컴컴컴컴컴컴컴컴컴컴컴
> 컴컴컴컴컴켸");
> printf("Loading . . .
> ");
> if(order==NULL)
> {
> gotoxy(1,25);
> printf("There was a error opening the order file. Press any key to

exit
> . . .");
> getch();
> exit(0);
>

Should be exit(EXIT_FAILURE) since you are exiting with a fail condition.
>
> }
> gotoxy(14,5);
> printf("%s",name);
> fprintf(order,"%i: %s\n",lo + 1,name);
> gotoxy(15,6);
> printf("%i",cost);
> total = total + cost;
> lo = lo + 1;
> gotoxy(14,7);
> printf("%s",key);
> gotoxy(1,25);
> fclose(order);
>

Just so that you know, fclose() can fail as well as fopen(). It is unlikely
and doesn't really matter in an exercise, but if your data was very precious
you would check the return value.
>
> printf("Key Code: ");
> }
>


>
> void draw_border2(void)
> {
> gotoxy(1,1);
>
>

printf("旼컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴 컴컴컴컴컴컴컴컴컴컴컴컴컴
> 컴컴컴컴컴커");
> printf(" JKS Shop 1.0.0 - Please finish order
> ");
>
>

printf("쳐컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴 컴컴컴컴컴컴컴컴컴컴컴컴컴
> 컴컴컴컴컴캑");
> printf("
> ");
> printf(" Total cost: o
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
> printf("
> ");
>
>

printf("읕컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴 컴컴컴컴컴컴컴컴컴컴컴컴컴
> 컴컴컴컴컴켸");
> printf("Loading . . .
> ");
> gotoxy(15,5);
> printf("%f.2f",total);
> gotoxy(1,25);
> printf("Opening the order file . . .");
> order=fopen("orders.dat","r");
> if(order==NULL)
> {
> gotoxy(1,25);
> printf("There was a error opening the order file. Press any key to

exit
> . . .");
> getch();
> exit(0);
> }
> gotoxy(1,25);
> printf("Writing to the order file . . .");
> fprintf(order,"Order Complete!");
> fclose(order);
> gotoxy(1,25);
> printf("Please declare the cost and press any key to exit . . .");
> getch();
> exit(0);
> }
>
>





Eirik 08-16-2003 08:09 PM

Re: Help with my source code
 
On Sat, 16 Aug 2003 17:33:01 +0100, Malcolm wrote:



conio.h is a header file that Borland makes, I think. I haven't seen it
anywhere else.


All times are GMT. The time now is 02:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.