We help IT Professionals succeed at work.

Money Conversion problem

on
Medium Priority
245 Views
I am writing a currency conversion program.  I need to print five currencies from one US Dollar amount.  Here is what I have.  I have tested letter input and need to test negative numbers.  What is wrong?

#include <stdio.h>       /*Use this .h file for input, output and get; standard library for programs functional use*/
#include <ctype.h>       /*Use this .h file to test user input*/

/* print Money Conversion
*Cricke9601
*Last Update: 4/3/2006

*The purpose of this program is for a user to convert US Dollars to

*Euro Dollars: conversion rate ==.833678
*Canada Dollars:conversion rate ==1.167
*Japanese Yen:conversion rate ==117.775
*Switzerland Francs:conversion rate ==1.3025
*Australia Dollars:conversion rate ==1.39567

*Version 2.0--user conversion:
*/

main()                                    /*declares the start of the function*/
{                                    /*begin local variable*/
/**/
char cResponse = '\0';

printf("\t\t\t\tCurrency Conversion\n\n\n\n");            /*tabs to center then displays title and skips four lines*/

printf("Welcome to the Currency Conversion Program, Version 2.0\n\n"); /*welcome user line*/

printf("\nPlease enter the amount of money you want to convert.\n\n");   /*prompts user to enter \$amount*/
printf("\nYour amount must be in numbers: \n\n\n\n");

scanf("%s", &cResponse);

if (isdigit (cResponse) )                        /*Create if,else stucture and input val; no need to surround isdigit function*/
printf("\nThank you\n\n\n");                      /*with parentheses as long as function returns Boolean, true or false, value.*/

else
printf("\nYou did not enter a valid number\n");      /*if entered data is not a number, print this*/

printf("%s %.2f %s", "US Dollar equals", %cResponse*.833678, "Euro Dollars\n\n");     /*displays entered dollars == Euro*/
printf("%s %.2f %s", "US Dollar equals", %cResponse*1.167, "Canada Dollars\n\n");   /*displays entered dollars == Canadian*/
printf("%s %.2f %s", "US Dollar equals", %cResponse*117.775, "Japanese Yen\n\n");   /*displays entered dollars == Japanese*/
printf("%s %.2f %s", "US Dollar equals", %cResponse*1.3025, "Switzerland Francs\n\n");  /*displays entered dollars == Swizerland*/
printf("%s %.2f %s", "US Dollar equals", %cResponse*1.39567, "Australia Dollars\n\n\n\n");   /*displays entered dollars == Australian*/

printf ("Press any key to continue...\n\n");      /*displays end of program and double space*/

getch ();                              /*wait for user entry*/
return 0;

}                                    /*end local variable*/

Comment
Watch Question

View Solutions Only

CEO/Programmer
CERTIFIED EXPERT
Commented:
char != *char

So this code:

char cResponse = '\0';
scanf("%s", &cResponse);

Can not work and isdigit works exactly on one character a time, but you like to have
a character arrray (or string)

So you have to have something like

char response[SOME_LIMIT];
scanf is not a good idea for interactive input, but in the case you are careful with it
that's usualy beyone the beginner level.

For getting input from the user fgets it fine especially if you want char *

So the line should read
fgets (response, SOME_LIMIT, stdin);

if you want to check wheter the input consists just of digits (or can be read as an integer) then you can use the atol or atoi function.

You can check yourself with isdigit but then you have to run a loop over the input
something aling this lines

size_t response_len = strlen(response);
int result = 1;
for (int i = 0; i < response_len && result ; ++i){
if (! isdigit(response[i])){
result = 0;
}
}

if (result) // everythings fine should be a number

Regards
Friedrich

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
>    printf("%s %.2f %s", "US Dollar equals", %cResponse*.833678, "Euro Dollars\n\n");     /*displays entered dollars == Euro*/

%cResponse is not valid ...
use just cResponse
the line should look like
printf("%s %.2f %s", "US Dollar equals", cResponse*.833678, "Euro Dollars\n\n");     /*displays entered dollars == Euro*/
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Limited access with a free account allows you to:

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile