943,755 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2503
  • C++ RSS
Nov 11th, 2007
0

Input error

Expand Post »
I coded a program its for temperature conversion its still not developed yet and I'm having a problem.

Here's the code:
C++ Syntax (Toggle Plain Text)
  1. /* Software Name = Temperature Converter Calculator...
  2. Made by Manzoor Ahmed.*/
  3. // Program that helps converting Temperature degrees.
  4. // Formulas obtained from wikipedia.org.
  5.  
  6. #include <cstdlib>
  7. #include <cctype>
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. int CelFunc() ;
  13.  
  14. void CnvrtTemp()
  15. {
  16. char Cnvrt_Temp_Option ;
  17.  
  18.  
  19. cout << "\n==================== Convert Temperature =======================" << endl ;
  20. do
  21. {
  22. cout << "\n\n[C] Celsius Converter" << endl ;
  23. cout << "[F] Fahrenheit Converter" << endl ;
  24. cout << "[K] Kelvin Converter" << endl ;
  25. cout << "[R] Rankine Converter" << endl ;
  26. cout << "[M] Back to Main Menu." << endl ;
  27.  
  28.  
  29.  
  30. cout << "\n(Characters enclosed by the square brackets are the options.)" << endl ;
  31.  
  32. cout << "\nEnter your option: " ;
  33. cin >> Cnvrt_Temp_Option ;
  34.  
  35. if ( Cnvrt_Temp_Option == 'C' || Cnvrt_Temp_Option == 'c' )
  36. {
  37. CelFunc();
  38. }
  39.  
  40. if ( Cnvrt_Temp_Option == 'F' || Cnvrt_Temp_Option == 'f' )
  41. {
  42. cout << "FahrFunc()" << endl ;
  43. }
  44.  
  45. if ( Cnvrt_Temp_Option == 'K' || Cnvrt_Temp_Option == 'k' )
  46. {
  47. cout << "KelFunc()" << endl;
  48. }
  49.  
  50. if ( Cnvrt_Temp_Option == 'R' || Cnvrt_Temp_Option == 'r' )
  51. {
  52. cout << "RankFunc()" << endl ;
  53. }
  54. } while (!( Cnvrt_Temp_Option == 'M' || Cnvrt_Temp_Option == 'm' ));
  55. }
  56.  
  57. int main()
  58. {
  59. char Main_Menu_Option;
  60. // program name output
  61. cout << "################################################################################";
  62. cout << "# #";
  63. cout << "# #";
  64. cout << "# #";
  65. cout << "# Temperature Converter Calculator #";
  66. cout << "# #";
  67. cout << "# #";
  68. cout << "# #";
  69. cout << "# #";
  70. cout << "################################################################################\n\n";
  71.  
  72.  
  73. do
  74. { // Main Menu
  75.  
  76. cout << "\n================================ Main Menu =====================================\n\n\n";
  77. // Menu
  78. cout << "[C] Convert Temperature" << endl ;
  79. cout << "[H] Help" << endl ;
  80. cout << "[E] Exit" << endl ;
  81.  
  82. cout << "\n(Characters enclosed by the square brackets are the options.)\n" << endl ;
  83.  
  84.  
  85.  
  86. cout << "\nEnter your option : ";
  87. cin >> Main_Menu_Option ;
  88.  
  89.  
  90. if (!( Main_Menu_Option == 'C' || Main_Menu_Option == 'c' || Main_Menu_Option == 'H' || Main_Menu_Option == 'h' || Main_Menu_Option == 'E' || Main_Menu_Option == 'e' ))
  91. {
  92. cout << "\nWrong option entered\n";
  93. }
  94.  
  95.  
  96.  
  97. if ( Main_Menu_Option == 67 || Main_Menu_Option == 99) // 67 == C , 99 == c.
  98. {
  99. CnvrtTemp();
  100. }
  101.  
  102. if ( Main_Menu_Option == 69 || Main_Menu_Option == 104 )
  103. {
  104. cout << "Help section is under construction.\n";
  105. }
  106.  
  107.  
  108. } while (!( Main_Menu_Option == 69 || Main_Menu_Option == 101 ));
  109. system ("pause");
  110. return 0;
  111. }
  112.  
  113. int CelFunc()
  114. { char again;
  115. cout << "\n=============================== Celsius Converter ==============================\n\n" ;
  116.  
  117.  
  118. do {
  119. cout << "Enter °C (Celsius) degree to convert :";
  120. double cTemp2Conv;
  121. cin >> cTemp2Conv;
  122.  
  123. if (isdigit(cTemp2Conv)) {
  124.  
  125.  
  126. double cFahrenheit = (cTemp2Conv * 1.8) + 32;
  127. double cKelvin = cTemp2Conv + 273.15;
  128. double cRankine = (cTemp2Conv + 273.15) * 1.8;
  129.  
  130.  
  131. cout << "\nAnswer: " << cTemp2Conv << " Celsius in Fahnrenheit scale = " << cFahrenheit;
  132. cout << "\nAnswer: " << cTemp2Conv << " Celsius in Kelvin scale = " << cKelvin;
  133. cout << "\nAnswer: " << cTemp2Conv << " Celsius in Rankine scale = " << cRankine;
  134. cout << "\n\n";
  135. }
  136. else {
  137. cout << "\nINVALID INPUT !";
  138. cout << "\nPlease enter numerical values.\n";
  139. }
  140. cout << "\nDo you want to convert temperature again ?";
  141. cout << "\nEnter Y for yes, N for no.";
  142. cout << "\nEntering wrong option other than Y/N will result in getting back to \nConvert Temprature menu.";
  143. cout << "\n[y]/[n]:";
  144. cin >> again;
  145.  
  146. } while (again == 'y'|| again == 'Y');
  147. }

Well the problem is when you go to Celsius Converter and their input a character then its starts looping infinitely. How to avoid looping ?
I used isdigit() here to check whether the input is a numerical value or its an alphabet.
I also used the cin.good() function but I'm still getting the same error.

Sorry if my code is too big but I couldn't lessen it because I'm a beginner and don't know where the real problem is.
Similar Threads
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
manzoor is offline Offline
54 posts
since Nov 2007
Nov 11th, 2007
0

Re: Input error

I think the problem is that after cin >> Cnvrt_Temp_Option ; you need to strip the '\n' (Enter key) from the keyboard buffer. One way to do that is cin.ignore()
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 11th, 2007
0

Re: Input error

But I have problem in the CelFunc()

Anyway I'm gonna check it out


The Problem is in the CelFunc(), there when it asks for a number, but instead of inputting a number if you input an alphabet then it stars looping infinitely.



In CelFunc(), if you see the if statement its if (!(isdigit(cTemp2Conv))) not if (isdigit(cTemp2Conv))
Last edited by manzoor; Nov 11th, 2007 at 12:06 pm.
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
manzoor is offline Offline
54 posts
since Nov 2007
Nov 11th, 2007
0

Re: Input error

You are using isdigit for double or int.but it wont return true.you may use for char.and if char is digit it will return true.so you may change your method for getting number from user.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
hgedek is offline Offline
10 posts
since Nov 2007
Nov 11th, 2007
0

Re: Input error

Can any one tell me which method should I use ???

Just tell me the function I'm not saying that do it for me
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
manzoor is offline Offline
54 posts
since Nov 2007
Nov 11th, 2007
0

Re: Input error

c++ Syntax (Toggle Plain Text)
  1. string s;
  2. cin>>s;
  3.  
  4. for(int i=0;i<s.length();i++)
  5. {
  6. if(isalpha(s[i]))
  7. {
  8. cout<<"error!!!!!!";
  9. return -1;
  10. }
  11.  
  12. }
  13. int a=atoi(s.c_str());
This is a basic test function.User cant input a alpha or alphanumeric string.
Last edited by Ancient Dragon; Nov 11th, 2007 at 3:47 pm. Reason: add code tags
Reputation Points: 11
Solved Threads: 1
Newbie Poster
hgedek is offline Offline
10 posts
since Nov 2007
Nov 11th, 2007
0

Re: Input error

isdigit only works with a single character, not a double or int. doubles are always guarenteed to be all digits plus the decimal point so there's no point using isdigit with it. If you want to check that someone did not type a digit then use cin's error method if( cin.fail() )
Last edited by Ancient Dragon; Nov 11th, 2007 at 4:00 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 12th, 2007
0

Re: Input error

Do i need to pass arguments in the cin.fail() function to check my input ?
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
manzoor is offline Offline
54 posts
since Nov 2007
Nov 12th, 2007
0

Re: Input error

manzoorr plzz add me i also wanted a little help from u
my id is
<snipped>
Last edited by Ancient Dragon; Nov 12th, 2007 at 8:19 am. Reason: removed email
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ali_110 is offline Offline
4 posts
since Nov 2007
Nov 12th, 2007
0

Re: Input error

Click to Expand / Collapse  Quote originally posted by manzoor ...
Do i need to pass arguments in the cin.fail() function to check my input ?
No -- you need to start reading the documentation
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Understanding arrays
Next Thread in C++ Forum Timeline: Urgently Needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC