在没有字符串的情况下删除函数中的空格

时间:2013-10-24 20:32:08

标签: c++

此项目中的数字值未显示,基础工作正常但我真的可以使用一些帮助找出它不打印的原因。它达到打印出基础的程度,但之后它就会停止。

#include <iostream>;
#include <cctype>;
using namespace std;

const int MIN_VALID_BASE = 2;
const int MAX_VALID_BASE = 9;
const int MAX_LENGTH = 256;

//------------------ Function Properties ------------------
int ReadUntilValidBaseRead( );
int ReadNumbersReturningValue( int base );
int DecimalValueOf( char chDigit );
bool IsValid( char chDigit, int base );

int main()
{
   int totalSum = 0;
   int numberValue;
   int base;
   base = ReadUntilValidBaseRead();
   while(!cin.eof())
   {
      cout << "For the given base " << base << ",";
      numberValue = ReadNumbersReturningValue(base);
      if (numberValue == -1)
         cout << " the number is NOT valid!" << endl;
      else
      {
         cout << "the decimal value of the input string is "
            << numberValue << endl;
         totalSum = numberValue + totalSum;
      }
      base = ReadUntilValidBaseRead();

   }
   cout << "The total sum of all valid values is " << totalSum << endl;

}

//---------------------------------------------------------------------
// This function reads bases until a valid base is read or eof occurs.
// If an invalid base is read, an error message is displayed and the 
// rest of the line is ignored and another attempt to read a base value
// will be attempted.
// -1 is returned if eof occurs otherwise a valid base value is 
// returned.
//---------------------------------------------------------------------
int ReadUntilValidBaseRead( )
{
   int baseValid;

      cin >> baseValid;
      while (!cin.eof() && ( baseValid < MIN_VALID_BASE ||
         baseValid > MAX_VALID_BASE))
      {
         cout << "Invalid base given, throwing away the "
            << "rest of the line.";
         cin.ignore(MAX_LENGTH, '\n');
         cin >> baseValid;
      }
      if (baseValid >= MIN_VALID_BASE || baseValid <= MAX_VALID_BASE)
          return baseValid;
      else
          return -1;


}
//---------------------------------------------------------------------
// This function reads in a sequence of characters that represent
// a number in the given base.  A valid sequence is given in a 
// "backwards" format such that the rightmost digit is given first,
// the second to the rightmost digit is next, etc. 
// This function returns the value of this sequence of characters if
// it is a valid sequence.  If it is not valid it returns -1. 
// params: TODO
//---------------------------------------------------------------------
int ReadNumbersReturningValue( int base )
{
   int numberValue;
   char chDigit;
   int sum = 0;
   int basePOW = 1;
   bool valid = IsValid(chDigit, base);
   do
   {
      cin >> chDigit;
   }while (isspace(chDigit));
   valid;
   while (valid && chDigit != '\n')
   {
      valid = IsValid(chDigit, base);
      sum += (DecimalValueOf(chDigit) * (basePOW *= base));
      cin >> chDigit;
   }
   if (valid)
      return sum;
   else
      return -1;
}

//---------------------------------------------------------------------
// This function returns the numeric value of the character digit that
// is stored in chDigit.
// params: TODO
//---------------------------------------------------------------------
int DecimalValueOf( char chDigit )
{
   return chDigit - '0';
}

//---------------------------------------------------------------------
// This function returns true if chDigit is a valid digit in the given
// base, it returns false otherwise.
// params: TODO
//---------------------------------------------------------------------
bool IsValid( char chDigit, int base )
{
   return DecimalValueOf(chDigit) < base &&
        DecimalValueOf(chDigit) >= 0;

}

1 个答案:

答案 0 :(得分:0)

只需调试代码,就可以注意到你没有初始化变量“chDigit”。因此,代码可能会在第83行中断,您可以使用此代码:

int ReadNumbersReturningValue( int base )
{
    int numberValue;
    char chDigit; // <------- SHOULDN'T HAVE BEEN INITIALIZED?
    int sum = 0;
    int basePOW = 1;
    bool valid = IsValid(chDigit, base); // <------ HERE
. . .
你检查过了吗?这可能是你的问题。