jeudi 1 février 2018

short form of if (private case) c++ [duplicate]

I'm trying to make a program that uses for cycle to continuously read and sum numbers. I have already done it , now I want to use short form of IF and it gives me compiling errors on the following line :

num == 0 ? break : sum += num;

errors : expected primary-expression before 'break' ; expected ':' before 'break' ; I have no idea why this is happening and will be thankful if someone can explain to me.

full code:

#include<iostream>
using namespace std;

int main() {
    int sum = 0;

    for (;;) {
        int num = 0;
        cin >> num;
        num == 0 ? break : sum += num;
    }
    cout << sum ;
}

Aucun commentaire:

Enregistrer un commentaire