#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    3
    Rep Power
    0

    while, scan loop not working


    I know I have several mistakes in this program - it is an assignment for school (I'm just starting out). Can someone PLEASE help point out the mistakes? Is there an easier way to do this?

    /*CURRENCY.c currency conversion program converts a specific amount US dollars to foreign currency equivalent*/
    #include <stdio.h>
    int main(void)
    {
    int dollar;
    float JPY;
    float RUR;
    float MXN;
    float CHF;
    float GBP;
    int currency;

    printf("CURRENCY CONVERSION\n\n\n");
    printf("You can convert your American dollars to: Japanese Yen, Russian Rubles, Mexican Pesos, Canadian Dollars or United Kingdom Pounds.\n");
    printf("Choose the currency you wish to convert to:\n");
    printf("[1] = Japanese Yen\n");
    printf("[2] = Russian Rubles\n");
    printf("[3] = Mexican Pesos\n");
    printf("[4] = Canadian Dollars\n");
    printf("[5] = United Kingdom Pounds\n");
    printf("[6] = Exit Program\n");
    printf("The currency you wish to convert to is: \n");
    scanf("%i", &currency);

    while (currency<=5)
    switch (currency)
    {
    case 1: currency = 1;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    JPY = 117.390 * dollar; /*set value for Japanese yen*/
    printf("%i dollars equals %f Japanese Yen.\n", dollar, JPY); /*prints japanese equivalent*/
    break;
    case 2: currency = 2;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    RUR = dollar * 30.51; /*set value for Russian Rubles*/
    printf("%i dollars equals %f Russian Rubles.\n", dollar, RUR); /*prints russian equivalent*/
    break;
    case 3: currency = 3;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    MXN = dollar * 10.5330; /*set value for Mexican pesos*/
    printf("%i dollars equals %f Mexican Pesos.\n", dollar, MXN); /*prints mexican equivalent*/
    break;
    case 4: currency = 4;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    CHF = dollar * 1.3320; /*set value for Canadian dollars*/
    printf("%i dollars equals %f Canadian Dollars.\n", dollar, CHF); /*prints canadian equivalent*/
    break;
    case 5: currency = 5;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    GBP = dollar * .597907; /*set value for United Kingdom pounds*/
    printf("%i dollars equals %f United Kingdom Pounds.\n", dollar, GBP); /*prints united kingdom equivalent*/
    break;
    case 6: currency = 6;
    printf("you have choosen to stop. Thank you.\n");
    break;
    }

    getChar(); /*creates a pause*/
    return 0;
    }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    3
    Rep Power
    0

    while loop not working - more information


    I realize I did not give enough information about what I'm looking for. The assignment is to write a program that will convert any amount of american dollars into a choice of 1 of 5 different currencies. The program should continue over and over asking the user for a new selection until they decide to quit. It should also be able to tell them when they make an incorrect selection.
    Right now the way it stands,
    the program compiles but it gets "stuck" in whatever type of currency is first selected - and you can't exit out of it nor can you move into the other loops.
    Lisa.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    13
    Rep Power
    0
    well im not sure if I fixed it right....but it compiled without errors...it crashed when I put in numbers...mybe I didnt put in the numbers right...lol....here is the code I fixed...

    #include <stdio.h>
    int main(void)
    {
    int dollar;
    float JPY;
    float RUR;
    float MXN;
    float CHF;
    float GBP;
    int currency;

    printf("CURRENCY CONVERSION\n\n\n");
    printf("You can convert your American dollars to: Japanese Yen, Russian Rubles, Mexican Pesos, Canadian Dollars or United Kingdom Pounds.\n");
    printf("Choose the currency you wish to convert to:\n");
    printf("[1] = Japanese Yen\n");
    printf("[2] = Russian Rubles\n");
    printf("[3] = Mexican Pesos\n");
    printf("[4] = Canadian Dollars\n");
    printf("[5] = United Kingdom Pounds\n");
    printf("[6] = Exit Program\n");
    printf("The currency you wish to convert to is: \n");
    scanf("%i", "Ący");

    while (currency<=5)
    switch (currency)
    {
    case 1: currency = 1;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    JPY = 117.390 * dollar; /*set value for Japanese yen*/
    printf("%i dollars equals %f Japanese Yen.\n", dollar, JPY); /*prints japanese equivalent*/
    break;
    case 2: currency = 2;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    RUR = dollar * 30.51; /*set value for Russian Rubles*/
    printf("%i dollars equals %f Russian Rubles.\n", dollar, RUR); /*prints russian equivalent*/
    break;
    case 3: currency = 3;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    MXN = dollar * 10.5330; /*set value for Mexican pesos*/
    printf("%i dollars equals %f Mexican Pesos.\n", dollar, MXN); /*prints mexican equivalent*/
    break;
    case 4: currency = 4;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    CHF = dollar * 1.3320; /*set value for Canadian dollars*/
    printf("%i dollars equals %f Canadian Dollars.\n", dollar, CHF); /*prints canadian equivalent*/
    break;
    case 5: currency = 5;
    printf("How many dollars do you have: \n");
    scanf("%i", &dollar);
    GBP = dollar * .597907; /*set value for United Kingdom pounds*/
    printf("%i dollars equals %f United Kingdom Pounds.\n", dollar, GBP); /*prints united kingdom equivalent*/
    break;
    case 6: currency = 6;
    printf("you have choosen to stop. Thank you.\n");
    break;
    }

    int getChar(); /*creates a pause*/
    return 0;
    }

    see no errors linking.....

    Linking...

    Q.exe - 0 error(s), 0 warning(s)
    if you looked closely I only fixed 2 or 3 items...im not sure if they are right...but try it.:-)
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Typically, posts that are homework/classwork are frowned upon because you should be going to your instructor and peers first. They should be your first and primary resources during your learning experiences in school.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    3
    Rep Power
    0
    well, thank you so much for your help!
    Obviously the instructor has not responded in a manner that I understand or I would not have been on this site looking for help. As for peers - I did post it in our group (not that I have to defend myself to you) and they have not been able to figure out what I'm doing wrong either.
  10. #6
  11. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Look, you need to calm down some. The reason behind not coming here is because if someone here writes this code for you, it is most likely going to contain things that you haven't covered yet and will blatenly be obvoius to your instructor that you are getting code from someone. The code is simple, here is an example that I have written in C and compiled and run using VC++.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define YEN		117.390
    #define RUB		30.51
    #define PES		10.533
    #define CAD		1.332
    #define GBP		0.597907
    #define USD		1
    
    #ifdef WIN32
    #	include <conio.h>
    #	define CLEAN "cls"
    #	define doPause getch
    #else
    #	define CLEAN "clear"
    #	define doPause getchar
    #endif
    
    #ifndef MAXBUFLEN
    #	define MAXBUFLEN 50
    #endif
    #define QUIT_OPTION 6
    #define MENU_ALLOC_ERROR -1
    
    int printMenu();
    void printDollarRequest();
    void printErr();
    void printMenuErr();
    void printContinue();
    
    int main(void) {
    	int iMenuEntry = 0; /* menu entry */
    	char *cEntry; /* typical buffer for entry */
    	double fCurrency = 0.0; /* converted amount */
    
    	cEntry = (char *)malloc(MAXBUFLEN + 1);
    	if (NULL == cEntry) {
    		printf("The application was unable to allocate it's needed memory.");
    		return (1);
    	}
    
    	while (QUIT_OPTION != (iMenuEntry = printMenu())) {
    		if (MENU_ALLOC_ERROR == iMenuEntry) {
    			printf("The printMenu function was unable to allocate it's needed memory.");
    			return (1);
    		}
    		switch (iMenuEntry) {
    		case 1: /* yen */
    			printDollarRequest();
    			fgets(cEntry, MAXBUFLEN, stdin);
    			if (0 == atoi(cEntry)) {
    				printErr();
    				break;
    			}
    			fCurrency = atoi(cEntry) * YEN;
    			printf("%u US Dollars converts to %.2f Yen\n", atoi(cEntry), fCurrency);
    			printContinue();
    			break;
    		case 2: /* rubles */
    			printDollarRequest();
    			fgets(cEntry, MAXBUFLEN, stdin);
    			if (0 == atoi(cEntry)) {
    				printErr();
    				break;
    			}
    			fCurrency = atoi(cEntry) * RUB;
    			printf("%u US Dollars converts to %.2f Rubles\n", atoi(cEntry), fCurrency);
    			printContinue();
    			break;
    		case 3: /* pesos */
    			printDollarRequest();
    			fgets(cEntry, MAXBUFLEN, stdin);
    			if (0 == atoi(cEntry)) {
    				printErr();
    				break;
    			}
    			fCurrency = atoi(cEntry) * PES;
    			printf("%u US Dollars converts to %.2f Pesos\n", atoi(cEntry), fCurrency);
    			printContinue();
    			break;
    		case 4: /* cad */
    			printDollarRequest();
    			fgets(cEntry, MAXBUFLEN, stdin);
    			if (0 == atoi(cEntry)) {
    				printErr();
    				break;
    			}
    			fCurrency = atoi(cEntry) * CAD;
    			printf("%u US Dollars converts to %.2f Canadian Dollars\n", atoi(cEntry), fCurrency);
    			printContinue();
    			break;
    		case 5: /* gbp */
    			printDollarRequest();
    			fgets(cEntry, MAXBUFLEN, stdin);
    			if (0 == atoi(cEntry)) {
    				printErr();
    				break;
    			}
    			fCurrency = atoi(cEntry) * GBP;
    			printf("%u US Dollars converts to %.2f Pounds\n", atoi(cEntry), fCurrency);
    			printContinue();
    			break;
    		case QUIT_OPTION: /* quit */
    			printf("\nApplication ended.\n");
    			break;
    		default: /* invalid input */
    			printMenuErr();
    			break;
    		}
    	}
    	if (NULL != cEntry)
    		free(cEntry);
    	return (0);
    
    }
    
    int printMenu() {
    	char *cEntry = NULL;
    	int iRetval = 0;
    
    	/* alloc a buffer for entry */
    	cEntry = (char *)malloc(MAXBUFLEN + 1);
    	if (NULL == cEntry) 
    		return (MENU_ALLOC_ERROR);
    
    	/* clean the screen */
    	system(CLEAN);
    	printf("Currency Convertor\n");
    	printf("------------------\n");
    	printf("To use: select the currency to convert to \n");
    	printf("from the menu listed below and enter your \n");
    	printf("dollar amount below.\n\n");
    	printf("1) Japanese Yen\n");
    	printf("2) Russian Rubles\n");
    	printf("3) Mexican Pesos\n");
    	printf("4) Canadian Dollars\n");
    	printf("5) United Kingdom Pounds\n");
    	printf("\nQ) Quit\n");
    	printf("Enter your selection:");
    	fgets(cEntry, MAXBUFLEN, stdin);
    
    	if ('q' == tolower(cEntry[0])) {
    		if (cEntry != NULL)
    			free(cEntry);
    		return (QUIT_OPTION);
    	} else {
    		iRetval = atoi(cEntry);
    		if (cEntry != NULL)
    			free(cEntry);
    		return (iRetval);
    	}		
    }
    
    void printDollarRequest() {
    	system(CLEAN);
    	printf("Please enter the dollar amount you wish to convert: ");
    }
    
    void printErr() {
    	printf("Invalid entry, please enter a numeric value greater than zero.\n");
    	printContinue();
    }
    
    void printMenuErr() {
    	printf("Invalid entry, please enter a valid menu option.\n");
    	printContinue();
    }
    
    void printContinue() {
    	printf("Press any key to continue.");
    	doPause();
    }
    [edit]made it cross platform from previous function. Tested with gcc in RH 7.2[/edit]
    Last edited by Onslaught; June 30th, 2003 at 08:56 AM.

IMN logo majestic logo threadwatch logo seochat tools logo