A program that reads integers as long as they are alternately positive and negative. C++

Viewed 121

I'm new to programming in C++. I've been trying to create a program that reads integers as long as they are alternately positive and negative. I don't know how to make it without using if in if, and because of that it can read e.g (2, -2, 2, -2, -2, -2).

int temp = 0, x = 0, y = 0;
while (temp == 0){
    cin >> x;
    if (x > 0) {
        y = x;
        cin >> x;
        if (x < 0)
            continue;
        else if(y > 0 && x > 0)
            break;
    }
    if(x < 0){
        y = x;
        cin >> x;
        if (x > 0) 
            continue;
        else if(y < 0 && x < 0)
            break;
    }
}
7 Answers

I would read the first number outside the loop, and set a variable based on whether that number is positive or negative. Then I'd have the loop read numbers, and based on the variable know whether to expect a positive or negative number. If it gets what it expects, invert the value in the variable, and repeat. In something pseudo-codeish, something on this general order:

read(number)

// if this number's negative, expect the next to be positive (and vice versa)
expect_positive = number < 0

for (;;) {
    read(number)
    bool positive = number >= 0
    if (positive != expect_positive)
        break;
    expect_positive = !expect_positive;
}

Also, you could use property (a XOR b) < 0 if a and b have a different sign bit.

#include <iostream>

int main()
{
    int a, b;
    std::cin >> b;
    do {
        a = b;
        std::cin >> b;
    }while( (a^b) < 0);
    std::cout << "Same sign" << std::endl;
}
template<typename In, typename Out, typename F>
Out copy_while_alternative(In b, In e, Out r, F f)
{
    if (b == e) return r;
    
    bool lastF = f(*b);
    *r++ = *b++;
    while (b != e && f(*b) != lastF) {
        *r++ = *b++;
        lastF = !lastF;
    }
    return r;
}

https://godbolt.org/z/9rq93e

One possible implementation:

#include <iostream>

int main()
{
    int currentNumber, previousNumber;
    std::cin >> currentNumber;
    do
    {
        previousNumber = currentNumber;
        std::cin >> currentNumber;
    } while (currentNumber * previousNumber < 0);
}

Two numbers are opposite signs if their product is negative.

Move the first cin outside the loop

int temp = 0, x = 0, y = 0;
cin >>x;
if ( x !=0)
{
    while (temp == 0){
//cin >> x;// should be above as this because it resets the +- order every 2nd time
        if (x > 0) {
            y = x;
            cin >> x;
            if (x < 0)
                continue;
            else if(y > 0 && x >= 0)//>= to avoid infinite loop if 0 is input
                break;
        }
        if(x < 0){
            y = x;
            cin >> x;
            if (x > 0) 
                continue;
            else if(y < 0 && x <= 0)//<= to avoid infinite loop if 0 is input
                break;
        }
    }
}

The other answers are already good, but I wanted to give a solution that is close to yours, to show you how you can simplify things.

For example, you have a lot of unnecassary checks. You test x < 0, set y=x and then test y < 0 again.

You were missing to consider the starting case separately. The cin >> x should not happen unconditionally every loop iteration.

Here is a simplification:

int x = 0;
int last = 0; //more meaningful name
cin >> last; //you were missing some starting point
while (true){ //you do not need temp, instead just replace it by what you want to say
    cin >> x;
    if (x > 0 && last < 0) {
        last = x;   
        continue;
    }
    else if(x < 0 && last > 0){
        last = x;
        continue;
    }
    break;
}

Of course it is somewhat better to replace these checks by a product check to simplify things even more.

If only the oposite sign is needed then number * -1 will do fine.

#include <iostream>
#include <string>

int main(){
    int input_int;
    std::string input;

    int last = 2;

    while (1){

        std::cout << "Last int -> " << last << std::endl;

        std::cin >> input;
        input_int = std::stoi(input);

        if (last * -1 == input_int) {
            last = input_int;
        } else {
            break;
        }

    }
    return 0;
}
Related