Error Checking While Loop
3.6 - Error-Checking with while Purpose: To learn how to write better error-checking using a while loop Error! In Unit 2 we discussed some basic error-checking of user input. For example, if we want the user to enter an even integer, we could have used the following code: System.out.print("Enter an even integer: "); number = keybd.readInt(); if(number%2==1) { System.out.println("Bad input."); } else { System.out.println("Thank you!"); } That works fine at identifying an error. But for error-checking to be truly useful, we need to allow the user to re-enter their input until they get it right. This requires repetition in general and specifically, a while loop. Let's modify the above code to see an example. System.out.print("Enter an even integer: "); number = keybd.readInt(); while(number%2==1) { System.out.println("Bad input. Try again!"); System.out.print("Enter an even integer: "); number = keybd.readInt(); } System.out.println("Thank you!"); In this version, the if statement is replaced by a while loop. The loop will not exit until the condition is false, meaning until the number entered is even. If the user enters an odd integer, the loop will tell them they made a mistake and require them to perform the input again. This will continue until they get it correct. Another example Suppose we need the user to enter an integer between 1 and 5. We can use the following to ensure correct input: System.out.print("Enter an integer from 1-5: "); number = keybd.readInt(); while(number<1 || number>5) { System.out.println("Bad input. Try again!"); System.out.print("Enter an integer from 1-5:"); number = keybd.readInt(); } System.out.println("Thank you!"); In closing, The while loop version of error-checking is much more useful than the if statement version as it gives the user the ability to re-enter their input until they get it right. This helps maintain the integrity of the data entered by users and ensures that the program will not continue until it receives the correct information. From now on, when writing erro
1 New Topic/Question Reply 5 Replies - 6320 Views - Last Post: 27 October 2009 - 11:40 AM Rate Topic: #1 winggundam3451 New D.I.C Head Reputation: 0 Posts: 15 Joined: 03-October 09 Error checking user input with loop Posted 26 October 2009 - 09:35 PM I would post the entire code.. but this a popular hw assignment and I'm not giving it out. This function: void getbet (int money, int &bet ) { cout << "Lets play HiLo! Enter a bet!--->"; cin >> bet; while ( (bet http://danshuster.com/apcs/java0306.htm > money) || (bet < 0) ) { cout << "Enter a valid bet please--->"; cin >> bet; } } Reads in a bet from the user playing a HighLow number guessing game. Part of the problem is to have it check for invalid inputs. I have it making sure you cannot bet 0 dollars and you cannot bet more than you have. http://www.dreamincode.net/forums/topic/134639-error-checking-user-input-with-loop/ I have to guard against letters and any input which would be an invalid bet. Ive included cctype at the top of my code to allow me to use isalpha and similar cctypes. However, they do not check. It just throws it into an endless loop. Any suggestions ? Thanks, Ryan Is This A Good Question/Topic? 0 Back to top MultiQuote Quote + Reply Replies To: Error checking user input with loop #2 OliveOyl3471 Everybody's crazy but me! Reputation: 135 Posts: 6,581 Joined: 11-July 07 Re: Error checking user input with loop Posted 26 October 2009 - 10:33 PM void getbet (int money, int &bet ) { cout << "Lets play HiLo! Enter a bet!--->"; cin >> bet; while (!cin.good()) //checks to see if the input is not good { cout <<"You must type a number.\n"; cin.clear();//clear the error state cin.ignore(numeric_limits
Programming Boards C Programming Error checking with loop Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: Error http://cboard.cprogramming.com/c-programming/46145-error-checking-loop.html checking with loop Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 10-20-2003 #1 CrackerJack View Profile View Forum Posts Registered User Join Date Oct http://www.cplusplus.com/forum/beginner/71359/ 2003 Posts 49 Error checking with loop OK guys I'm back! This was actually a problem I asked you guys about before, but now I am trying to add a looped error checking. I want the program to say error checking if the hour is wrong, if the seconds are wrong, or both. Then let the user retry it. I got the hour to work, but the other two are acting wierd. This is what I have so far: Code: #include
do while loop. I want to post the code for it but I'm not sure how to do it on here. If someone could help me with that than we can move on with the help with the actual code :P Last edited on May 18, 2012 at 5:16pm UTC May 18, 2012 at 5:01pm UTC slider57 (46) copy and paste the code into the text field, but make sure you use the format option for code named 'source code' (those tools should be next to the text field). May 18, 2012 at 5:05pm UTC Flashkilla (6) There that should do it. It's a mass conversion program that goes from imperial to metric and then back. I have the error checking working just fine for the unit of measurement its checking the int is the issue. Try running this and see what you can come up with. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Christopher Reath May 11th, 2012 This program converts a mass unit of measurement into another type of measurement */ #include