2013-04-28 21 views
0

我完全不明白它有什麼問題。最後的嘗試次數是9,即使有一個while循環。我想檢查猜測是字符串中的座標之一,但它從來沒有工作。 :/字符串/數組情況C++

我在哪裏移動if語句?

int main() { 
int guesses, destroy, numAttempts = 11; 
string guess, i; 
string coordinate[3] = {"B1", "C4", "D3"}; 

cout << "Enter in a coordinate between A-1 and D-4 (i.e. C4): "; 
cin >> guess; 

guesses = 0; 
destroy = 0; 
while (guess != coodinate[i] && guesses < numAttempts - 1) { 
    cout << "Target missed. Try again: "; 
    cin >> guess; 
    guesses++;; 
} 
if (guess != coordinate[i]) 
    cout << "Failed!"; 
else 
    cout << "Congrats!"; 

    /*if (guess == coordinate) { 
    cout << "Target hit! Next target: "; 
    cin >> guess; 
destroy++; 
guesses++; 

} 
*/ 

} 
+1

考慮到Bigood被打錯了,這是真的嗎? – StoryTeller 2013-04-28 16:37:39

+0

'我'應該被聲明爲'int',而不是'string' ... – dreamlax 2013-04-28 17:27:26

回答

1

你得在這裏一個錯字:

while (guess != coodinate[i] && guesses < numAttempts - 1) 
       //coordinate[i] 

嘗試:

while ((guess != coordinate[i]) && (guesses < (numAttempts - 1))) 
//Parenthesis are not mandatory 

此外,正如其他人所指出的,你不是在尋找價值guess所有陣列coordinate,因爲您不會增加i

+1

我向上投了,但我認爲額外的括號確實是不必要的。他們只會增加角色數量。 – StoryTeller 2013-04-28 16:39:05

+0

@StoryTeller爲了清楚起見,我添加了這些內容,我不再使用C++:我不確定它們是否有用。我會編輯我的答案來指出。 – Bigood 2013-04-28 16:44:00

2

你忘了增加i .. i++(至少,我假設你是?)。 可能是錯誤的一部分。 如果你確實有遞增我,請確保它不會出界..

guess = input ; 
    guesses = 0; 
    while (guesses < numAttempts && guess != coodinate[i]) { 
    cout << "Target missed. Try again: "; 
    cin >> guess; 
    guesses++; 
    i = (i+1)%3; 
    } 
+0

謝謝。這有一點幫助,但我的主要問題是: – 2013-04-28 16:55:38

+0

「錯誤C2677:二進制'[':沒有找到全局運算符,其類型'std :: string'(或沒有可接受的轉換) 」 – 2013-04-28 16:56:08

+0

這個錯誤發生在哪裏? – 2013-04-28 16:57:36

0

你需要學習如何創建一個小的通用功能,並將它們組合在一起。

#include <set> // because you have a set of targets... 
#include <string> // used to represent a target 

using Target = std::string; 

static Target const MissedTarget = ""; 

static bool isGuessCorrect(Target const& guess, 
          std::set<Target> const& targets) 
{ 
    return targets.count(guess); 
} 

// Returns the target hit (if any), or MissedTarget otherwise 
static Target tryOnce(std::set<Target> const& targets) { 
    std::cout << "Enter in a coordinate between A-1 and D-4 (i.e. C4): "; 

    std::string guess; 
    if (std::cin >> guess) { 
     if (isGuessCorrect(guess, targets)) { return guess; } 

     return MissedTarget; 
    } 

    // Something that could not (unfortunately) be parsed, 
    // we need to clear std::cin 
    std::cin.clear(); 
    std::cin.ignore(std::numeric_limit<size_t>::max(), '\n'); 

    return MissedTarget; 
} 

static bool tryFewTimes(size_t const tries, std::set<Target>& targets) { 
    for (size_t n = 0; n != tries; ++n) { 
     Target const target = tryOnce(targets); 

     if (target == MissedTarget) { 
      std::cout << "Missed! Try again!\n"; 
      continue; 
     } 

     targets.erase(target); 

     std::cout << "Congratz! You got " << target 
        << "! Only " << targets.size() << " remaining\n"; 
     return true; 
    } 

    std::cout << "You flunked it, can't always win :(\n"; 
    return false; 
} 


int main() { 
    std::set<Target> targets = { "A1", "B1", "C1" }; 

    while (not targets.empty() and tryFewTimes(3, targets)) {} 
}