944,120 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 548
  • C++ RSS
Oct 27th, 2009
0

infinite while loop

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninreznorgirl2 is offline Offline
11 posts
since Oct 2009
Oct 27th, 2009
-7
Re: infinite while loop
>> 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; Oct 27th, 2009 at 10:39 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Oct 27th, 2009
0
Re: infinite while loop
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninreznorgirl2 is offline Offline
11 posts
since Oct 2009
Oct 27th, 2009
-7
Re: infinite while loop
Please re-read my previous post -- I changed it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Oct 27th, 2009
0
Re: infinite while loop
Please re-read my previous post -- I changed it.
i changed it to this

C++ Syntax (Toggle Plain Text)
  1. while( ch != isalpha(ch) || k == MAX_DIGITS)

and its still an infinite loop.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninreznorgirl2 is offline Offline
11 posts
since Oct 2009
Oct 27th, 2009
1
Re: infinite while loop
purposely over commented code for beginner to understand process
C++ Syntax (Toggle Plain Text)
  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; Oct 27th, 2009 at 11:45 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 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: How to read and sort data in a textfile (ascending order)
Next Thread in C++ Forum Timeline: please help to debug this





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


Follow us on Twitter


© 2011 DaniWeb® LLC