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.

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.

>> 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.

Edited 7 Years Ago by Ancient Dragon: n/a

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.

Please re-read my previous post -- I changed it.

i changed it to this

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

and its still an infinite loop.

purposely over commented code for beginner to understand process

//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

Edited 7 Years Ago by Lerner: n/a

This article has been dead for over six months. Start a new discussion instead.