HELP! If else statement
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4

Thread: HELP! If else statement

  1. #1
    Join Date
    May 2013

    HELP! If else statement

    I need to create a if else statement to check multiple model numbers (200+)... How can i build this? I basically want the user to enter their model number and if its on the list they get a approved or denied message.

    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?

    #include <iostream>
    using namespace std;
    int main(){
        int model;
        cout << "Enter model" << endl;
        cin >> model;
        if(model == 90||100)
                cout << "Model Approved" << endl;
                cout << "Model Denied" << endl;
        return 0;

  2. #2
    Join Date
    Jan 2006

    Re: HELP! If else statement

    Hint: model == 9002||1002 is equivalent to model == (9002||1002), which is probably not what you intended.

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999

    Re: HELP! If else statement

    Quote Originally Posted by blackb0x View Post
    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?

    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.


    Paul McKenzie

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.6.6)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)