2013-10-17 35 views
0

這個程序的目標是輸入一個基數,然後輸入任意數量的空格,然後輸入任意數字的序列,只要它們比基數小1。我有覆蓋的錯誤,但我無法得到它顯示。無法理解爲什麼函數不起作用

如果我輸入2 1101,我的輸出是對於給定的基2,「沒有出現」。

The output should be the following: Test Case # 1 
Input for Run 1: 
2  1101 
3 1212 
5 66 
2 1111 
8 36 
2 01 

The output for Test Run 1: 
For the given base 2, the decimal value of the input string is 11. 
For the given base 3, the decimal value of the input string is 70. 
For the given base 5, the number is NOT valid! 
For the given base 2, the decimal value of the input string is 15. 
For the given base 8, the decimal value of the input string is 51. 
For the given base 2, the decimal value of the input string is 2. 

這裏是我的編碼方案本節我有問題同:

#include <iostream> 
#include <cmath> 

using namespace std; 

const int MAX_CHARS = 256; 
const int MAX_BASE = 10; 

int readUntiValidBaseRead(); 
int readNumbersReturningValue(int base); 
int decimalValueOf(char chDigit); 
bool isValid(char chDigit, int base); 

//--------------------------------------------------------------------- 
// 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 readingBase; 
    cin >> readingBase; 
    while(!cin.eof() && (readingBase < 1 || readingBase > MAX_BASE)) 
    { 
     cout << "Invalid base given, " << endl; 
     cin.ignore(MAX_CHARS, '\n'); 
     cin >> readingBase; 
    } 
    if(readingBase > 1 && readingBase <= MAX_BASE) 
     return readingBase; 
    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: base -> IN 
//--------------------------------------------------------------------- 
int readNumbersReturningValue(int base) 
{ 
    char readingNumber; 
    int sum = 0; 
    int theValue = 1; 
    bool flaq = true; 
    cin >> readingNumber; 
    while(readingNumber != '\n' && flaq) 
    { 
     flaq = isValid(readingNumber, base); 
     sum += (theValue* decimalValueOf(readingNumber)); 
     theValue *= base; 
     cin >> readingNumber; 
     flaq = isValid(readingNumber, base); 
    } 
    if(flaq == true) 
     return sum; 
    else 
     return -1; 
} 


//--------------------------------------------------------------------- 
// This function returns the numeric value of the character digit that 
// is stored in chDigit. 
// params: chDigit -> IN 
//--------------------------------------------------------------------- 
int decimalValueOf(char chDigit) 
{ 
    int decimalNum; 
    decimalNum = chDigit - '0'; 
    return decimalNum; //return integer value of 
} 

//--------------------------------------------------------------------- 
// This function returns true if chDigit is a valid digit in the given 
// base, it returns false otherwise. 
// params: chDigit -> IN, base -> IN 
//--------------------------------------------------------------------- 
bool isValid(char chDigit, int base) 
{ 
    if(decimalValueOf(chDigit) >= 0 && decimalValueOf(chDigit) < base) 
     return true; 
    else 
     return false; 
} 
//--------------------------------------------------------------------- 
// 
// 
// 
int main() 
{ 
    int totalSum = 0; 
    int base; 
    int singleSum; 

    base = readUntilValidBaseRead(); 
    while(!cin.eof()) 
    { 

     cout << "For the given base " << base << ", "; 
     singleSum = readNumbersReturningValue(base); 

     if(singleSum == -1) 
     { 
     cout << "Not valid. Throwing away rest of line. " << endl; 
     cin.ignore(MAX_CHARS, '\n'); 
     } 
     else 
     { 
     cout << "The decimal value of the input string is " << singleSum; 
     totalSum += singleSum; 
     } 
     base = readUntilValidBaseRead(); 
    } 
    cout << totalSum; 
    return 0; 
} 
+1

你錯過了你的一半功能。如果您需要特定功能的幫助,請構建一個僅使用該功能的完整且可編譯的示例。如果你這樣做,很可能你會自己發現你的問題,但如果沒有,發佈一些我們可以編譯和研究的東西。 –

+0

你有沒有反對'std:string'? – Beta

+0

打印出'base'的值。你確定'readUntilValidBaseRead'可以嗎? – Cramer

回答

0

這裏:

cin >> readingNumber; 
while(readingNumber != '\n' && flaq) 
{ 
    flaq = isValid(readingNumber, base); 
    sum += (theValue* decimalValueOf(readingNumber)); 
    theValue *= base; 
    cin >> readingNumber; 
    flaq = isValid(readingNumber, base); 
} 

你第一次讀取增加sum,無論是否不是讀取的字符是有效的。它永遠不會有效,因爲你的isValid()函數返回空間的false。你應該跳過任何空格,然後纔開始檢查數字。當您讀入int時,您不需要擔心空格,但是當您讀入char時,由於空格字符是字符,因此cin不知道是否要跳過它們。

do { 
    cin >> readingNumber; 
} while (isspace(readingNumber)); 

你需要#include <cctype>isspace()

你可以像跳過他們。請注意,這將跳過任何空白字符,包括製表符和換行符,因此如果您可能完全缺少輸入,您可能需要單獨檢查\n

這裏有一個工作的例子,只是爲了跳過空格和閱讀您的號碼(即忽略了閱讀基地):

#include <iostream> 
#include <cctype> 

int decimalValueOf(char chDigit) { 
    return chDigit - '0'; 
} 

bool isValid(char chDigit, int base) { 
    return (decimalValueOf(chDigit) >= 0 && decimalValueOf(chDigit) < base); 
} 

int readNumbersReturningValue(int base) { 
    char readingNumber; 
    int sum = 0; 
    int theValue = 1; 
    bool flaq = true; 

    do { 
     std::cin >> readingNumber; 
    } while (std::isspace(readingNumber)); 

    while (readingNumber != '\n' && (flaq = isValid(readingNumber, base))) { 
     sum += (theValue* decimalValueOf(readingNumber)); 
     theValue *= base; 
     readingNumber = std::cin.get(); 
    } 

    if (flaq) { 
     return sum; 
    } else { 
     return -1; 
    } 
} 

int main() { 
    int sum = readNumbersReturningValue(2); 
    std::cout << "Sum is: " << sum << std::endl; 
    return 0; 
} 

輸出:

[email protected]:~/src/cpp/scratch$ ./readchars 
      1101 
Sum is: 11 
[email protected]:~/src/cpp/scratch$ 

注意std::cin >> readingNumber;將無法​​正常工作因爲std::cin是行緩衝的,所以你必須使用std::cin.get()來代替。

+0

我這樣做了,輸出仍然停止,並且在描述中的上面的「沒有任何東西出現」聲明之後不會輸出任何東西。 – user2108147

+0

@ user2108147:因爲你正在做的事情對於閱讀換行符不起作用,所以它總是掛起來。看我的編輯。 –

+0

嘿,我真的很感激它。我看到 while(readingNumber =='') readingNumber = cin.get() 也工作。非常感謝你的幫助。我的腰帶上只有一個半月的時間,真的很痛苦。如果我可以問,你從哪裏學到這一切?只是通過體驗和在這個網站上,介意與新手分享一些技巧? – user2108147