473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help! Esc button won't exit program

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
8 5350
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
"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
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
"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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
0
by: tbatwork828 | last post by:
If you were like me trying to figure out how to launch context sensitive help topic by the context id, here is the link: http://weblogs.asp.net/kencox/archive/2004/09/12/228349.aspx and if...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
2
by: BT Openworld | last post by:
I have just had to upgrade to Access 2003 as Access 97 EMail (SendObject) doesn't work when loaded on Windows XP. I'm finding my way around Access 2003 but my biggest problem is getting...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.