While loop

not sure why, nether my while loop or my if else statement is not working on line 29 and 38 getting some sort of logic error from what I can tell on the while loop whether I press y or n still keeps looping eventhough I have yesOrno set condition to yesOrno != 'y'|| yesOrno != 'n' and as far as the if else statement well it is not reading remainder as 0 looks like it not sure y but you put 2.5 works fine but you put 5 doesn't work.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{
    
    double numberTwo,remainder;
    int number, count;
    char yesOrno;
    char endProgram;
    do 
    {
        cout << "Please enter an integer" << endl;

        cin >> numberTwo;
        number = numberTwo;
        remainder = numberTwo - number;
        
        if (number > 2147483647)
            {
                cout << "Need to enter an integer lower than 2147483647 please
                         enter another integer \n";
                cin >> numberTwo;
                number = numberTwo;
                remainder = numberTwo - number;
            }
        else if (number != 0)
            {
                cout << remainder << " will be trunicated from the "  
                     << numberTwo << endl;
                cout << "If you wish to continue with this integer type y \n";
                cout << "otherwise type in n and we will enter another
                         integer\n";
                cin >> yesOrno;
                cout << yesOrno << endl;
                
                while(yesOrno != 'n' || yesOrno != 'y')
                {
                    cout << "Please enter a valid y or n \n";
                    cin >> yesOrno;
                }
            }
        else if (yesOrno == 'n')
            {
                 cout << "Please enter an  integer" << endl;

                 cin >> numberTwo;
                 number = numberTwo;
                 remainder = numberTwo - number;
             }
             
        else if (yesOrno == 'y')
             {
                    cout << "Your floating point number would be \n";
                    cout << fixed;
                    cout << setprecision(10) << number << endl;
                    cout << endl;
                    cout << endl;
                    cout << "Would you like to end program or enter another
                             integer press y for yes and n for no \n";
                    cin >> endProgram;
                    while (endProgram != 'n' || endProgram != 'y')
                    {
                        cout << "This is not a valid entry! Please enter y for 
                                 yes or n for no \n";
                        cin >> endProgram;
                    }
            }
        else if (endProgram == 'y')
            {
                count++;
            }
        else if (endProgram == 'n')
            {
                count = 0;
            }
            
    } while (count <= 0 );     
                    
    system ("pause");
    
    return 0;
}
while(yesOrno != 'n' || yesOrno != 'y')

Can you think of a value for yesOrno for which
yesOrno != 'n' || yesOrno != 'y'
will not be true?

(No, you cannot; while(yesOrno != 'n' || yesOrno != 'y') might as well say while(true))
everything in else if....
Some things will not work correctly.

for example: say you entered a number,Then pressed y,It again go to ask input for number.It never ends.Or ends under certain circumstances.
well I was trying to elimate them typing wxtio and being able to brake the loop with the loop <while(yesOrno != 'n' || yesOrno != 'y'>

As far as the else if statement not sure what your talking of akshit can you please clearify for future reference.
You cannot do this!!

if (number > 2147483647)

Because any number bigger than INT_MAX would not fit in number in the first place, so the bits in number will be truncated at 32 and any number would be in fact smaller than INT_MAX.

You're if/else logic is messed up. You can't do an if for a number and do an else or else if for a string, well you can and it might work, but it's not consistent. Plus, I don't see a default case, the body of else is missing.
Last edited on
Topic archived. No new replies allowed.