Money Conversion problem

Posted on 2006-04-03
Last Modified: 2010-04-15
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
  *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.*/
           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*/


Question by:cricket9601
    LVL 24

    Accepted Solution

    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


    LVL 2

    Assisted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now