urgently need help (while loop)

Dec 15, 2013 at 1:25pm
i keep getting run time error in this program. if I input a the loop still continue but if I put a for the second time, the loop will now stop and I can't figure out the bug. I want the loop to stop if user input either a,b,c,d to m. 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  // solution to question 2 chapter five

//write a menu program that  let the user
//select from a list of options, and if the
//input is not one of the options, reprint
//the list.

#include <iostream>
#include <string>
using namespace std; 

int main()
{

  string type = " ";

cout << "Welcome to xarm softdrinks shop" << endl << endl;

cout << "choose your order below! (a - n)" << endl << endl;
cout << "a - Coca-Cola \t\t b - Sprite" << endl; 
cout << "c - Fanta \t\t d - Limca" << endl; 
cout << "e - Mountain Dew \t f - Pepsi" << endl; 
cout << "g - 7-Up \t\t h - Maltina" << endl; 
cout << "i - Mirinda \t\t j - Malta Guiness \t" << endl; 
cout << "k - Malt \t\t l - Amstel Malta \t" << endl; 
cout << "m - maltina" << endl << endl;

cout << "Your order: ";

getline (cin, type);

while ((type != "a") || (type != "b") || (type != "c") || 
(type != "d") || (type != "e") || (type != "f") ||
 (type != "g") || (type != "h") || (type != "i") || 
(type != "j") || (type != "k") || (type != "l") ||
 (type != "m"))
{
    cout << endl;
    cout << "Invalid order! " << endl << endl;
    cout << "Choose from a - m only " << endl << endl;

    cout << "a - Coca-Cola \t\t b - Sprite" << endl; 
    cout << "c - Fanta \t\t d - Limca" << endl; 
    cout << "e - Mountain Dew \t f - Pepsi" << endl; 
    cout << "g - 7-Up \t\t h - Maltina" << endl; 
    cout << "i - Mirinda \t\t j - Malta Guiness \t" << endl; 
    cout << "k - Malt \t\t l - Amstel Malta \t" << endl; 
    cout << "m - maltina" << endl << endl;

    cout << "Your order again: ";

cin >> type; 

if ((type == "a") || (type == "b") || (type == "c") ||
 (type == "d") || (type == "e") || (type == "f") || 
(type == "g") || (type == "h") || (type == "i") || 
(type == "j") || (type == "k") || (type == "l") || 
(type == "m"))

{
 break; 
}

}



}
Last edited on Dec 15, 2013 at 1:29pm
Dec 15, 2013 at 2:28pm
Your while loop condition is false. Use AND instead of OR.

If you input "a", first condition(type != "a") is false, but next(type != "b") is true. Change it.

Cheers!
Last edited on Dec 15, 2013 at 2:28pm
Dec 15, 2013 at 3:10pm
but if I use &&. the whole condition will become false. I used || since one of the condition must be true.
Last edited on Dec 15, 2013 at 3:11pm
Dec 15, 2013 at 3:25pm
You can take whole condition from your if and negate it.
And you are right about the first part, my bad.
Dec 15, 2013 at 8:10pm
thanks
Topic archived. No new replies allowed.