By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,838 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,838 IT Pros & Developers. It's quick & easy.

Help! Esc button won't exit program

P: n/a
Please help! I can't get my program to exit if the user hits the
Escape button:

When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this
error:

Parse Error, expecting `'}''
'else if (choice == 27) exit(0) } }'

Here is my program (Simple loop to display currency equivalencies based
on the user's choice of currency)
#include<stdio.h>
#include <stdlib.h>

main(void)
{
char choice ; /*Assign a variable for the user's input*/
char status ; /*Assign a variable that will control the status of the
loop*/

float EUR = .07; /*EUR exchange rate*/
float GBP = .05; /*GBP exchange rate*/
float CAD = 1.23; /*CAD exchange rate*/
float JPY = 102.60; /*JPY exchange rate*/
float NZD = 1.40; /*NZD exchange rate*/

printf("Here is a list of five non-US currencies: \n"); /*Display five
currency choices*/
printf("1-EUR\n");
printf("2-GBP\n");
printf("3-CAD\n");
printf("4-JPY\n");
printf("5-NZD\n");
printf("\n");

printf("Choose the number beside the currency to display its US dollar
equivalent. Press ESC to exit.\n");
status = scanf("%s", &choice); /*Controls the status of the loops*/
while (status != 66)
{
if (choice == '1') /*If user chooses currency 1*/
printf("The equivalent is %.2f \n", EUR); /*then display the
equivalent rate for EUR*/
else if (choice == '2') /*If user chooses currency 2*/
printf("The equivalent is %.2f \n", GBP); /*then display the
equivalent rate for GBP*/
else if (choice == '3') /*If user chooses currency 3*/
printf("The equivalent is %.2f \n", CAD); /*then display the
equivalent rate for CAD*/
else if (choice == '4') /*If user chooses currency 4*/
printf("The equivalent is %.2f \n", JPY); /*then display the
equivalent rate for JPY*/
else if (choice == '5') /*If user chooses currency 5*/
printf("The equivalent is %.2f \n", NZD); /*then display the
equivalent rate for NZD*/
else if (choice != '1', '2', '3', '4', '5')
printf("Invalid entry. Try again.\n"); /*Prompt the user to input a
valid choice*/
printf("\n");
printf("Here is a list of five non-US currencies: \n"); /*Display
five currency choices*/
printf("1-EUR\n");
printf("2-GBP\n");
printf("3-CAD\n");
printf("4-JPY\n");
printf("5-NZD\n");
printf("\n");
printf("Choose the number beside the currency to display its US dollar
equivalent. Press ESC to exit.\n");
status = scanf("%s", &choice);
else if (choice == 27)
exit(EXIT_SUCCESS)
}
}

If you can help tonight, I will be forever indebted to you!

Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
drose0927 wrote:
Please help! I can't get my program to exit if the user hits the
Escape button:
Hmmm. Why should it?
When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this
error:

Parse Error, expecting `'}''
'else if (choice == 27) exit(0) } }'
Notice the missing semicolon.
Here is my program (Simple loop to display currency equivalencies based
on the user's choice of currency)
#include<stdio.h>
#include <stdlib.h>

main(void) int main(void) /* implicit int is gone in C99 */

{
char choice ; /*Assign a variable for the user's input*/
char status ; /*Assign a variable that will control the status of the
loop*/

float EUR = .07; /*EUR exchange rate*/
float GBP = .05; /*GBP exchange rate*/
float CAD = 1.23; /*CAD exchange rate*/
float JPY = 102.60; /*JPY exchange rate*/
float NZD = 1.40; /*NZD exchange rate*/

printf("Here is a list of five non-US currencies: \n"); /*Display five
currency choices*/
printf("1-EUR\n");
printf("2-GBP\n");
printf("3-CAD\n");
printf("4-JPY\n");
printf("5-NZD\n");
printf("\n");

printf("Choose the number beside the currency to display its US dollar
equivalent. Press ESC to exit.\n");
status = scanf("%s", &choice); /*Controls the status of the loops*/ `%s' is used to read a `string'. You want %c to read a char.
while (status != 66)
What's `66'? {
if (choice == '1') /*If user chooses currency 1*/
printf("The equivalent is %.2f \n", EUR); /*then display the
equivalent rate for EUR*/
else if (choice == '2') /*If user chooses currency 2*/
printf("The equivalent is %.2f \n", GBP); /*then display the
equivalent rate for GBP*/
else if (choice == '3') /*If user chooses currency 3*/
printf("The equivalent is %.2f \n", CAD); /*then display the
equivalent rate for CAD*/
else if (choice == '4') /*If user chooses currency 4*/
printf("The equivalent is %.2f \n", JPY); /*then display the
equivalent rate for JPY*/
else if (choice == '5') /*If user chooses currency 5*/
printf("The equivalent is %.2f \n", NZD); /*then display the
equivalent rate for NZD*/
else if (choice != '1', '2', '3', '4', '5')
printf("Invalid entry. Try again.\n"); /*Prompt the user to input a
valid choice*/
printf("\n");
printf("Here is a list of five non-US currencies: \n"); /*Display
five currency choices*/
printf("1-EUR\n");
printf("2-GBP\n");
printf("3-CAD\n");
printf("4-JPY\n");
printf("5-NZD\n");
printf("\n");
printf("Choose the number beside the currency to display its US dollar
equivalent. Press ESC to exit.\n");
status = scanf("%s", &choice);
else if (choice == 27)
What is 27?
exit(EXIT_SUCCESS)
Missing semicolon. }
}

If you can help tonight, I will be forever indebted to you!


Great. Just keep making the payments.

Other than that, I'll assume this is one of your first efforts --
because it shows (NTTAWWT).

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2

P: n/a
"drose0927" writes:
equivalent. Press ESC to exit.\n");
status = scanf("%s", &choice);


%s says read a string. But choice is a char.
Nov 14 '05 #3

P: n/a
drose0927 wrote:

Please help! I can't get my program to exit if the user hits the
Escape button:


If you are operating under BillWare you never hear the ESC. MS, in
its infinite wisdom, decided to use that as a line cancel signal,
in place of the traditional CAN = CTL-X. They pulled that stunt
almost 25 years ago.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #4

P: n/a
"drose0927" <da***********@yahoo.com> wrote in message news:<11**********************@z14g2000cwz.googleg roups.com>...
Please help! I can't get my program to exit if the user hits the
Escape button:

When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this
error:

Parse Error, expecting `'}''
'else if (choice == 27) exit(0) } }'

Here is my program (Simple loop to display currency equivalencies based
on the user's choice of currency)
#include<stdio.h>
#include <stdlib.h>

main(void)
{
char choice ; /*Assign a variable for the user's input*/
char status ; /*Assign a variable that will control the status of the
loop*/

float EUR = .07; /*EUR exchange rate*/
float GBP = .05; /*GBP exchange rate*/
float CAD = 1.23; /*CAD exchange rate*/
float JPY = 102.60; /*JPY exchange rate*/
float NZD = 1.40; /*NZD exchange rate*/

printf("Here is a list of five non-US currencies: \n"); /*Display five
currency choices*/
printf("1-EUR\n");
printf("2-GBP\n");
printf("3-CAD\n");
printf("4-JPY\n");
printf("5-NZD\n");
printf("\n");

printf("Choose the number beside the currency to display its US dollar
equivalent. Press ESC to exit.\n");
status = scanf("%s", &choice); /*Controls the status of the loops*/
while (status != 66)
{
if (choice == '1') /*If user chooses currency 1*/
printf("The equivalent is %.2f \n", EUR); /*then display the
equivalent rate for EUR*/
else if (choice == '2') /*If user chooses currency 2*/
printf("The equivalent is %.2f \n", GBP); /*then display the
equivalent rate for GBP*/
else if (choice == '3') /*If user chooses currency 3*/
printf("The equivalent is %.2f \n", CAD); /*then display the
equivalent rate for CAD*/
else if (choice == '4') /*If user chooses currency 4*/
printf("The equivalent is %.2f \n", JPY); /*then display the
equivalent rate for JPY*/
else if (choice == '5') /*If user chooses currency 5*/
printf("The equivalent is %.2f \n", NZD); /*then display the
equivalent rate for NZD*/
else if (choice != '1', '2', '3', '4', '5')
printf("Invalid entry. Try again.\n"); /*Prompt the user to input a
valid choice*/
printf("\n");
printf("Here is a list of five non-US currencies: \n"); /*Display
five currency choices*/
printf("1-EUR\n");
printf("2-GBP\n");
printf("3-CAD\n");
printf("4-JPY\n");
printf("5-NZD\n");
printf("\n");
printf("Choose the number beside the currency to display its US dollar
equivalent. Press ESC to exit.\n");
status = scanf("%s", &choice);
else if (choice == 27)
exit(EXIT_SUCCESS)
}
}

If you can help tonight, I will be forever indebted to you!


1)missing semicolon ';'
2)wrong token specifier %s when choice is actually declared as a char

if (choice==27) exit(0);

it works perfectly in mine!!
hope it works wid u too!
Nov 14 '05 #5

P: n/a
On 9 Feb 2005 17:08:35 -0800, "drose0927" <da***********@yahoo.com>
wrote:

<snip>
char choice ; /*Assign a variable for the user's input*/
char status ; /*Assign a variable that will control the status of the
loop*/

float EUR = .07; /*EUR exchange rate*/
float GBP = .05; /*GBP exchange rate*/
float CAD = 1.23; /*CAD exchange rate*/
float JPY = 102.60; /*JPY exchange rate*/
float NZD = 1.40; /*NZD exchange rate*/
Floating-point numbers on most (all?) computers today are binary and
cannot exactly represent most decimal fractions. For just storing and
printf-ing (which rounds) as you do in this simple program you won't
notice the error, but if you try actual amount calculations you will
often get (slightly) wrong results; FAQ 14.1 at the usual places and
http://www.eskimo.com/~scs/C-faq/top.html .

You can make this rarer, but not eliminate it entirely, by using the
greater precision (on most systems) of double instead of float. The
real solution for most money computations is to use (decimally) scaled
integers -- e.g. for US compute in cents but output in dollars.

Incidentally your values for EUR and GBP are _way_ wrong. But as far
as the computer and C are concerned any representable value is fine.

<snip> printf("Choose the number beside the currency to display its US dollar
equivalent. Press ESC to exit.\n");
status = scanf("%s", &choice); /*Controls the status of the loops*/
As already said, either %c for a char or %s for a sufficiently large
string (= array of char) or %<limit-1>s for a limited size string. But
note that either will leave the newline character required to
terminate input on nearly all systems 'pending' for the next scanf,
and if that next scanf is %c it will get only the newline; FAQ 12.18.
while (status != 66)
The return value of *scanf() is the number of values converted and
stored or EOF (a negative value normally -1) if input failure before
any conversion. Your format string begins with and contains only one
conversion specifier so the only possible returns are 1 and EOF.
{
if (choice == '1') /*If user chooses currency 1*/
printf("The equivalent is %.2f \n", EUR); /*then display the
equivalent rate for EUR*/
else if (choice == '2') /*If user chooses currency 2*/
printf("The equivalent is %.2f \n", GBP); /*then display the
equivalent rate for GBP*/
else if (choice == '3') /*If user chooses currency 3*/
printf("The equivalent is %.2f \n", CAD); /*then display the
equivalent rate for CAD*/
else if (choice == '4') /*If user chooses currency 4*/
printf("The equivalent is %.2f \n", JPY); /*then display the
equivalent rate for JPY*/
else if (choice == '5') /*If user chooses currency 5*/
printf("The equivalent is %.2f \n", NZD); /*then display the
equivalent rate for NZD*/
else if (choice != '1', '2', '3', '4', '5')
This doesn't do what you want. It first evaluates choice != '1', which
will necessarily be true (1) since if choice == '1' the first if above
would have been taken instead and we would never reach this test; then
it discards that true, evaluates '2' and discards that, same for '3'
and '4', then evaluates '5' and if it is true (nonzero) which it is
takes this if -- always.

You don't need to check here for not 1 2 3 4 5, since this test is not
even reached if any of the if's above was satisified. You may want to
test for != ESC, although I would rearrange instead, see below.

If you _did_ need to test for not 1 2 3 4 5 the most direct way is
if( choice != '1' && choice != '2' && etc. )

Or there's a library function you can use to help you
#include <string.h>
if( strchr ("12345", choice) == NULL ) /* char not in given list */
printf("Invalid entry. Try again.\n"); /*Prompt the user to input a
valid choice*/
printf("\n");
printf("Here is a list of five non-US currencies: \n"); /*Display
five currency choices*/
printf("1-EUR\n");
printf("2-GBP\n");
printf("3-CAD\n");
printf("4-JPY\n");
printf("5-NZD\n");
printf("\n");
printf("Choose the number beside the currency to display its US dollar
equivalent. Press ESC to exit.\n");
status = scanf("%s", &choice);
Your indentation indicates that you may think at least the first three
printf's and maybe the 7 more and scanf are part of the body of the
if-choice-not-digit. They aren't. The body of an if is only a single
statement; if you want multiple statements you must combine them into
a compound statement by surrounding them with braces { }.

But if you do make the printf's and scanf part of only this branch,
then your other 5 (good) branches above will loop forever because you
will effectively be left with
status = scanf ... /* result true */
while (status != 66) {
if( choice == good ) printf ...;
....
/* status and choice haven't changed */
}
else if (choice == 27)
exit(EXIT_SUCCESS)
This is a syntax error because you have no if(s) to match it with,
because of the intervening nonconditional printf's and scanf.
Even if you did the exit 'statement' (really exit function call which
is an expression statement) needs a semicolon to terminate it.

And as CBFalconer noted, on Windows ESC is unusable. Even on some
other systems where you _can_ input it, it's not a good choice because
it is special in the most common terminal (and keyboard) emulations
(xterm/vtnnn) and can be entered 'accidentally' by typing something
that isn't labelled ESC and/or getting used to entering it explicitly
may lead to incorrect results with other programs. It would be better
to use '0' or '9' or 'X' or somesuch; or (my personal usual favorite)
an empty input line.
}
}

If you can help tonight, I will be forever indebted to you!


Instead of trying to distinguish 'all other cases not yet handled',
it's simplest to do all specific cases first and then 'anything else':

/* if you want the prompt repeated every time */
while( printf (promptstring), scanf (" %c", &choice) > 0 ) {
if( choice == '1' ) ... do case 1
else if( choice == '2' ) ... do case 2
...
else if( choice == 'X' ) exit(EXIT_SUCCESS);
else printf ("Invalid input, try again\n");
}
/* exit loop if EOF or error on input; you may want to
distinguish these, or handle both as bad */

/* if you want the prompt repeated only for bad input */
printf (promptstring);
while( scanf (" %c", &choice) > 0 ) {
... as above except
else printf ("Invalid input, try again\n" promptstring );
}

The scanf space-%c to &character is a trick that at least skips a
left-pending newline but instead has the limitation (problem) that it
will skip (and thus keep inputting without reprompting) all whitespace
including unlimited newlines. This is not a good user interface, but
something better would clutter up the logic I am demonstrating.

Where you are selecting a scalar integer value among constant
alternatives as here, you can use switch instead of if/else:

while( scanf (" %c", &choice) > 0 ) {
switch (choice) {
case '1': ... do case 1; break;
case '2': ... do case 2; break;
...
default: printf ("Invalid input etc.");
}
}
(In this specific case the { } for the loop body aren't required since
the switch statement with all the alternatives in its body is a single
statement, but taking advantage of this may be confusing.)

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #6

P: n/a
In article <ah********************************@4ax.com>,
Dave Thompson <da*************@worldnet.att.net> wrote:
:Floating-point numbers on most (all?) computers today are binary and
:cannot exactly represent most decimal fractions.

I -think- I heard that the IBM AS400 series has true fixed-point decimal
artithmetic in hardware, and that it has a non-trivial following
in financial applications because of that.
--
csh is bad drugs.
Nov 14 '05 #7

P: n/a
On 14 Feb 2005 15:35:32 GMT, in comp.lang.c , ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
In article <ah********************************@4ax.com>,
Dave Thompson <da*************@worldnet.att.net> wrote:
:Floating-point numbers on most (all?) computers today are binary and
:cannot exactly represent most decimal fractions.

I -think- I heard that the IBM AS400 series has true fixed-point decimal
artithmetic in hardware, and that it has a non-trivial following
in financial applications because of that.


AFAIR this was a feature of the COBOL compiler, but AS400s do have a
packed-decimal format.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #8

P: n/a

In article <gq********************************@4ax.com>, Mark McIntyre <ma**********@spamcop.net> writes:
On 14 Feb 2005 15:35:32 GMT, in comp.lang.c , ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
I -think- I heard that the IBM AS400 series has true fixed-point decimal
artithmetic in hardware, and that it has a non-trivial following
in financial applications because of that.

It's possible that the original CISC AS/400s had hardware packed-
decimal support, like the S/390 and its predecessors. However,
according to various IBM insiders who've answered some AS/400
architecture questions for me of late, the "PowerAS" processors in
the RISC AS/400s (which are the only ones made now) are just PowerPCs
with some VMM tweaks for the 400's idiosyncratic memory architecture.
No hardware packed-decimal support, in other words.
AFAIR this was a feature of the COBOL compiler, but AS400s do have a
packed-decimal format.


COBOL does indeed require support for packed-decimal fixed-point
arithmetic, and so COBOL/400 supports it. I don't remember whether
MI, the 400's pseudo-assembly language, has packed-decimal
operations. The docs are online at IBM somewhere, if anyone's
curious.

--
Michael Wojcik mi************@microfocus.com

The lark is exclusively a Soviet bird. The lark does not like the
other countries, and lets its harmonious song be heard only over the
fields made fertile by the collective labor of the citizens of the
happy land of the Soviets. -- D. Bleiman
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.