| | |
infinite while loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
I'm not quite sure how to fix it. I have done the debugger on it, and it seems that it might have something to do with the cin.get() but im not sure.
i want the user to enter a number. it first goes through the zeros because it doesnt want to count them. then test for the decimal, and then finally, count the digits after the decimal, and at most there are 20 numbers in the array. let me know if this isn't enough and ill give you more information.
C++ Syntax (Toggle Plain Text)
void MyFloat::Read() { char ch; int k = 0; cin.get(ch); while( ch == '0' || isspace(ch)) cin.get(ch); if (ch != '.') return; cin.get(ch); while( k != isalpha(ch) || k != MAX_DIGITS) { Number[k] = ch - '0'; k++; cin.get(ch); } for(k; k<MAX_DIGITS; k++) Number[k]=0; NumberOfDigits = k; cin.putback(ch); }
i want the user to enter a number. it first goes through the zeros because it doesnt want to count them. then test for the decimal, and then finally, count the digits after the decimal, and at most there are 20 numbers in the array. let me know if this isn't enough and ill give you more information.
-7
#2 31 Days Ago
>> while( k != isalpha(ch) || k != MAX_DIGITS)
The problem is that line. Why test for k != isalpha(ch) ? The loop counter has nothing to do with the value of ch. And if you type fewer than MAX_DIGITS then the next cin.get() will wait for you to enter anoter character.
The problem is that line. Why test for k != isalpha(ch) ? The loop counter has nothing to do with the value of ch. And if you type fewer than MAX_DIGITS then the next cin.get() will wait for you to enter anoter character.
Last edited by Ancient Dragon; 31 Days Ago at 10:39 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.
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
0
#5 31 Days Ago
i changed it to this
and its still an infinite loop.
C++ Syntax (Toggle Plain Text)
while( ch != isalpha(ch) || k == MAX_DIGITS)
and its still an infinite loop.
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
1
#6 31 Days Ago
purposely over commented code for beginner to understand process
C++ Syntax (Toggle Plain Text)
//declare a flag variable bool invalidInput; //outer loop controls number of digits input while(k < MAX_DIGITS) { //reset flag each time through outer loop invalidInput = true; //inner loop while(invalidInput) { //gets char input cin.get(ch); //validates char input if(!isalpha(ch)) //could use if(isdigit(ch)) as alternate syntax---better validation---what if input is * char or ^ char or # char, etc. in addition to alphabetical char { //converts char input into int type and assigns it to an array at index k Number[k] = ch - '0'; //get next index k++; //change flag to stop inner loop invalidInput = false; }//end if body }//end inner loop }//end outer loop
Last edited by Lerner; 31 Days Ago at 11:45 pm.
Klatu Barada Nikto
![]() |
Similar Threads
- XP Startup Problem: Infinite Loop (Windows NT / 2000 / XP)
- infinite loop... (C++)
- Infinite Loop in the switch statements (C++)
- infinite loop (C)
- infinite loop (C++)
Other Threads in the C++ Forum
- Previous Thread: How to read and sort data in a textfile (ascending order)
- Next Thread: please help to debug this
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game 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 numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






