newbie having trouble with conversion program

S

sfraney

Here is what I have, it is just a very simple C program that need to
display 1 country's currency and and exit option. when the user hits
the 1 key to select the country, the program should prompt the user for
the amount to convert. My problem is when the user hits a non-numeric
key. The output is scrolling text and no way out.

Here is what I have:

#include <stdio.h>
#include <stdlib.h>

#define Mexican_Peso_rate 11.19; /*Mexican Peso rate*/
#define British_Pound_rate 0.53; /*British Pound rate*/
#define Fiji_Dollar_rate 1.67; /*Fiji Dollar rate*/
#define Canadian_Dollar_rate 0.66; /*Canadian Dollar rate*/
#define Euro_rate 0.78; /*Euro rate*/

void dashbar()
{
int i = 1;

while (i < 50)
{
putchar ('-');
i = i + 1;
}
putchar ('\n');
}

int disp_menu()
{
int choice;

printf("----------------------------------------\n\n");
printf(" Sean's Currency Conversion Program\n");
printf("----------------------------------------\n\n");
printf("1) Mexican Peso \n");
printf("2) Get me out of Here! \n");
printf(" \n\n");
printf("Select 1 or 2: ");

scanf("%d", &choice);

while (choice > 2)
{
printf("What the heck are doing?!? Please Enter 1 or 2:
");
scanf("%i", &choice);
}

return choice;
}

int main()
{
/*Declare floats*/

float Mexican_Peso; /*Mexican Peso*/
float USD; /*US Dollar*/
int choice;
choice = disp_menu();

while (1)
{
switch (choice)
{
case 1:
printf("Enter the amount: ");
scanf("%f",&Mexican_Peso);

/*Conversion Calculation 1*/
USD = Mexican_Peso / Mexican_Peso_rate;
printf ("
\n\n");
printf("%3.2f mexican peso = $%3.2f US
dollars\n", Mexican_Peso, USD);
printf("
\n\n");
break;
case 2:
printf("Exiting the program....\n");
printf("Goodbye!\n");
exit(0);
break;
}
choice = disp_menu();
}
getch();
return 0;
}
 
R

Richard Bos

Here is what I have, it is just a very simple C program that need to
display 1 country's currency and and exit option. when the user hits
the 1 key to select the country, the program should prompt the user for
the amount to convert. My problem is when the user hits a non-numeric
key. The output is scrolling text and no way out.

<http://www.eskimo.com/~scs/C-faq/q12.19.html>
<http://www.eskimo.com/~scs/C-faq/q12.20.html>
<http://www.eskimo.com/~scs/C-faq/s12.html>
<http://www.eskimo.com/~scs/C-faq/top.html>

HTH; HAND.

Richard
 
S

sfraney

Hmmm, thanks Richard. I have the While in place to make sure the user
only puts 1 or 2, as far the numbers go but I cannot figure out how to
utilize that to also say no alphas allowed. Any hints?
 
F

Fred L. Kleinschmidt

Here is what I have, it is just a very simple C program that need to
display 1 country's currency and and exit option. when the user hits
the 1 key to select the country, the program should prompt the user for
the amount to convert. My problem is when the user hits a non-numeric
key. The output is scrolling text and no way out.

Here is what I have:

#include <stdio.h>
#include <stdlib.h>
(snip)
int disp_menu()
{
int choice;

printf("----------------------------------------\n\n");
printf(" Sean's Currency Conversion Program\n");
printf("----------------------------------------\n\n");
printf("1) Mexican Peso \n");
printf("2) Get me out of Here! \n");
printf(" \n\n");
printf("Select 1 or 2: ");

scanf("%d", &choice);

while (choice > 2)
{
printf("What the heck are doing?!? Please Enter 1 or 2:
");
scanf("%i", &choice);
}

return choice;
}
(snip)

scanf() returns an int that tells you how many items were successfully
read. If the user types something that begins with a non-number, scanf
returns zero, and that non-number remains in the input stream. Therefore
the next time through the loop, it tries to read the same thing, fails,
tries, fails, etc.

You should check the return value of scanf to see if you successfully
read a number.
 
D

Dan P.

Hmmm, thanks Richard. I have the While in place to make sure the user
only puts 1 or 2, as far the numbers go but I cannot figure out how to
utilize that to also say no alphas allowed. Any hints?


You can use a function like fgets() to get the input from stdin. Then you
can use strtol() to convert that into a long. If the user typed in "x" for
instance, it would error out (i.e. return a 0).



Dan
 
S

SM Ryan

#
# # > Hmmm, thanks Richard. I have the While in place to make sure the user
# > only puts 1 or 2, as far the numbers go but I cannot figure out how to
# > utilize that to also say no alphas allowed. Any hints?
# >
#
#
# You can use a function like fgets() to get the input from stdin. Then you
# can use strtol() to convert that into a long. If the user typed in "x" for
# instance, it would error out (i.e. return a 0).

Does a string s contain an integer (with optional spaces before and after)?

char *p;
int radix = 10;
long v = strtol(s,&p,radix);
if (s==p) error("no number present");
while (*p && isspace(*p)) p++;
if (*p) error("junk after number");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top