0

my code doesnt want to read what is inbetween the comments and just skips it even though phone and snn is set to smaller than that the inside of the code

int num = 1;

 while (num > 0 && num < 2) {
    num = 1;

    string first = "chicken";
    string last = "salad";
    string full = "chicken salad";
    int phone = 1;
    int snn = 1;

    cout << "enter your first name: ";
    cin >> first;
    cout << "enter your last name: ";
    cin >> last;
    full = first + " " + last;
    cout << phone;
    // works all the way to here then doesnt read the while
    
    while (phone < 1000000000 && phone > 9999999999) {
            cout << "enter 10 digit phone nmber (with no dashes or parenthesis): ";
            cin >> phone;
        
    }
    while (snn < 100000000 && snn > 999999999) {
        cout << "enter 9 digit snn <with no dashes>: ";
        cin >> snn;

    }
// starts working again here
    cout << "\n\tT H A N K  Y O U\n\n";
    system("pause");
    return 0;
}
Godslayer
  • 1
  • 1
  • 5
    `phone` is initialized as `1`, making `phone > 9999999999` always false. Same thing for `snn`. Did you mean or (`||`)? – SuperStormer Apr 17 '21 at 18:15
  • You'll find the problem yourself in a second if you step through it in a debugger. – Caleb Apr 17 '21 at 18:16
  • What number is both < 1000000000 ***and*** > 9999999999? You need `||` in place of `&&`. – Adrian Mole Apr 17 '21 at 18:17
  • How can `phone` be < 1000000000 *and* > 9999999999? – G.M. Apr 17 '21 at 18:17
  • i had completly forgot about the || command thank you im used to making it inside the numbers instead of outside – Godslayer Apr 17 '21 at 18:19
  • @Caleb No need for a debugger - just enabling all compiler warning would work: *warning : overlapping comparisons always evaluate to false [-Wtautological-overlap-compare]* (Assuming `int` is big enough to hold 9999999999). – Adrian Mole Apr 17 '21 at 18:21
  • `9999999999` exceeds the value that can be stored in a 32-bit `int`, so `phone > 9999999999` will be `false` on any platform that supports a 32-bit `int` or smaller. Also (even if you have a `int` that is bigger than 32-bit which is rare) `1000000000` is less than `9999999999`, so there are no values that are simultaneously less than `1000000000` and greater than `9999999999` (which is what `phone < 1000000000 && phone > 9999999999` tests for). You probably need to use the `||` operator rather than `&&` (neither are a "command"). – Peter Apr 17 '21 at 18:39
  • Also, you really shouldn't be keeping a phone number as an integer, there's no reason to treat it as a numerical value. sure, it is made of numbers, but it is immutable, and more meant to represent a "key", than an actual integer number. please just keep phone numbers as a string. refer to the following stack overflow for a more in depth analisys of this question: https://stackoverflow.com/questions/23637057/why-is-it-best-to-store-a-telephone-number-as-a-string-vs-integer/23637154 – João Vítor Costa Apr 17 '21 at 19:03

1 Answers1

1

In both while loops, you have an && in place of an ||, meaning that you are asking for phone to be both less than 1000000000 and greater than 9999999999 at the same time, which will always return false.

UnsignedByte
  • 849
  • 10
  • 29