Solved

Exit from program when number is not in the loop

Posted on 2004-08-18
5
235 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now