looking for some solutions? You are welcome.

SOLVED: Why my program displays the output multiple times


could someone help me to fix this code. I need my output to display Invalid value input when enter a value equal or lower than zero, However,the output display hundreds of lines. I found some examples adding a (++;)to the value to fix this problem. but, If i use ++ the output displays ( invalid number and a -number multiple times. If i change to -- the output displays invalid value input hundred of times again.

Thanks for your help

`#include <iostream>

using namespace std;
int main()
    int c0 ;

    cout << " Enter a non negative number greater than 0: ";
    cin >> c0;

    while (c0 != 1)
        //if number is odd  i divide it by 2 i have a remainded of 1
        //if number is even i divide it by 2 i have a remainded of 0


        if (c0 % 2 == 1)

            c0 = (3 * c0) + 1;

         if (c0 <= 0)
            cout << "Invalid value input " ;


            c0 /= 2;

        cout << c0 << '\n';


Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots

No comments: