Input error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 54
Reputation: manzoor is an unknown quantity at this point 
Solved Threads: 3
manzoor manzoor is offline Offline
Junior Poster in Training

Input error

 
0
  #1
Nov 11th, 2007
I coded a program its for temperature conversion its still not developed yet and I'm having a problem.

Here's the code:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Input error

 
0
  #2
Nov 11th, 2007
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()
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 54
Reputation: manzoor is an unknown quantity at this point 
Solved Threads: 3
manzoor manzoor is offline Offline
Junior Poster in Training

Re: Input error

 
0
  #3
Nov 11th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 10
Reputation: hgedek is an unknown quantity at this point 
Solved Threads: 1
hgedek's Avatar
hgedek hgedek is offline Offline
Newbie Poster

Re: Input error

 
0
  #4
Nov 11th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 54
Reputation: manzoor is an unknown quantity at this point 
Solved Threads: 3
manzoor manzoor is offline Offline
Junior Poster in Training

Re: Input error

 
0
  #5
Nov 11th, 2007
Can any one tell me which method should I use ???

Just tell me the function I'm not saying that do it for me
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 10
Reputation: hgedek is an unknown quantity at this point 
Solved Threads: 1
hgedek's Avatar
hgedek hgedek is offline Offline
Newbie Poster

Re: Input error

 
0
  #6
Nov 11th, 2007
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Input error

 
0
  #7
Nov 11th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 54
Reputation: manzoor is an unknown quantity at this point 
Solved Threads: 3
manzoor manzoor is offline Offline
Junior Poster in Training

Re: Input error

 
0
  #8
Nov 12th, 2007
Do i need to pass arguments in the cin.fail() function to check my input ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 4
Reputation: Ali_110 is an unknown quantity at this point 
Solved Threads: 0
Ali_110 Ali_110 is offline Offline
Newbie Poster

Re: Input error

 
0
  #9
Nov 12th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Input error

 
0
  #10
Nov 12th, 2007
Originally Posted by manzoor View Post
Do i need to pass arguments in the cin.fail() function to check my input ?
No -- you need to start reading the documentation
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC