By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,143 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,143 IT Pros & Developers. It's quick & easy.

How to Terminate a program with invalid input.

P: 4
Hello,

I was wondering how to stop a program if invalid input is used. I am working on a problem about ultimate frisbee teams. The input I ask for is how many wins they have. They only play 10 games and I need to stop the program if someone puts a value more than 10 as input. How would I do this?
Feb 10 '11 #1

✓ answered by horace1

something like
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.   int games;
  3.   scanf("%d",&games);
  4.   if(games>10) 
  5.     { printf("too many games! "); return -1; }
  6.  
return 0 from main() is the normal all OK return anything else is error

Share this Question
Share on Google+
4 Replies


Banfa
Expert Mod 5K+
P: 8,916
Just return from main or if you are too deeply nested call exit or abort.
Feb 10 '11 #2

P: 4
The input is right at the beginning of the code. How exactly do I return from main?
Feb 10 '11 #3

Expert 100+
P: 1,510
something like
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.   int games;
  3.   scanf("%d",&games);
  4.   if(games>10) 
  5.     { printf("too many games! "); return -1; }
  6.  
return 0 from main() is the normal all OK return anything else is error
Feb 10 '11 #4

P: 4
Thank you so much!
Feb 10 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.