-1

I'm just starting so I'm trying to write a program which determine if a number is positive or negative.

#include <iostream>;

int step_function(int x) {
    
    int result = 0; 
    
    if (x > 0) 
        result = 1;
    else if (x < 0) 
        result = -1;
    else 
        result = 0;

    return result;
}

using namespace std;

int main() {
    
    int num;
    
    cout<< "please enter number : ";
    cin >> num;
    
    int a = step_function(num); 
    
    if (a == 1) 
        printf("%d is positive", num);
    else if (a == -1) 
        printf("%d is negative", num);
    else 
        printf(" it is zero");

    return 0;
} 
IrAM
  • 1,720
  • 5
  • 18
Im_new
  • 1
  • 2
    What input are you giving the program, what output are you expecting, and what output do you see? Please be very specific, and please edit your question with this information. – JohnFilleau Jan 14 '21 at 03:50
  • `#include ;` is obviously wrong – IrAM Jan 14 '21 at 04:02
  • you can replace `printf` with `cout` – IrAM Jan 14 '21 at 04:08
  • 1
    I can't reproduce any problems, please show a [mre] that actually causes an error. Please hardcode an example input (`num = ...`) to help us reproduce the problem. And please be more precise about what happens. – Lukas-T Jan 14 '21 at 06:36

2 Answers2

1

There is a few things you should do:

  • First things first you should get yourself a Good Book for C++.

  • Second thing is read why using namespace std; is a bad idea.

  • Lastly here is your code fixed. You needed to remove the semicolon as well as removing the printf(). I also removed the using namespace std; which made it more readable.

#include <iostream>

int step_function(int); //Function prototype

int main() {
    int num;
    std::cout << "please enter number : ";
    std::cin >> num;
    int a = step_function(num);
    if (a == 1)
        std::cout << num << " is postive"; 
    else if (a == -1)
        std::cout << num << " is negative";
    else std::cout <<" it is zero";

    return 0;
}


int step_function(int x) 
{
    int result = 0;
    if (x > 0) result = 1;
    else if (x < 0) result = -1;
    else result = 0;

    return result;
}

Geno C
  • 1,401
  • 3
  • 11
  • 26
0

Don't use semicolon after #include <iostream>. I think for C++ cout is more standard whereas printf is from C. You can also include printing of the text in the step_function. Also, it's better to use braces {} after if and else statements for clarity especially if the code becomes complex.

#include <iostream>
using namespace std;

void step_function(int x) {
    if (x > 0) {
        cout << x << " is positive" << endl;
    }
    else if (x < 0) {
        cout << x << " is negative" << endl;
    }
    else {
        cout << "it is zero" << endl;
    }
}

int main() {
    int num;
    cout<< "please enter number : ";
    cin >> num;

    step_function(num);

    return 0;
}
psquant
  • 21
  • 1
  • 6
  • 2
    Agree with most of the commentary, but... printing directly in `step_function` and returning nothing reduces the usability of `step_function` to cases where you merely want to print. Asker's version is better in my opinion. – user4581301 Jan 14 '21 at 05:26