-2

so I got this part of my code here:

string stringparam;

string stringparam1;

double d;

unsigned x;


cout << "Welcome" << endl;

cout << "Choose one (ASUS, MSI, Gigabyte, ASrock)" << endl;
cin >> stringparam;
while (stringparam != "ASUS" || "MSI" || "Gigabyte" || "ASrock")
{
    cout << "you gave bad input \n" << endl;
    cout << "Choose one (ASUS, MSI, Gigabyte, ASrock)" << endl;
    cin >> stringparam;
}

the problem is even if I give him a correct answer like "ASUS" it still gives me the error message : "you gave bad input" What can i do about it?

abaraj11
  • 3
  • 1
  • 1
    `stringparam != "ASUS" || "MSI" || "Gigabyte" || "ASrock"` doesn’t do what you think it does. It’s the same as `(stringparam != "ASUS") || "MSI" || "Gigabyte" || "ASrock"`, and `"MSI"` will always be true. You want `stringparam != "ASUS" && stringparam != "MSI" && ...`. – Biffen May 04 '21 at 09:23
  • 2
    Just blindly guessing syntax never works. Learn what `stringparam != "ASUS" || "MSI" || "Gigabyte" || "ASrock"` really means by reading about the operators. – klutt May 04 '21 at 09:23
  • thats the closest I could find: https://stackoverflow.com/questions/28144294/c-multiple-conditions-for-if-statement. Your condition is: "(stringparam is not "ASUS") or ("MSI") or ..." not "stringparam is not ("ASUS" or "MSI" or ...)" – 463035818_is_not_an_ai May 04 '21 at 09:24
  • 1
    `or` is the wrong operator anyway, even if you fix the more obvious problem of not comparing the string. A logical or is very different from the `or` as used in a natural spoken languange. – Devolus May 04 '21 at 09:25
  • thanks for the fast response guys I will try to look around a bit more – abaraj11 May 04 '21 at 09:26

3 Answers3

5

Just blindly guessing syntax never works. I recommend learning what stringparam != "ASUS" || "MSI" || "Gigabyte" || "ASrock" really means by reading about the operators.

You could do something like this to achieve what you want in a quite neat way. It requires C++11 or later.

std::set<std::string> ss = {"ASUS","MSI" , "Gigabyte","ASrock" };

while(ss.find(stringparam) == ss.end()) {
klutt
  • 30,332
  • 17
  • 55
  • 95
1

Here the main problem is that you can't do what you want with "or" operator, because in that way while condition it will always be True.

For example: if you type "ASUS", the result of while is True, this because you need ONLY one || expression = True to "come in" while's body (this caused infinite loop).

So you have to use "&" operator. With this, all condition MUST be True to execute while's body.

while (stringparam != "ASUS" && "MSI" && "Gigabyte" && "ASrock")

Alternatively, you can use the do/while loop:

do{
    cout << "you gave bad input \n" << endl;
    cout << "Choose one (ASUS, MSI, Gigabyte, ASrock)" << endl;
    cin >> stringparam;
}while(stringparam != "ASUS" && stringparam != "MSI" && ...);
  • it's working now thanks for the help – abaraj11 May 04 '21 at 09:40
  • You're welcome! Have a good day! :) – ElMaestroDeToMare May 04 '21 at 09:41
  • `cout << cout << ..` compiles? – 463035818_is_not_an_ai May 04 '21 at 11:10
  • 1
    @AndreaMaestrutti Good that you're taking it the right way. More often than not, such comments are completely without any bad intent. But I think this answer should be improved further. You're making the do-while the central thing, but that's not really related to the problem. It's just an improvement. But you don't explain anything what's done to actually solve the problem. – klutt May 04 '21 at 17:20
  • 1
    @AndreaMaestrutti I removed my downvote because you reacted so well. Correct the issues I mentioned and have an upvote. ;) – klutt May 04 '21 at 17:40
  • Ok thanks for suggestion, and sorry for the bad answer ;) but this morning i was at work with some bugs in the code, and that made me so nervous... – ElMaestroDeToMare May 04 '21 at 19:03
-3

Yes,your code goes in infinite loop Because ,your have used string with OR operator. And in C++ anything except O, null, empty string is positive. So,your while loop condition is always true. That's why your code goes in infinite loop.

  • 1
    Your explanation is wrong. Assuming that `O == 0`, and an empty string is also a string which would return true. What you probably meant is using `string.empty()` which is rather different. – Devolus May 04 '21 at 09:31