www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/09/18/10:58:21

From: "A. Sinan Unur" <asu1 AT c-o-r-n-e-l-l DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Getche doesnt work!
Date: 17 Sep 2002 21:49:36 GMT
Organization: Cornell University
Lines: 176
Sender: asu1 AT cornell DOT invalid (on pool-141-149-209-34.syr.east.verizon.net)
Message-ID: <Xns928CB5548BBC0asu1cornelledu@132.236.56.8>
References: <am7jh7$ru1$1$8300dec7 AT news DOT demon DOT co DOT uk>
NNTP-Posting-Host: pool-141-149-209-34.syr.east.verizon.net
X-Trace: news01.cit.cornell.edu 1032299376 4865 141.149.209.34 (17 Sep 2002 21:49:36 GMT)
X-Complaints-To: usenet AT news01 DOT cit DOT cornell DOT edu
NNTP-Posting-Date: 17 Sep 2002 21:49:36 GMT
User-Agent: Xnews/5.04.25
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Jenny Reeves" <jenny DOT reeves AT s-4-b DOT com> wrote in
news:am7jh7$ru1$1$8300dec7 AT news DOT demon DOT co DOT uk: 

> Getche doesnt work!

by the way, the code as you posted it, does not compile, so it is hard to 
figure out what you mean by 'does not work':

C:\Dload\misc>gcc -c t.c -Wall
t.c:3: warning: return type of `main' is not `int'
t.c: In function `main':
t.c:7: `AccFile' undeclared (first use in this function)
t.c:7: (Each undeclared identifier is reported only once
t.c:7: for each function it appears in.)
t.c:7: warning: implicit declaration of function `fopen'
t.c:7: `FileName' undeclared (first use in this function)
t.c:10: warning: implicit declaration of function `fill_array'
t.c:19: warning: implicit declaration of function `welcome'
t.c:21: warning: implicit declaration of function `get_acc_no'
t.c:23: warning: implicit declaration of function `get_pin'
t.c:27: `match' undeclared (first use in this function)
t.c:35: warning: implicit declaration of function `menu'
t.c:37: warning: implicit declaration of function `get_choice'
t.c:39: warning: implicit declaration of function `do_choice'
t.c:43: `choice' undeclared (first use in this function)
t.c:47: warning: implicit declaration of function `rewind'
t.c:51: `i' undeclared (first use in this function)
t.c:55: warning: implicit declaration of function `write_file'
t.c:55: `accounts' undeclared (first use in this function)
t.c:59: warning: implicit declaration of function `fclose'
t.c: At top level:
t.c:63: parse error before "get"
t.c:75: parse error before string constant
t.c:75: warning: type defaults to `int' in declaration of `printf'
t.c:75: warning: conflicting types for built-in function `printf'
t.c:75: warning: data definition has no type or storage class
t.c:77: parse error before string constant
t.c:77: warning: type defaults to `int' in declaration of `scanf'
t.c:77: warning: data definition has no type or storage class
t.c:79: warning: type defaults to `int' in declaration of `getche'
t.c:79: warning: data definition has no type or storage class
t.c:81: parse error before '}' token
t.c:123: parse error before numeric constant
t.c:123: warning: type defaults to `int' in declaration of `gotoxy'
t.c:123: warning: data definition has no type or storage class
t.c:125: parse error before string constant
t.c:125: warning: type defaults to `int' in declaration of `printf'
t.c:125: warning: data definition has no type or storage class
t.c:133: warning: type defaults to `int' in declaration of `getche'
t.c:133: warning: data definition has no type or storage class
t.c:135: parse error before "break"


> void main() 

main always returns an int, and if you are writing C, the standard way to 
specify an empty argument list is to use void:

int main(void)

<snip>

>                   get_choice();
> 
>                   do_choice();
>                   }
> 
>       }while (choice !=5);

<snip>

> void get_choice()
> 
> {
> 
>  gotoxy(21,22);
> 
>  printf("Please enter your choice: ");
> 
>  scanf("%d",&choice);
> 
>  getche();
> 
>  }

first, don't mix conio functions with stdio. if you want to display text at 
specific coordinates on the screen, use cprintf etc.

second, you are mixing buffered input (scanf) with unbuffered input 
(getche). it is in general not a good idea to use scanf for interactive 
input.

third, everything would be easier to follow if you gave up on the global 
variable choice, and used return values instead.

maybe the following code would help you:

#include <conio.h>
#include <dpmi.h>
#include <pc.h>

void wait_for_keypress() {
	while(kbhit() == 0) {
		__dpmi_yield();
	}
}

void simple_print(const char *msg) {
	int ch;
	clrscr();
	cputs(msg);
	wait_for_keypress();
	ch = getch();	
}

void display_menu(const char *menu[], int num_choices, int x, int y) {
	int i;
	for(i = 0; i < num_choices; ++i) {
		gotoxy(x, y + i);
		cprintf("[ %d ] %s", i + 1, menu[i]);
	}
}

void do_menu(void) {
	int ch;
	int please_quit = 0;
#define NUM_CHOICES 5
	static const char *menu[] = {"Balance", "Withdraw", "Deposit", 
"Help", "Quit"};

	while(!please_quit) {
		clrscr();
		display_menu(menu, NUM_CHOICES, 30, 10);
		
		wait_for_keypress();
		
		ch = getch();

		switch(ch) {
			case '1' :
				simple_print(menu[0]);
				break;
			case '2' :
				simple_print(menu[1]);
				break;
			case '3' :
				simple_print(menu[2]);
				break;
			case '4' :
				simple_print(menu[3]);
				break;
			case '5' :
				simple_print(menu[4]);
				please_quit = 1;
				break;
			default :
				break;
		}
	}
	return;
}

int main(void) {
	do_menu();
	return 0;
}





-- 
A. Sinan Unur
asu1 AT c-o-r-n-e-l-l DOT edu
Remove dashes for address
Spam bait: mailto:uce AT ftc DOT gov

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019