这个项目中的数值没有显示,基础工作正常,但我真的可以使用一些帮助来找出它不打印的原因。它到达了打印出基础的地步,但之后它就停止了。
#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;
}