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! 8 5161
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
"drose0927" writes: equivalent. Press ESC to exit.\n"); status = scanf("%s", &choice);
%s says read a string. But choice is a char.
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
"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!
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
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.
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 =----
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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?...
|
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...
|
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:
...
|
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",...
|
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...
|
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...
|
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...
|
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...
|
by: trunxnirvana007 |
last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more:...
|
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...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
| |