baozhadehulu
baozhadehulu
采纳率52.6%
2015-05-20 13:16

C++中的这个问题怎么解决

已采纳

#include
int main()
{
int base, exponent;
std::cout << "Please enter the base and the exponent :" << std::endl;
std::cin >> base >> exponent;
long result = 1;
if (base == 0 && exponent == 0)
{
result = 0;
std::cout << "the " << exponent << " power of "
<< base << " is " << result;

}
else if (base == 0 && exponent != 0)
{
result = 0;
std::cout << "the " << exponent << " power of "
<< base << " is " << result;
}
else if (base != 0 && exponent == 0)
{
result = 1;
std::cout << "the " << exponent << " power of "
<< base << " is " << result;
}
else

{}
if ( exponent < 0)
std::cout << " Exponent can't be smaller than 0." << std::endl;
else
{
for (int i = 1; i <= exponent; i++)
result = result * base;
std::cout << "the " << exponent << " power of "
<< base << " is " << result;
std::cout << std::endl;
}
}
return 0;
}

这是怎么回事,为什么会出现这种情况?
[Error] expected unqualified-id before 'return'
[Error] expected declaration before '}' token

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • gamefinity 知常曰明 6年前

    你多了一个}。根据你代码的意思,应该是else下一个空的{}中的}应该不要吧

    点赞 1 评论 复制链接分享
  • wsh1992o 存钱买冰棍 6年前

    return 0;前面多了一个 }

    点赞 3 评论 复制链接分享
  • qingrenufo qingrenufo 6年前

    将return 0 前面的那个}去掉就对了

    点赞 1 评论 复制链接分享
  • 91program 91program 6年前

    这种括号匹配的错误,最好是借助编辑工具来查。
    VS 快捷键:CTRL + ]
    UltraEdit 快捷键: CTRL +B

    点赞 1 评论 复制链接分享
  • danielinbiti danielinbiti 6年前

    最好把你的if else整理成有条理些。上面的if else可以改成如下,看着条件就清晰了

     int main()
    {
    int base, exponent;
    std::cout << "Please enter the base and the exponent :" << std::endl;
    std::cin >> base >> exponent;
    long result = 1;
    if(base==0){//base分成等于0和不等于0
         if(exponent == 0){//exponent分成等于0和不等于0
            result = 0;
            std::cout << "the " << exponent << " power of " 
            << base << " is " << result;
         }else{
             result = 0;
            std::cout << "the " << exponent << " power of " 
            << base << " is " << result;
         }
    }else{
        if(exponent == 0){//exponent分成等于0,小于0,以及大于0
            result = 1; 
            std::cout << "the " << exponent << " power of " 
            << base << " is " << result; 
        }else if ( exponent < 0)
            std::cout << " Exponent can't be smaller than 0." << std::endl;
        else{
            for (int i = 1; i <= exponent; i++){
                result = result * base;
            }
            std::cout << "the " << exponent << " power of " 
            << base << " is " << result;
            std::cout << std::endl;
        }
    }
    return 0;
    }
    
    点赞 1 评论 复制链接分享
  • autocyz autocyz 6年前

    恩 ,return 0;前面那个}是多余的,编译器会认为这个括号和main的{相匹配,所以对于后面的return会报错。

    点赞 1 评论 复制链接分享
  • cuiwei1026522829 另一花生 6年前

    我看了一下,return上面那个括号多余了,你测试出来了么

    点赞 1 评论 复制链接分享

相关推荐