Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

actual code keyboard error checking code 2 (to attention of 2ffat)

Status
Not open for further replies.

cooldude69

Technical User
Aug 14, 2000
3
GB
Here is the actualcode which i am trying to develope<br><br><br>calculation()<br>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clrscr();<br><br> cout&lt;&lt;&quot;Pleae enter a value for resistance&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cin&gt;&gt;R;<br><br> while (cin.fail())<br>&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;&quot;You have eneter a illegal value please try again&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp; cin&gt;&gt;R;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br> cout&lt;&lt;&quot;Pleae enter a value for capacitance&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp; cin&gt;&gt;C;<br><br>&nbsp;&nbsp;&nbsp;&nbsp; while (cin.fail())<br>&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;&quot;You have eneter a illegal value please try again&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp; cin&gt;&gt;C;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br> cout&lt;&lt;&quot;Pleae enter a value for inductance&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp; cin&gt;&gt;L;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (cin.fail())<br>&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;&quot;You have eneter a illegal value please try again&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp; cin&gt;&gt;L;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;=========================================================&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;You have entered the following values&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;Resistance (Ohms) = &quot;&lt;&lt; R &lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;Inductance (Heneries) = &quot;&lt;&lt; L &lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;Capacitance (Farads) = &quot;&lt;&lt; C &lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;=========================================================&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;The resonant frequency for a series RLC circuit with the&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp; W=1/(L*C);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;above component values is W (rads) = &quot;&lt;&lt;W;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot; frequency (Hz) = &quot;&lt;&lt; W/(2*3.14)&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt;&quot;The circuit also has a qualtity factor Q =&quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;cin&gt;&gt;quit;<br> }while(quit=='Y');<br><br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;}
 
cooldud69,<br><br>&nbsp;&nbsp;&nbsp;&nbsp;I haven't forgotten you but I've been very busy at work. My suggestion is to collect your input into a string or character array then translate the string into a floating point number. The <i>cin</i> stream already does this when you input a string into a float. The problem with most of these schemes (e.g., <i>atol</i>) is that they simply stop translating when they encounter an alpha instead of a numeric. <br><br>&nbsp;&nbsp;&nbsp;&nbsp;From your description, you want to know <b>when</b> they enter an invalid entry. My (not so eligant) suggestion is to use the <i>ctype</i> functions on each character in the string. By checking each character in the input, you can tell if the input is incorrect of not. For example,<br><font color=blue><br>string Input;<br>bool Check = true;<br>cin &gt;&gt; Input;<br><br>for (int x = 0; x &lt; Input.length(); x++)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;char Z = Input[x];<br>&nbsp;&nbsp;&nbsp;&nbsp;if (!isalnum(Z) && !ispunct(Z))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Z is a control character<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Check = false;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!isnum(Z) && (Z != '.'))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Character is not a number or period<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Check = false;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br>if (!Check)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;Not a valid number!!!!&quot; &lt;&lt; endl;<br>}<br></font><br><br>&nbsp;&nbsp;&nbsp;&nbsp;Of course, this is off the top of my head so there probably errors in it but you should get an idea. If you use a character array, you may have to check for the null character instead of the length of the string. You best bet is to make this a function that you can call for each input.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;If you want me to post some actual code, let me know here and I'll see what I can do.<br><br> <p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top