什么是给定代码中的错误? [英] Whats is the error in given code ?

查看:59
本文介绍了什么是给定代码中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
#include<string>
int main()
{
	std::string name;
	int a;
	std::cout<<"welcome please enter you name below"<<std::endl;
	std::cin>>name;
	std::cout<<"hey ";
	std::cout<<name<<std::endl;
	std::cout<<"//choose a for submision//"<<std::endl;
	std::cout<<"//choose b for factorial//"<<std::endl;
	std::cout<<"you choosed : ";
    std::cin>>a;
    switch (a)
    {
    case (1):
    int limit, result=0;
    std::cout<<"enter num = ";
    std::cin>>limit;
    for(int x=1;x<=limit;x++)
    {
    	result=result+x;
    }
    std::cout<<"sum of number= "<<result;
    break ;
    case (2):
    int fact=1,l;
    std::cout<<"enter limit";
    std::cin>>l;
    for(int i=1;i<=l;i++)
    {
    	fact=fact*i;
    }
    std::cout<<fact;
    break;
    }
}





我的尝试:



每次编译时都显示

< stdin>:27:5:错误:无法从switch语句跳转到此case标签

case(2):

^

< stdin> ;:18:16:注意:跳转绕过变量初始化

int limit ,结果= 0;

^

1错误产生。



不知道该怎么办?请帮助我。



What I have tried:

Every time I compile it show
<stdin>:27:5: error: cannot jump from switch statement to this case label
case (2):
^
<stdin>:18:16: note: jump bypasses variable initialization
int limit, result=0;
^
1 error generated.

Don't know what to do? Please Help me.

推荐答案

在您的案例代码周围加上大括号:

Put curly brackets round your case code:
#include<iostream>
#include<string>
int main()
{
	std::string name;
	int a;
	std::cout<<"welcome please enter you name below"<<std::endl;
	std::cin>>name;
	std::cout<<"hey ";
	std::cout<<name<<std::endl;
	std::cout<<"//choose a for submision//"<<std::endl;
	std::cout<<"//choose b for factorial//"<<std::endl;
	std::cout<<"you choosed : ";
    std::cin>>a;
    switch (a)
    {
        case 1:
        {
            int limit, result=0;
            std::cout<<"enter num = ";
            std::cin>>limit;
            for(int x=1;x<=limit;x++)
            {
            	result=result+x;
            }
            std::cout<<"sum of number= "<<result;
            break ;
        }
        case 2:
        {
            int fact=1,l;
            std::cout<<"enter limit";
            std::cin>>l;
            for(int i=1;i<=l;i++)
            {
            	fact=fact*i;
            }
            std::cout<<fact;
            break;
        }
    }
}







Quote:

错误是< stdin> ;:27:12:错误:无法从switch语句跳转到此case标签case(2):^< stdin> ;:18:24 :注意:跳转绕过变量初始化int limit,result = 0;生成了^ 1错误。

the error is <stdin>:27:12: error: cannot jump from switch statement to this case label case (2): ^ <stdin>:18:24: note: jump bypasses variable initialization int limit, result=0; ^ 1 error generated.





我的编译器没有这个,所以试着移动中断大括号外:



I don't get that with my compiler, so try moving the break outside the curly brackets:

#include<iostream>
#include<string>
int main()
{
	std::string name;
	int a;
	std::cout<<"welcome please enter you name below"<<std::endl;
	std::cin>>name;
	std::cout<<"hey ";
	std::cout<<name<<std::endl;
	std::cout<<"//choose a for submision//"<<std::endl;
	std::cout<<"//choose b for factorial//"<<std::endl;
	std::cout<<"you choosed : ";
    std::cin>>a;
    switch (a)
    {
        case 1:
        {
            int limit, result=0;
            std::cout<<"enter num = ";
            std::cin>>limit;
            for(int x=1;x<=limit;x++)
            {
            	result=result+x;
            }
            std::cout<<"sum of number= "<<result;
        }
        break;
        case 2:
        {
            int fact=1,l;
            std::cout<<"enter limit";
            std::cin>>l;
            for(int i=1;i<=l;i++)
            {
            	fact=fact*i;
            }
            std::cout<<fact;
        }
        break;
    }
}

丑陋,不应该需要 - 您使用的是什么编译器?

Ugly, and shouldn't be needed - what compiler are you using?


您必须为您的交换机指定范围案例

You must give scope to your switch cases
#include<iostream>
#include<string>
int main()
{
  std::string name;
  int a;
  std::cout<<"welcome please enter you name below"<<std::endl;
  std::cin>>name;
  std::cout<<"hey ";
  std::cout<<name<<std::endl;
  std::cout<<"//choose 1 for submision//"<<std::endl;
  std::cout<<"//choose 2 for factorial//"<<std::endl;
  std::cout<<"you choosed : ";
  std::cin>>a;
  switch (a)
  {
  case 1:
    {
      int limit, result=0;
      std::cout<<"enter num = ";
      std::cin>>limit;
      for(int x=1;x<=limit;x++)
      {
        result=result+x;
      }
      std::cout<<"sum of number= "<<result;
    }
    break ;
  case 2:
    {
      int fact=1,l;
      std::cout<<"enter limit";
      std::cin>>l;
      for(int i=1;i<=l;i++)
      {
        fact=fact*i;
      }
      std::cout<<fact;
    }
    break;
  }
}





现代 C ++ 你可以写



In modern C++ you could write

#include <iostream>
#include <functional>
#include <array>
#include <tuple>
using namespace std;

int main()
{
  array< tuple < string,  function<int (int, int)> >, 2 > af =
  {{
    { "sum", plus<int>() },
    { "factorial", multiplies<int>() }
  }};

  cout << "please enter ";
  for ( size_t n=0; n<af.size(); ++n)
    cout << (n+1) << " for " << get<0>(af[n]) << " ";
  cout << "\n";

  size_t choice;
  cin >> choice;
  --choice;
  if ( choice >= af.size())
  {
    cerr << "invalid choice\n";
    return -1;
  }

  const  auto & [ name, fun ] = af[choice];

  cout << "plaese enter the limit\n";
  size_t limit;
  cin >> limit;

  size_t result = 1;
  for (size_t n=2; n<=limit; ++n)
    result = fun(result, n);

  cout << "result of " << name << " is " << result << endl;

}


这篇关于什么是给定代码中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆