| | |
Input error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 54
Reputation:
Solved Threads: 3
I coded a program its for temperature conversion its still not developed yet and I'm having a problem.
Here's the code:
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.
Here's the code:
C++ Syntax (Toggle Plain Text)
/* Software Name = Temperature Converter Calculator... Made by Manzoor Ahmed.*/ // Program that helps converting Temperature degrees. // Formulas obtained from wikipedia.org. #include <cstdlib> #include <cctype> #include <iostream> using namespace std; int CelFunc() ; void CnvrtTemp() { char Cnvrt_Temp_Option ; cout << "\n==================== Convert Temperature =======================" << endl ; do { cout << "\n\n[C] Celsius Converter" << endl ; cout << "[F] Fahrenheit Converter" << endl ; cout << "[K] Kelvin Converter" << endl ; cout << "[R] Rankine Converter" << endl ; cout << "[M] Back to Main Menu." << endl ; cout << "\n(Characters enclosed by the square brackets are the options.)" << endl ; cout << "\nEnter your option: " ; cin >> Cnvrt_Temp_Option ; if ( Cnvrt_Temp_Option == 'C' || Cnvrt_Temp_Option == 'c' ) { CelFunc(); } if ( Cnvrt_Temp_Option == 'F' || Cnvrt_Temp_Option == 'f' ) { cout << "FahrFunc()" << endl ; } if ( Cnvrt_Temp_Option == 'K' || Cnvrt_Temp_Option == 'k' ) { cout << "KelFunc()" << endl; } if ( Cnvrt_Temp_Option == 'R' || Cnvrt_Temp_Option == 'r' ) { cout << "RankFunc()" << endl ; } } while (!( Cnvrt_Temp_Option == 'M' || Cnvrt_Temp_Option == 'm' )); } int main() { char Main_Menu_Option; // program name output cout << "################################################################################"; cout << "# #"; cout << "# #"; cout << "# #"; cout << "# Temperature Converter Calculator #"; cout << "# #"; cout << "# #"; cout << "# #"; cout << "# #"; cout << "################################################################################\n\n"; do { // Main Menu cout << "\n================================ Main Menu =====================================\n\n\n"; // Menu cout << "[C] Convert Temperature" << endl ; cout << "[H] Help" << endl ; cout << "[E] Exit" << endl ; cout << "\n(Characters enclosed by the square brackets are the options.)\n" << endl ; cout << "\nEnter your option : "; cin >> Main_Menu_Option ; 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' )) { cout << "\nWrong option entered\n"; } if ( Main_Menu_Option == 67 || Main_Menu_Option == 99) // 67 == C , 99 == c. { CnvrtTemp(); } if ( Main_Menu_Option == 69 || Main_Menu_Option == 104 ) { cout << "Help section is under construction.\n"; } } while (!( Main_Menu_Option == 69 || Main_Menu_Option == 101 )); system ("pause"); return 0; } int CelFunc() { char again; cout << "\n=============================== Celsius Converter ==============================\n\n" ; do { cout << "Enter °C (Celsius) degree to convert :"; double cTemp2Conv; cin >> cTemp2Conv; if (isdigit(cTemp2Conv)) { double cFahrenheit = (cTemp2Conv * 1.8) + 32; double cKelvin = cTemp2Conv + 273.15; double cRankine = (cTemp2Conv + 273.15) * 1.8; cout << "\nAnswer: " << cTemp2Conv << " Celsius in Fahnrenheit scale = " << cFahrenheit; cout << "\nAnswer: " << cTemp2Conv << " Celsius in Kelvin scale = " << cKelvin; cout << "\nAnswer: " << cTemp2Conv << " Celsius in Rankine scale = " << cRankine; cout << "\n\n"; } else { cout << "\nINVALID INPUT !"; cout << "\nPlease enter numerical values.\n"; } cout << "\nDo you want to convert temperature again ?"; cout << "\nEnter Y for yes, N for no."; cout << "\nEntering wrong option other than Y/N will result in getting back to \nConvert Temprature menu."; cout << "\n[y]/[n]:"; cin >> again; } while (again == 'y'|| again == 'Y'); }
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.
•
•
Join Date: Nov 2007
Posts: 54
Reputation:
Solved Threads: 3
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))
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.
c++ Syntax (Toggle Plain Text)
string s; cin>>s; for(int i=0;i<s.length();i++) { if(isalpha(s[i])) { cout<<"error!!!!!!"; return -1; } } int a=atoi(s.c_str());
Last edited by Ancient Dragon; Nov 11th, 2007 at 3:47 pm. Reason: add code tags
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.
•
•
•
•
Do i need to pass arguments in the cin.fail() function to check my input ?
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.
![]() |
Similar Threads
- Reading txt file into Hash Table (C++)
- exception error (VB.NET)
- Error in mailer script (PHP)
- Help with a reservation program! GUI Messed XD (Java)
- Help with File Reading loop (C++)
- help clear mess in single-scripted array (C++)
- reading a file into code (Java)
- What is wrong with my program? (C++)
Other Threads in the C++ Forum
- Previous Thread: Understanding arrays
- Next Thread: Urgently Needed
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






