while loop problem

Q)Write a C++ program that takes a positive integer from user and store it in variable
posNumber. Follow these conditions;
If the number is less than 1, print wrong input.
If it is 1, Print its value.
If value is greate than 1, check the value is it Even or Odd.
If it is Even, half it and print.
If it is Odd, multiply it by 3 and print result.
Repeat the whole process until user enter 1.(use while loop for repetation of this whole process)

The problem in this code is that whenever i enter the even odd loop it runs infinite loop of the answer what should i modify in this program so that after the answer of even or odd is printed the program then loops back to "ENTER A POSITIVE INTEGER" until i press 1. Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include<iostream>
using namespace std;

int main()
{

la:cout <<"ENTER A POSITIVE INTEGER:";
    int posNumber=0;
    cin >> posNumber;

while(posNumber!=1)
{  //this part runs even if the while part is false
    if(posNumber<1)
    {
            cout <<"WRONG INPUT"<<endl;
            goto la;
    }
    else if(posNumber==1)
    {
        cout <<"Value is" <<posNumber;
    }
    else if(posNumber>1)
    {
       if((posNumber%2)==0)
        {

            posNumber/=2;
        cout << "value is "<<posNumber;
        //If it is Even, half it and print.
//If it is Odd, multiply it by 3 and print result.

       }
       else
            {

               posNumber*=3;
                cout << "value is "<<posNumber;


            }
    }

}
cout <<"\nTHANK YOU";

return 0;
}




REVISED CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
using namespace std;

int main()
{

int posNumber=0;

while(posNumber!=1)
{
     cout <<"ENTER A POSITIVE INTEGER:";

    cin >> posNumber;//this part runs even if the while part is false
    if(posNumber<1)
    {
            cout <<"WRONG INPUT"<<endl;

    }
    else if(posNumber==1)
    {
        cout <<"Value is " <<posNumber<<endl;;
    }
    else if(posNumber>1)
    {

      {

      if((posNumber%2)==0)
        {

            posNumber/=2;
        cout << "value is "<<posNumber<<endl;
        //If it is Even, half it and print.
//If it is Odd, multiply it by 3 and print result.

       }
       else
            {

               posNumber*=3;
                cout << "value is "<<posNumber<<endl;


            }
    }
    }

}

cout <<"\nTHANK YOU";

return 0;
}
Last edited on
You don't change posNumber at all, of course it goes into an infinite loop.
changed it and now when i enter 8 it shows value is 4 value is 2 value is 1 thank you and exits the loop where the output should be value is 4 and prompt for a positive integer
Not really. Your prompt for a positive integer is outside the loop, soo it doesnt ask again, move it to inside the loop.

Also. Dont use goto.

Instead of using goto inside that if statement, prompt for a input.
Last edited on
Thanks that worked :)
Topic archived. No new replies allowed.