Hey guys, I need some help with looping. I am writing a currency converter (who in begining programming isn't these days) and I want to have it loop so that I can it keep asking for another entry or make it quit. Here is what I have so far:

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

/* Main Program */
int main(void)
{

float USD;
float CAD;
float EUR;
float GBP;
float CHF;
float YEN;
int input;
char q;

/*Title of program*/
printf("\t This program will convert foreign currency to US Dollars\n\n");

/*Assigning Values*/
 USD = 1.0; /* US Dollar*/
 CAD = 1.2257; /* Canadian Dollar*/
 EUR = 0.752162; /* European Euro */
 GBP = .51573; /* British Pound */
 CHF = 1.134; /* Swiss Franc*/
 YEN = 1.038; /* Japanese Yen*/

printf("Please choose form the following list of currency or press q to quit.\n"); 
printf("\n");
printf("[1] Canadian Dollar\n");
printf("[2] British Pound\n");
printf("[3] European Euro\n");
printf("[4] Swiss Franc\n");
printf("[5] Japanese Yen\n"); 
printf("\n");
scanf("%d", &input);


	/*switching statements */
	switch (input)
	{
	case 1 : 
	 printf("%1.6f Canadian Dollars is equal to %1.1f US Dollar.\n", CAD,USD);
	break;
	case 2 :
	 printf("%1.6f European Euro is equal to %1.1f US Dollar.\n", EUR,USD); 
	break;
	case 3 :
	 printf("%1.6f British Pound is equal to %1.1f US Dollar.\n", GBP,USD);
	break;
	case 4 : 
	 printf("%1.6f Swiss Franc is equal to %1.1f US Dollar.\n", CHF,USD);
	break;
	case 5 :
	 printf("%1.6f Japanese Yen is equal to %1.1f US Dollar.\n", YEN,USD); 
	break;
	}

scanf("%d");
return 0;
}

Recommended Answers

All 3 Replies

use a while loop checking if q is pressed. You havent actually used char, and are asking the user to input a letter or number, but only grabbing the number! I would convert to c++ and use iostream, I dont know the C equivalent

char input; // user will type, so input is a char!

while (input != 'q' || input != 'Q') // loops untill you put q or Q in input
{
   code here...
   I would use cin >> input;
   then a strtoi (i think that is how it is spelled) call to convert the char to a number
}

Unless you want to do precise error handling and throw a message if the user enters something other than q or Q, you can simply place the entire processing code into an infinite loop and break if scanf fails:

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

/* Main Program */
int main(void)
{
  float USD;
  float CAD;
  float EUR;
  float GBP;
  float CHF;
  float YEN;
  int input;

  /*Title of program*/
  printf("\t This program will convert foreign currency to US Dollars\n\n");

  /*Assigning Values*/
  USD = 1.0f; /* US Dollar*/
  CAD = 1.2257f; /* Canadian Dollar*/
  EUR = 0.752162f; /* European Euro */
  GBP = .51573f; /* British Pound */
  CHF = 1.134f; /* Swiss Franc*/
  YEN = 1.038f; /* Japanese Yen*/

  for (;;) {
    printf("Please choose form the following list of currency or press q to quit.\n"); 
    printf("\n");
    printf("[1] Canadian Dollar\n");
    printf("[2] British Pound\n");
    printf("[3] European Euro\n");
    printf("[4] Swiss Franc\n");
    printf("[5] Japanese Yen\n"); 
    printf("\n");
    if (scanf("%d", &input) != 1)
      break;

    /*switching statements */
    switch (input)
    {
    case 1 : 
      printf("%1.6f Canadian Dollars is equal to %1.1f US Dollar.\n", CAD,USD);
      break;
    case 2 :
      printf("%1.6f European Euro is equal to %1.1f US Dollar.\n", EUR,USD); 
      break;
    case 3 :
      printf("%1.6f British Pound is equal to %1.1f US Dollar.\n", GBP,USD);
      break;
    case 4 : 
      printf("%1.6f Swiss Franc is equal to %1.1f US Dollar.\n", CHF,USD);
      break;
    case 5 :
      printf("%1.6f Japanese Yen is equal to %1.1f US Dollar.\n", YEN,USD); 
      break;
    }
  }

  return 0;
}

That's the easiest solution. If you want something more structured and good error messages, it takes more effort, usually in the form of string input and validation followed by conversions.

Thanks for your help. This is exactly what I needed. You made it look so easy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.