0

Implement in C ++, using a personal library, an application that determines based on the choice made by the user if a number is positive or negative or if a number is prime or not. This is the main code:

#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;   
}

The library is:

bool sepos(int numb)
{
    if(numb>=0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

For now, I was trying to see if the number was positive or not. But the application won't work, i got a lot of errors.

[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
Qualenome
  • 15
  • 7
  • 5
    So what is the problem? – Ali Kanat Jan 27 '20 at 12:55
  • What is your question? – MadScientist Jan 27 '20 at 12:55
  • 2
    The statement `true;` doesn't do anything useful. Do you perhaps mean `return true;`? And `if (bool sepos(int numb)==1)` is wrong as well. Perhaps you should take some time to go through your class notes or read about calling functions in your books? – Some programmer dude Jan 27 '20 at 12:57
  • Also please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jan 27 '20 at 12:57
  • The problem is i got a lot of errors and the application won't work. I fixed the return true; but still it doesn't work. – Qualenome Jan 27 '20 at 12:59
  • 3
    Take your time to read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or even a basic online tutorial. The way you call functions is completely wrong, why do you repeat the function declaration every time? – Lukas-T Jan 27 '20 at 13:01
  • 2
    @Qualenome "_i got a lot of errors_", and why didn't you provide said errors in the question? – Algirdas Preidžius Jan 27 '20 at 13:01
  • 2
    Please don't do stuff like `if(condition) return true; else return false;` – just write `return condition;` instead... – Aconcagua Jan 27 '20 at 13:01
  • And sorry, i still go to school, i'm not so good with the custom libraries at the moment, this is my first time in this website. – Qualenome Jan 27 '20 at 13:03
  • Just some general advice: 1. Good formatting and proper indentation makes the code much more readable. 2. Try to write your code in little pieces: Compile after you finished a short segment, and if it does already something meaningful, run it to check (you might possibly add some test output). Then after having completed that successfully, add next segment. Using an IDE will help you pretty much in that, e. g. providing automated code formatting. If you want to go the little steps with your programme: don't throw it away, just comment out what you are not going to test now... – Aconcagua Jan 27 '20 at 13:06
  • Alright, thanks. I got a lot to learn, please don't attack me XD – Qualenome Jan 27 '20 at 13:07
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Jan 27 '20 at 13:07
  • I suggest you to understand the compilation errors and warnings then fix one by one. I tried to fix for you [here](https://onlinegdb.com/r1l5ewn-U) – TruthSeeker Jan 27 '20 at 13:08
  • Thanks @TruthSeeker, now the application works fine. I will try to learn all of my errors you guys told me. Thanks to all. – Qualenome Jan 27 '20 at 13:11
  • @Qualenome "_I got a lot to learn, please don't attack me_" Do note, that criticism is not an attack. If you take all criticism as personal attacks - you won't get far. – Algirdas Preidžius Jan 27 '20 at 13:24
  • Yeah, you're right, my bad. – Qualenome Jan 27 '20 at 13:29

1 Answers1

0

I've noticed a couple of things you are doing wrong.

The first one is: if (bool sepos(int numb)==1) you are trying to compare a bool value ( true or false ) with the number one. Yes, C++ treats 1 and 0 like true or false but your function already returns true or 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;   
}

The other issue is: your function bool sepos(int numb) is declared to return a boolean value, you don't have to cast it.

numb>=0 returns true or false, you don't have to return those values explicitly.

bool sepos(int numb)
{
    return numb>=0;
}

My Advice: Try to learn a bit more about the syntax of the language, you can avoid a lot of mistakes.

Gabriel Lidenor
  • 2,905
  • 2
  • 25
  • 26