Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 个人库,数字为正或负_C++ - Fatal编程技术网

C++ 个人库,数字为正或负

C++ 个人库,数字为正或负,c++,C++,使用个人库在C++中实现一个应用程序,该应用程序根据用户的选择确定一个数字是正数还是负数,或者一个数字是否为素数。 这是主要代码: #include <iostream> #include "libreria.cpp" using namespace std; int s; int main() { int num1,cont; cout<<"\n 1) Positive "; cout<<"\n 2) Prime "; cou

使用个人库在C++中实现一个应用程序,该应用程序根据用户的选择确定一个数字是正数还是负数,或者一个数字是否为素数。 这是主要代码:

#include <iostream>
#include "libreria.cpp"
using namespace std;
int s;
int main()
{
    int num1,cont;
    cout<<"\n 1) Positive ";
    cout<<"\n 2) Prime ";
    cout<<"\n 3) Exit ";
    cout<<"\n Choose: ";

    do
    {
        cin>>s;
        switch (s)
        {
        case 1:
            cout<<"\nInsert the number: ";
            cin>>num1;
            bool sepos(int numb);
            if (bool sepos(int numb)==1)
            {
                cout<<"\nIl numero "<<num1<<" e' positive";
            }
            else 
            {
                cout<<"\nIl numero "<<num1<<" e' negative";
            }
        break;
        case 2:
        break;
        }
    } while (s!=3);
    return 0;   
}
现在,我想看看这个数字是否为正。 但是这个应用程序不起作用,我犯了很多错误

[Error] function 'bool sepos(int)' is initialized like a variable 
[Error] expected primary-expression before '==' token 
[Error] expected '=' before '==' token 
[Warning] declaration of 'bool sepos(int)' has 'extern' and is initialized

我注意到有几件事你做错了

第一个是:
if(bool sepos(int numb)==1)
您试图将bool值(true或false)与数字1进行比较。是的,C++对待1和0就像是真的,但你的函数已经返回true或false。
#include <iostream>
#include "libreria.cpp"
using namespace std;
int s;
int main()
{
  int num1,cont;
  cout<<"\n 1) Positive ";
  cout<<"\n 2) Prime ";
  cout<<"\n 3) Exit ";
  cout<<"\n Choose: ";

  do
  {
    cin>>s;
    switch (s)
    {
    case 1:
        cout<<"\nInsert the number: ";
        cin>>num1;
        if (sepos(int numb))
        {
            cout<<"\nIl numero "<<num1<<" e' positive";
        }
        else 
        {
            cout<<"\nIl numero "<<num1<<" e' negative";
        }
    break;
    case 2:
    break;
    }
  } while (s!=3);
    return 0;   
}

我的建议是:试着多学一点这种语言的语法,你可以避免很多错误。

那么问题出在哪里呢?语句
true没有做任何有用的事情。你的意思可能是
返回true?如果(bool sepos(int numb)==1)
也是错误的。也许你应该花点时间阅读课堂笔记,或者阅读书本中关于调用函数的内容?花点时间阅读一篇甚至是一篇基本的在线教程。您调用函数的方式完全错误,为什么每次都重复函数声明?@Qualenome“我有很多错误”,为什么不在问题中提供所述错误?请不要执行
if(condition)return true之类的操作;否则返回false
–只需写入
返回条件而不是。。。
#include <iostream>
#include "libreria.cpp"
using namespace std;
int s;
int main()
{
  int num1,cont;
  cout<<"\n 1) Positive ";
  cout<<"\n 2) Prime ";
  cout<<"\n 3) Exit ";
  cout<<"\n Choose: ";

  do
  {
    cin>>s;
    switch (s)
    {
    case 1:
        cout<<"\nInsert the number: ";
        cin>>num1;
        if (sepos(int numb))
        {
            cout<<"\nIl numero "<<num1<<" e' positive";
        }
        else 
        {
            cout<<"\nIl numero "<<num1<<" e' negative";
        }
    break;
    case 2:
    break;
    }
  } while (s!=3);
    return 0;   
}
bool sepos(int numb)
{
    return numb>=0;
}