• C

Currency convertor - can't get exit to work

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

}
ghost8067Asked:
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.

RichieHindleCommented:
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

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
ghost8067Author Commented:
Thanks so much. It works like a charm.
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.