• C

Money Conversion problem

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*/




 






 
cricket9601Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fridomCEO/ProgrammerCommented:
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




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zultiCommented:
>    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*/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.