[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Currency convertor - can't get exit to work

Posted on 2006-04-08
2
Medium Priority
?
283 Views
Last Modified: 2010-05-18
I am having trouble getting an if statement to recognize that the letter q or Q has been entered and stop execution. I have put a comment in the program below where I am having trouble. I put a printf in to show me the value in str, and it does display a q, but does not seem to enter the if statement and execute the exit. Any assistance would be greatly appreciated. When I do enter a "q", I fall through to the else. This is fairly urgent, as I have to complete this and a cople of other changes before Monday.
Thanks,
Jon

/* Currency Converter Program */
/* This program displays 5 currencies and their equivalents for one US dollar */

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


/*Function prototype*/
float getCurrencyValue(float, float);

/* define constant conversion values  */
#define Pound .56974
#define Yen 116.7600
#define Ruble 27.7330
#define Euro .82176
#define Yuan 8.0367


/* Begin main program  */
main()
{

  /* Declaration and Initialization of Variables to be Utilized */
  float fltPoundValue = 0.00;
  float fltYenValue = 0.00;
  float fltRubleValue = 0.00;
  float fltEuroValue = 0.00;
  float fltYuanValue = 0.00;
  float iamount = 0.00;
  float fltValue = 0.00;
  int valid_amount;
  int iresponse;
  char str[1];
  char strAmt[10];




  /* write title line */

  printf("\t\t\t\tCurrency Convertor \n\n\n");

  /* write and format 5 currency choices and selection numbers */

  printf("\t\t\t 1. United Kingdom Pound \t ");
  printf("\n\n\t\t\t 2. Japanese Yen \t\t ");
  printf("\n\n\t\t\t 3. Russian Ruble \t\t ");
  printf("\n\n\t\t\t 4. Euro \t\t\t ");
  printf("\n\n\t\t\t 5. Chinese Juan \t\t ");



  {
    int valid_choice = 0; /* create variable to use for while loop*/
    while (valid_choice == 0)
    /* Begin While Loop  */
    {
      printf(
        " \n\n\n  Please select which currency equivalent you would like to see: "); /* Ask user for currency selection  */
      scanf("%s", str); /* Get currency selection from user  */
      iresponse = atoi(str);
      printf("\n  str is %s \n\n",str);                             /* display added to show me value of str  */
      if ((str == "q") || (str == "Q"))                          /*These 2 lines are my issue. Do not execute */
      exit(0);
      else if ((iresponse == 1) || (iresponse == 2) || (iresponse == 3) ||  /* check for valid selection  */
      (iresponse == 4) || (iresponse == 5))
        break; /* When selection valid, leave loop  */
      else   /* This else executed when invalid selection */
        printf("\n  Please enter a value between 1 and 5  ");
        continue;    /*  Loop resumes  */

    }

    {
      /* This section validates an valid amount is entered  */

      valid_amount = 0; /* create variable to use for while loop*/


      while (valid_amount == 0)   /* Begin While Loop  */
      {
        printf("\n  Please enter the amount of foreign currency you have:  ");   /* Ask user for amount to convert  */
        scanf("%s", strAmt); /* Get currency selection from user  */
        iamount= atoi(strAmt);
        if (iamount > 0.00){  /* check for valid selection  */
          valid_amount = 1;
          break; /* When amount valid, leave loop  */
          }
        else   /* This else executed when invalid amount */
          printf("\n  You have entered an invalid value. Please enter a numeric value  ");
        continue; /*  Loop resumes  */
      }
    }

  }

  /* This switch checks the value the user selected for currency type  */

  switch (iresponse)
  {

    case 1:
      fltPoundValue = getCurrencyValue(iamount, Pound); /* call getcurrency function passing amount and conversion factor and return result */
      printf(
        "\n\n   -------------------------------------------------------------------------\n\n"); /* print separator line  */
      printf(
        "\n  Your British Pounds convert to $%.2f in United States Dollars. \n\n", fltPoundValue); /* Print conversion to Pounds*/
      break;   /* leave loop  */

    case 2:
      fltYenValue = getCurrencyValue(iamount, Yen); /* call getcurrency value
        function passing amount and conversion factor and return result */
      printf(
        "\n\n   -------------------------------------------------------------------------\n\n"); /* print separator line  */
      printf(
        "\n  Your Japenese Yen convert to $%.2f in United States Dollars. \n\n",fltYenValue);   /* Print conversion to Yen*/
      break;   /* leave loop  */

    case 3:
      fltRubleValue = getCurrencyValue(iamount, Ruble); /* call getcurrency value function passing amount and conversion factor and return result */
      printf(
        "\n\n   -------------------------------------------------------------------------\n\n"); /* print separator line  */
      printf(
        "\n  Your Russian Rubles convert to $%.2f in United States Dollars. \n\n",fltRubleValue);   /* Print conversion to Rubles*/
      break;  /* leave loop  */

    case 4:
      fltEuroValue = getCurrencyValue(iamount, Euro); /* call getcurrency value
        function passing amount and conversion factor and return result */
      printf(
        "\n\n   -------------------------------------------------------------------------\n\n"); /* print separator line  */
      printf("\n  Your Euros convert to $%.2f in United States Dollars. \n\n",fltEuroValue);  /* Print conversion to Euros*/
      break;   /* leave loop  */

    case 5:
      fltYuanValue = getCurrencyValue(iamount, Yuan); /* call getcurrency value
        function passing amount and conversion factor and return result */
      printf(
        "\n\n   -------------------------------------------------------------------------\n\n"); /* print separator line  */
      printf(
        "\n  Your Chinese Juan convert to $%.2f in United States Dollars. \n\n",fltYuanValue);  /* Print conversion to Yuan*/
      break;  /* leave loop  */



  }


}

//function definition
float getCurrencyValue(float iamount, float currency)
{

  return iamount / currency; /* calculate the value and return it to the main program  */

}
0
Comment
Question by:ghost8067
2 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 2000 total points
ID: 16408063
Replace this:

    if ((str == "q") || (str == "Q"))

with either this:

    if ((str[0] == 'q') || (str[0] == 'Q'))

or this:

   if ((strcmp(str, "q") == 0) || (strcmp(str, "Q") == 0))

The former compares a character with a character; the latter compares a string with a string.  Your code compares a pointer with a different pointer, which will always give false.
0
 

Author Comment

by:ghost8067
ID: 16408121
Thanks so much. It works like a charm.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.
Suggested Courses

834 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