Solved

Exit from program when number is not in the loop

Posted on 2004-08-18
5
264 Views
Last Modified: 2010-04-15
Now what am I doing wrong? I had this so that if I entered in any other number except for 0-5 that it would ask me to select another number? I used the while loop the first time and now I cannot get it back. I never should have changed it around the second time. Thanks

/*Select a currency conversion type by code number 8/11/04*/
#include <stdio.h>                            
int main(void)
{

int EUR,GBP,JPY,CAD,RUR,US;  
float fCAD,fEUR,fGBP,fJPY,fRUR,fUS;
char option;
  EUR=1;
  GBP=2;
  JPY=3;
  CAD=4;
  RUR=5;

   
  fEUR=0.829643f;              
  fGBP=0.548242f;
  fJPY=110.631f;
  fCAD=1.31751f;
  fRUR=29.1800f;                                                                                  
   fUS=1.00f;
  printf("Select a currency type\n\n");
  printf("1=European Euro 2=United Kingdom Pound 3=Japanese Yens 4=Canadian Dollars 5=Russian Rubles\n\n");
  printf("Enter 0 to exit\n\n");

while(1){
printf("Enter your choice:\n\n");


option=getch();
switch(option)
{
case '1':
printf("The selected currency type is the European Euro of %f equivalent to %f US  Dollar\n",fEUR,fUS);
break;
case '2':
printf("The selected currency type is the United Kingdom Pound of %f equivalent to %f US  Dollar\n",fGBP,fUS);
break;
case'3':
printf("The selected currency type is the Japanese Yens of %f equivalent to %f US  Dollar\n",fJPY,fUS);
break;
case'4':
printf("The selected currency type is the Canadian Dollars of %f equivalent to %f US  Dollar\n",fCAD,fUS);
break;
case '5':
printf("The selected currency type is the Russian Rubles of %f equivalent to %f US  Dollar\n",fRUR,fUS);
break;
default:
return (0);
break;
}
}
getch();
return 0;                          
}
0
Comment
Question by:RebeccaFusco
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11837380
Change  the default case :
>>
default:
return (0);  //here return 0 returns for main and the program ends
break;
<<

to:

default:
break;

and add a case for '0'

case '0':exit(0);//or return 0;

case '0' should be before the default case.
0
 
LVL 7

Expert Comment

by:aib_42
ID: 11837394
Your 'default' case, which is used when the user does not enter 1, 2, 3, 4 or 5 has "return 0;", which returns from main, effectively terminating your program.

Make the 'default' case do nothing, ie. remove the "return(0);" line. Also make sure that you have a "case '0'" with return(0); so the program can exit :).
0
 

Author Comment

by:RebeccaFusco
ID: 11837466
I think I did the right thing and it still will not tell me that a wrong selection is invalid? Thanks

/*Select a currency conversion type by code number 8/11/04*/
#include <stdio.h>                            
int main(void)
{

int EUR,GBP,JPY,CAD,RUR,US;  
float fCAD,fEUR,fGBP,fJPY,fRUR,fUS;
char option;
   EUR=1;
   GBP=2;
   JPY=3;
   CAD=4;
   RUR=5;
 
   
   fEUR=0.829643f;              
   fGBP=0.548242f;
   fJPY=110.631f;
   fCAD=1.31751f;
   fRUR=29.1800f;                                                                                  
    fUS=1.00f;
   printf("Select a currency type\n\n");
   printf("1=European Euro 2=United Kingdom Pound 3=Japanese Yens 4=Canadian Dollars 5=Russian Rubles\n\n");
 
while(1){

printf("Enter your choice:\n\n");

option=getch();
switch(option)
{
case '1':
printf("The selected currency type is the European Euro of %f equivalent to %f US  Dollar\n",fEUR,fUS);
break;
case '2':
printf("The selected currency type is the United Kingdom Pound of %f equivalent to %f US  Dollar\n",fGBP,fUS);
break;
case'3':
printf("The selected currency type is the Japanese Yens of %f equivalent to %f US  Dollar\n",fJPY,fUS);
break;
case'4':
printf("The selected currency type is the Canadian Dollars of %f equivalent to %f US  Dollar\n",fCAD,fUS);
break;
case '5':
printf("The selected currency type is the Russian Rubles of %f equivalent to %f US  Dollar\n",fRUR,fUS);
break;
case '0':
printf("exiting...\n");
return (0);
break;
default:


}
}
getch();
return 0;                          
}
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 250 total points
ID: 11837487
If you want a message,use:
default:
printf("Invalid Choice\n");
break;
0
 

Author Comment

by:RebeccaFusco
ID: 11837523
Roger that :-) Thank you so..much. I do not know how I did it before but it was not like you have it. Somehow I used a loop but your way is great and so simple. The simple things slways seem to be the hardest things for me  to do.    

/*Select a currency conversion type by code number 8/11/04*/
#include <stdio.h>                            
int main(void)
{

int EUR,GBP,JPY,CAD,RUR,US;  
float fCAD,fEUR,fGBP,fJPY,fRUR,fUS;
char option;
   EUR=1;
   GBP=2;
   JPY=3;
   CAD=4;
   RUR=5;
 
   
   fEUR=0.829643f;              
   fGBP=0.548242f;
   fJPY=110.631f;
   fCAD=1.31751f;
   fRUR=29.1800f;                                                                                  
    fUS=1.00f;
   printf("Select a currency type\n\n");
   printf("1=European Euro 2=United Kingdom Pound 3=Japanese Yens 4=Canadian Dollars 5=Russian Rubles\n\n");
 
while(1){

printf("Enter your choice:\n\n");

option=getch();
switch(option)
{
case '1':
printf("The selected currency type is the European Euro of %f equivalent to %f US  Dollar\n",fEUR,fUS);
break;
case '2':
printf("The selected currency type is the United Kingdom Pound of %f equivalent to %f US  Dollar\n",fGBP,fUS);
break;
case'3':
printf("The selected currency type is the Japanese Yens of %f equivalent to %f US  Dollar\n",fJPY,fUS);
break;
case'4':
printf("The selected currency type is the Canadian Dollars of %f equivalent to %f US  Dollar\n",fCAD,fUS);
break;
case '5':
printf("The selected currency type is the Russian Rubles of %f equivalent to %f US  Dollar\n",fRUR,fUS);
break;
case '0':
printf("exiting...\n");
return (0);
break;
default:
printf("Invalid Choice\n");
break;


}
}
getch();
return 0;                          
}
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
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 nested-loops in the C programming language.

829 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