2016-03-07 100 views
-4

我在嵌套if语句中不断收到预期的表达式错误。我不明白是什么问题,因为我在代码中有其他的语句格式化(就我所见),完全相同的方式不会产生错误!嵌套if语句中的预期表达式错误C++

这是用于第一年C++类中的一项任务,它根据用户输入数据计算保龄球分数。

#include <iostream> 

using namespace std; 

int main() { 

    // Set Variables 
    int userin_1; 
    int userin_2; 
    int userin_3; 
    int combined2_3; 
    int score; 


    // Query User 

    cout << "Welcome to Josh's Bowling Score Calculator! " << "Please enter your three scores. " 
    << "They should be between 0 and 10." << endl << endl; 

    cin >> userin_1 >> userin_2 >> userin_3; 

    // Verify input data 

    if (userin_1 <= 10 && userin_2 <=10 && userin_3 <=10) 
     userin_1 = userin_1; 
     else cout << endl << "Input Error: You cannot score greater than 10 in one turn! :|" << endl; 

    if (userin_1 >= 0 && userin_2 >=0 && userin_3 >=0) 
     userin_1 = userin_1; 
    else cout << endl << "Input Error: You cannot score less than 0 in one turn! :|" << endl; 

    if (userin_1 == 10) 
     userin_1 = userin_1; 
     else if (userin_1 + userin_2 <= 10) 
      cout << "Input Error: You can't score more than 10 on your first two throws unless your first throw is a strike! " 
      << endl; 

    if (userin_2 == 10) 
     userin_2 = userin_2; 
    else if (userin_2 + userin_3 <= 10) 
     cout << "Input Error: You can't score more than 10 on your first two throws unless your first throw is a strike! " 
     << endl; 

    // Calculate Score 

    combined2_3 = userin_2 + userin_3; 

    if (userin_1 + userin_2 < 10) 
    { 
     score = userin_1 + userin_2; 
     cout << "Because you scored less than 10 on your first two throws, your last score doesn't count :[ " 
    << "Your score is: " << score << endl; 
    } 
     else 
     { 
     if (userin_1 == 10 && userin_2 == 10 && userin_3 == 10) 
     score = userin_1 + userin_2 + userin_3; 
     cout << "Wowow 3 strikes! Your score is " << score << endl; 
     } 
      else 
      { 
      if (userin_1 == 10 && userin_2 == 10) 
      score = userin_1 + userin_2 + userin_3; 
      cout << "Two strikes! Your score is " << score << endl; 
      } 
       else 
       { 
       if (userin_1 == 10 && userin_2 + userin_3 >= 10) 
        cout << "It's not possible to score greater than 10 on your last two throws, unless your first throw is a strike :| " << endl; 
       } 
        else 
        { 
         if (userin_1 == 10 && combined2_3 >= 10) 
          score = userin_1 + userin_2 + userin_3; 
         cout << "Nice, a strike! Your score is " << score << endl; 
        } 
         else 
         { 
          if (userin_1 == 0 && userin_2 == 0 && userin_3 == 0) 
           cout << "Donny, you're out of your element. All zeroes." << endl << endl; 
         } 


    // Closing Statement 

    cout << endl << "Thanks for bowling with me, hope you have a great day"; 




} 
+3

你有(太)嵌套和不一致/不好的缩进级别。这对我们(以及大多数对您)正确缩进您的代码会非常有帮助。 – Borgleader

+1

这不是问题,但不要使用'std :: endl',除非你需要额外的东西。 ''\ n''结束一行。 –

+1

这也不是问题,但要学习DeMorgan的定理,以便不必像'if(userin_1 <= 10 && userin_2 <= 10 && userin_3 <= 10)userin_1 = userin_1;否则无论();'。你可以这样写:'if(userin_1> 10 || userin_2> 10 || userin_3> 10)whatever();'。 –

回答

0

编辑:我错了,这里的错误:

else 
    { 
    if (userin_1 == 10 && userin_2 == 10 && userin_3 == 10) 
    score = userin_1 + userin_2 + userin_3; 
    cout << "Wowow 3 strikes! Your score is " << score << endl; 
    } 
     else 
     { 
     if (userin_1 == 10 && userin_2 == 10) 
     score = userin_1 + userin_2 + userin_3; 
     cout << "Two strikes! Your score is " << score << endl; 
     } 

第二else没有收到相关if。将if的条款移到相应的else条款中。

else if (userin_1 == 10 && userin_2 == 10 && userin_3 == 10) { 
     score = userin_1 + userin_2 + userin_3; 
     cout << "Wowow 3 strikes! Your score is " << score << endl; 
    } else if (userin_1 == 10 && userin_2 == 10) { 
     score = userin_1 + userin_2 + userin_3; 
     cout << "Two strikes! Your score is " << score << endl; 
    } 

但是,这里更严重的问题是您接近任务的方式。此任务的嵌套if语句的方式过多。你在条件上重复几次,而正确的方法是只检查一次,然后分支。

此外,之类的语句

if (userin_1 <= 10 && userin_2 <=10 && userin_3 <=10) 
    userin_1 = userin_1; 

没有任何意义,则应该检查的对面,完全跳过else条款。

您还应该使用正确的缩进,因为它极大地帮助提高了代码的可读性并有助于避免类似这样的错误。

+0

谁留下了-1,请解释一下。 – iksemyonov

+0

OP询问代码中的错误,我发现它。请撤销。我认为OP不理解if-else的作品是如何编写的,而不是编码风格。 – iksemyonov

+1

没有机会。 OP可能甚至不知道他可以通过不写这样糟糕的代码开始而自己发现它。你应该真的帮助他。 – Puppy