2013-04-28 22 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)) {} 
}