Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting rid of spaces in my function without string
    primarykey
    data
    text
    <p>The number value in this project is not showing, the base works fine but I really could use some help figuring out the reason why it is not printing. It gets to the point where it prints out the base but after that it just stops.</p> <pre><code>#include &lt;iostream&gt;; #include &lt;cctype&gt;; 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 &lt;&lt; "For the given base " &lt;&lt; base &lt;&lt; ","; numberValue = ReadNumbersReturningValue(base); if (numberValue == -1) cout &lt;&lt; " the number is NOT valid!" &lt;&lt; endl; else { cout &lt;&lt; "the decimal value of the input string is " &lt;&lt; numberValue &lt;&lt; endl; totalSum = numberValue + totalSum; } base = ReadUntilValidBaseRead(); } cout &lt;&lt; "The total sum of all valid values is " &lt;&lt; totalSum &lt;&lt; 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 &gt;&gt; baseValid; while (!cin.eof() &amp;&amp; ( baseValid &lt; MIN_VALID_BASE || baseValid &gt; MAX_VALID_BASE)) { cout &lt;&lt; "Invalid base given, throwing away the " &lt;&lt; "rest of the line."; cin.ignore(MAX_LENGTH, '\n'); cin &gt;&gt; baseValid; } if (baseValid &gt;= MIN_VALID_BASE || baseValid &lt;= 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 &gt;&gt; chDigit; }while (isspace(chDigit)); valid; while (valid &amp;&amp; chDigit != '\n') { valid = IsValid(chDigit, base); sum += (DecimalValueOf(chDigit) * (basePOW *= base)); cin &gt;&gt; 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) &lt; base &amp;&amp; DecimalValueOf(chDigit) &gt;= 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload