infinite while loop

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

Join Date: Oct 2009
Posts: 11
Reputation: ninreznorgirl2 is an unknown quantity at this point 
Solved Threads: 0
ninreznorgirl2 ninreznorgirl2 is offline Offline
Newbie Poster

infinite while loop

 
0
  #1
31 Days Ago
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.

  1. void MyFloat::Read()
  2. {
  3. char ch;
  4. int k = 0;
  5.  
  6. cin.get(ch);
  7.  
  8. while( ch == '0' || isspace(ch))
  9. cin.get(ch);
  10.  
  11. if (ch != '.')
  12. return;
  13.  
  14. cin.get(ch);
  15.  
  16. while( k != isalpha(ch) || k != MAX_DIGITS)
  17. {
  18. Number[k] = ch - '0';
  19. k++;
  20. cin.get(ch);
  21. }
  22.  
  23. for(k; k<MAX_DIGITS; k++)
  24. Number[k]=0;
  25.  
  26. NumberOfDigits = k;
  27. cin.putback(ch);
  28.  
  29. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
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: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-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.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 11
Reputation: ninreznorgirl2 is an unknown quantity at this point 
Solved Threads: 0
ninreznorgirl2 ninreznorgirl2 is offline Offline
Newbie Poster
 
0
  #3
31 Days Ago
that one is working fine. I'm just skipping zeros or spaces that are in front of the decimal. its the other while loop that isn't working.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
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: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #4
31 Days Ago
Please re-read my previous post -- I changed it.
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: Oct 2009
Posts: 11
Reputation: ninreznorgirl2 is an unknown quantity at this point 
Solved Threads: 0
ninreznorgirl2 ninreznorgirl2 is offline Offline
Newbie Poster
 
0
  #5
31 Days Ago
Originally Posted by Ancient Dragon View Post
Please re-read my previous post -- I changed it.
i changed it to this

  1. while( ch != isalpha(ch) || k == MAX_DIGITS)

and its still an infinite loop.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso
 
1
  #6
31 Days Ago
purposely over commented code for beginner to understand process
  1. //declare a flag variable
  2. bool invalidInput;
  3.  
  4. //outer loop controls number of digits input
  5. while(k < MAX_DIGITS)
  6. {
  7. //reset flag each time through outer loop
  8. invalidInput = true;
  9.  
  10. //inner loop
  11. while(invalidInput)
  12. {
  13. //gets char input
  14. cin.get(ch);
  15.  
  16. //validates char input
  17. 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
  18. {
  19. //converts char input into int type and assigns it to an array at index k
  20. Number[k] = ch - '0';
  21.  
  22. //get next index
  23. k++;
  24.  
  25. //change flag to stop inner loop
  26. invalidInput = false;
  27. }//end if body
  28. }//end inner loop
  29. }//end outer loop
Last edited by Lerner; 31 Days Ago at 11:45 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC