Thread: Looping switch structure Help (newbie)

  1. #1
    Wheres the CLS?!?
    Join Date
    Dec 2005
    Posts
    2

    Looping switch structure Help (newbie)

    Hello,

    I'm doing the "currency conversion program" that you all have seen a million times already. I coded the program and it runs.
    When invalid choice is made from my "menu" it ends the program
    (pick 1-6 and someone picks 9).

    what I'm wanting to do is to loop the switch so that if user picks "9" it will printf "Please try again" and loop the menu.

    ANY help would be great because my brain is mush right now

    Code:
    #include <stdio.h>
    
    main()
    
    {
    
    	float fr[6], input2;
    	int input1;
    	
    	fr[0] = 1;         
    	fr[1] = 0.7435;    
    	fr[2] = 1.190;    
    	fr[3] = 102.1;     
    	fr[4] = 1.280;     
    	fr[5] = 23.01;     
    	
    	while ( input1 < 6 )  
    	 {      
    	 	printf("\n\t************************");     
    		printf("\n\t* Currency  Conversion *");    
    		printf("\n\t*        Ver 1.2       *");     
    		printf("\n\t************************\n");   
    		printf("\n\tSelect a currency to be converted or quit (1-6).\n");
    		printf("\n1)  Euro                   %.4f     \n", fr[1]); 
    		printf("2)  Canadian Dollar        %.3f   \n", fr[2]); 
    		printf("3)  Japanese Yen           %.1f   \n", fr[3]); 
    		printf("4)  Australian Dollar      %.3f   \n", fr[4],); 
    		printf("5)  Czech Republic Koruny  %.2f   \n", fr[5],); 
    		printf("6)  Quit\n");
    		printf("\n\n\n"); 
    		printf("\n\nSelection: ");  
    		scanf("%d", &input1);  
    			switch (input1) {
    				case 1:
    					printf("\n You have selected Euro.\n");
    					break;
    				case 2:
    					printf("\n You have selected Canadian Dollar.\n");
    					break;
    				case 3:
    					printf("\n You have selected Japanese Yen.\n");
    					break;
    				case 4:
    					printf("\n You have selected Australian Dollar.\n");
    					break;
    				case 5:
    					printf("\n You have selected Czech Republic Koruny.\n");
    					break;
    				case 6:
    					printf("\n Thank you. \n");
    					break;
    				default:
    					printf("\n Sorry that is not a valid selection.\n");		
    				}  
    		if (input1 < 6) {		
    		      printf("\n Enter amount to be converted:");
    		      scanf("%f", &input2);
    		      }
    		      else {
    		      printf("\n Hope you enjoyed my program,");
    		      input2 = 6;
    		      }  
    		
    		if (input2 <= 0.00) {      
    			printf("\n  Error: Please input a positive amount.\n");   
    			}
    	 	  else { 
    			switch (input1) {
    				case 1:
    					printf("\n %.4f Euro is equivalent to %.2f American Dollars.\n",input2,input2/fr[1]);
    					break;
    				case 2:
    					printf("\n %.3f Canadian Dollars is equivalent to %.2f American Dollars.\n",input2,input2/fr[2]);  
    					break;
    				case 3:
    					printf("\n  %.1f Japanese Yen is equivalent to %.2f American Dollars.\n",input2,input2/fr[3]);
    					break;
    				case 4:
    					 printf("\n  %.3f Australian Dollar is equivalent to %.2f American Dollars.\n",input2,input2/fr[4]);
    					break;
    				case 5:
    					printf("\n  %.2f Czech Republic Koruny is equivalent to %.2f American Dollars.\n",input2,input2/fr[5]);
    			 		break;
    			 	case 6: 
    			 		printf("\n Have a nice day!\n");
    			 		}  //end switch				
    	     	        } //end else
    		 printf("\n Press Enter key to continue......."); 
    	
    		 getchar();    
    		 printf("\n\n\n\n\n\n\n\n\n");
    	 } //end while loop
                    
    } // end main program

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just change your while loop to only match the exit condition:
    Code:
    while ( input1 != 6)
    As a side note, your while() loop is checking the value of input1 before it's set to anything. You can either initialize it to something (anything but 6) at declaration or change your loop from a while to a do-while.
    Last edited by itsme86; 12-02-2005 at 02:04 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Wheres the CLS?!?
    Join Date
    Dec 2005
    Posts
    2
    Thank you for your help!, I will also initialize the input1 at declaration.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another newbie question (switch...case)
    By Terran in forum C++ Programming
    Replies: 11
    Last Post: 05-23-2008, 04:07 PM
  2. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  3. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 12:39 AM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM