Re: HELP! If else statement
Hint: model == 9002||1002 is equivalent to model == (9002||1002), which is probably not what you intended.
EDIT:
Well, that would be a hint to fix this particular problem. For a more manageable solution to your problem... have you learned about say, std::set or std::unordered_set? Or have you learned about the switch construct?
Re: HELP! If else statement
Quote:
Originally Posted by
blackb0x
So far I have this, the "if(model == 9002||1002)" doesnt seem right, it needs to basically check 200+ model numbers... am I even on the right track?
No.
First, C++ is not English. When we speak, we may say "if model equals 9002 or 1002 or 678 or 890", but that is not the syntax used by C++ when comparing multiple values to another value/variable. In C++, you have to say "if model equals 9002 or model equals 1002 or model equals 678" etc... Does that give a clue of how the if() statement is to be constructed?
Second, to empahsize laserlight's point, even if you get it right with the if() statement, are you really going to construct an if() statement with over 200 comparisons? What if there were 300, 400, or 10,000 model numbers to compare? It would be better to store the numbers in a container (array, vector, std::set, etc.) where you just call a simple function or at least, write a loop to see if the number is in the container.
Regards,
Paul McKenzie
Re: HELP! If else statement
Are the model numbers consecutive (or at least some consecutive) or are they all disjoint or don't you know? If they are consecutive then you can simplify things somewhat by using one or more range tests. If they are disjoint then you would be better off building a look-up table using a container (if you are familiar with these) or just a simple array of the model numbers from which to test. IMO building if statements (or even using a switch) for several hundred comparisons would be a non-starter for me!
Another approach would be to use a data file containing the model numbers to check. The program would then simply read these model numbers from the file into a container (or array) and then check the entered model number against those stored. In this way if the model numbers to check changed then all that would be required would be the data file to be changed. The program wouldn't need to be altered.