面对c ++代码中的错误

时间:2016-06-03 20:13:15

标签: c++

我正在编写ACM问题的代码,我们必须检查不同项目的可能性。这是代码中的一些小错误。

#include<iostream>
using namespace std;
void CheckPossibilities( int numItems, int maxWeights )
{
    if( numItems <= 0  )
    {
        cout << "Invalid Items";

    }

    if ( maxWeights <= 0 )
    {
        cout << "Impossible";

    }

    while( maxWeights > 0 )
    {
        if(numItems%2==0)                //for even
        {
        numItems = numItems / 2;
        maxWeights--;
        }
        else 
        {
             numItems = (numItems -1)/ 2;      //for odd
             maxWeights--;
        }

    }

    if( numItems <= 1 )
    {
        cout << "Possible";
    }
    else
    {
        cout << "Impossible";
    }

}

void main()
{

    int  numItems1,maxWeights1;
    cout<<"enter numItems"<<endl;
    cin>>numItems1;
    cout<<"maxWeights"<<endl;
    cin>>maxWeights1;
    cout<<numItems1 "AND" maxWeights1<<endl;
cout<<CheckPossibilities(numItems1, maxWeights1);
}

2 个答案:

答案 0 :(得分:1)

你的错误是试图在一行中输入多个字符串而不以某种方式将它们连接起来,或者用&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;或者+。你也不能尝试一个void函数,因为它试图输出void,你只需要调用它并让函数进行输出。修复错误后,主要应该是

int main()
{

    int  numItems1,maxWeights1;
    cout<<"enter numItems"<<endl;
    cin>>numItems1;
    cout<<"maxWeights"<<endl;
    cin>>maxWeights1;
    cout<<numItems1+"AND"+maxWeights1<<endl;
    CheckPossibilities(numItems1, maxWeights1);
    return 0;
}

下次查看在编译和搜索这些特定错误时引发错误的行,因为这些是非常简单且特定的语法错误,可以通过Google搜索轻松找到。

答案 1 :(得分:-3)

void main()
{

  int  numItems1, maxWeights1;
  cout << "enter numItems" << endl;
  cin >> numItems1;
  cout << "maxWeights" << endl;
  cin >> maxWeights1;
  cout << numItems1 <<  "AND" << maxWeights1 << endl;
  CheckPossibilities(numItems1, maxWeights1);
}

你永远不能这样做:cout<<CheckPossibilities(numItems1, maxWeights1); cout采用标准输出流,而不是函数。而你也忘了把&lt;&lt;在此表单的cout << numItems1 << "AND" << maxWeights1 << endl;中,您的代码构建成功。