Thread: simple check.

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    21

    simple check.

    this was the only coding exercise that I've had trouble with so far on this great learning adventure

    just looking to see if there was a better way to do this, or if any tips could be thrown my way. the program runs well.
    cheers




    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main (){
       int answer;
       int obama = 0;
       int romney = 0;
       int option3 = 0;
         while (obama < 5 && romney < 5 && option3 < 5)
         {
          cout<< " who would you like to see become president?\n";
          cout<< "1. obama\n2. romney\n3. The bank owns the government.\n";
          cin>> answer;
          
          if (answer == 1){
               obama++;
               }
                  
          else if (answer == 2){
               romney++;
               }
                                       
          else if (answer == 3){
               option3++;
               }
               else {
                    cout<< "please enter a number 1-3\n";
                    }                   
          }    
          cout<< "obama- " << obama << "\n";
          cout<< "romney- " << romney << "\n";
          cout<< "doesn't matter- " << option3 << endl;
                        
                                
          
       
       
                          cin.ignore ();
                          cin.get ();
                          
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I suggest giving the user the message "please enter a number 1-3\n" from the beginning.I can not understand the purpose of lines 41,42.You get the user to input something without a reason.
    A non programming tip would be i think,why all the inputs have the same value?I mean somebody may wants very much to select one president,that you may give the ability to the user to input a weight to his input.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    I assumed choosing 1-3 was obvious, i only put the "please choose between 1-3" in as a default message to anything BUT 1-3.
    I'm still early in learning and lines 40+41 are the only way I know of (for now) to keep the program open for me to see the results if any option gets to 5.
    also it is meant to be a "poll" exercise, so that user(s) cast a vote, it is stored, and when there is a "winner" (first to hit 5) it will be displayed. imgaine 17 people in a circle passing my laptop around, entering 1-3. (VERY unpractical but again this was just an exercise to practice loops, Boolean, and If statements

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok i see.It is too obvious ,i agree.But your programs in general have to be easy to use to everyone!Even though people who are not familiar with pc'es.Imagine an old man who does not know anything about computers.I believe that a programmer ought to help the user inputs something acceptable.
    Another think i would change,is that at the end of the program you could print who the winner is and not all the results,or both(the results and the winner )
    As for lines 40-41 when you start writing at an IDE(e.g. netbeans) you should not need them.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    very true, I'll make it a habit of being specific, as i can see it could cause problems down the road.. printing the winner would make more sense
    thanks mate

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I also suggest you think in terms of extensibility. For each option you have supported, you have copied similar code, added a new variable, and a string corresponding to that variable.

    Can you think of a way of structuring your code so, if you want to add or remove options (say, if Obama decides not to run again for the presidency, or some other politician does) you can do it with minimal changes, rather than having to duplicate multiple bits of code (as you are)?

    That may sound like gilding the lily, but I can probably do the same thing as you with half the lines of code, and STILL be able to evolve it more easily than you can.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    I'm trying.. really hard.. to think of another way....... but i can't. I don't think I know enough fundamentally to re-structure this into a more efficient code :/ are you speaking of my if statements when referring to duplicating? from my current perspective I cannot see how I would be able to compact this, I just finished learning loops (still trying to grasp efficient use of them) the other day, and I'm currently trying to learn about functions outside of main. Its hard for me to fully understand because the examples are so... simple..
    This was an example given for me.. Surely I understand its meant to be simple for learning purposes.. but I won't understand it if I don't see a viable use for it.

    Code:
    #include <iostream>
    using namespace std;
    
    int multiply (int x, int y);
    
    int main ()
    {
         int x;
         int y;
         cout<< "please enter two numbers to be multiplied.\n";
         cin>> x, y;
         cin.ignore();
         cout<< "the product of your two numbers is " << int multiply (x, y) << "\n" //why wouldn't i just enter x * y..
         cin.get();
    //do i need a return 0; here?
    }
    
    int multiply (int x, int y);
    {
         return x * y;
    }

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    As for lines 40-41 when you start writing at an IDE(e.g. netbeans) you should not need them.
    Yes you will - The ide console runner may provide the halt on the window disappearing after execution- but using the build debug or release version would not - unless the programmer provides it.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Its hard for me to fully understand because the examples are so... simple..
    I hear you, but the idea is that you need to extrapolate from the simple examples and see how it maps to your own code and requirements - you can't expect to find an exact match for your programming goal every time you do a search, so if you hit related tutorials etc then you will get good at taking what you need from them- in terms of learning and concept.

    //do i need a return 0; here?
    Yes! that is the standard - Elysia has posted some good links on this if i recall, maybe she will drop one in this thread for you.

    int multiply (x, y) << "\n" //why wouldn't i just enter x * y..
    absolutley, you could if you wanted - obviously other things would have to change to accomodate this - your function defintion as it stands needs two arguments - x , y - If you just passed in a*b then that would only be one argument, ie a single value.

    And ignoring functions, if what you meant was "couldn't i just put x*y in the cout statement?" then yes - that is valid also.

    Maybe for practice, rewrite it using the function with your x*y idea, remebering the function defintion itself, and it's return staement will change.. by the way -get rid of the 'int' part, you dont need to declare the return type when you call the function - you have already declared it at the top of your file in the prototype; function calls only require their name and args.
    Last edited by rogster001; 07-15-2012 at 12:31 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    i spotted a mistake while rewriting the code, line 11 should have been written cin>> x >> y; ugh caused like 15 mins worth of frustration over two arrows.. is this the life of a programmer!? lol anyways, if i replaced the multiply function with x * y (one arguement, right?) then wouldn't the program still work, since I'm simply not using 'multiply' at all ?
    Im slowly getting the hang of this.. thanks guys.
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int multiply (int x, int y);
    
    
    int main ()
    {
     int x;
     int y;    
        cout<< "enter two numbers to multiply\n";
        cin>> x >> y;
        cin.ignore();
        cout<< "product of two numbers is " << x * y << "\n"; //if i DID call the function here, it would only be << multiply (x, y) << correct?
        cin.get();
        return 0;
    }
    
    
    int multiply (int x, int y)
    {
        return x * y;
    }
    Last edited by Mike Beal; 07-15-2012 at 01:32 PM.

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    //
    Code:
    #include <iostream>
    
    using namespace std;
    
    int multiply(int multipliedValue)
    {
        return multipliedValue;
    }
    
    int main()
    {
        int val = 5;
    
        cout << val * val << "\n";
    
        cout << multiply(val * val);
    
        return 0;
    }
    //
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    so essentially, its the same thing, functions are just to organize code a little better? I suppose if I were to use x*y (or another, more complicated equation) very often it would make sense to put it into a function that i could just call multiple times.. thanks alot mate, as of yesterday I was utterly lost on the subject of functions, now I've got a solid grasp on them.. next up, Switch Case!! expect another NOOBY post by me soon
    cheers

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You need to look at more code - Its much more regular to place variables in the function call - rather than maths - sometimes its handy to do like in recursion where you can alter the value of the variable passed in with -- or ++ or whatever.
    Check out the 'How do i...' pages of the faq's they are really very useful - i remember i printed off all the stuff i was interested in in a booklet form, quick lookup then when needed.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Mike Beal View Post
    I'm trying.. really hard.. to think of another way....... but i can't.
    Alright, I'll take pity.

    This is your code from your first post
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
     
    int main (){
       int answer;
       int obama = 0;
       int romney = 0;
       int option3 = 0;
         while (obama < 5 && romney < 5 && option3 < 5)
         {
          cout<< " who would you like to see become president?\n";
          cout<< "1. obama\n2. romney\n3. The bank owns the government.\n";
          cin>> answer;
           
          if (answer == 1){
               obama++;
               }
                   
          else if (answer == 2){
               romney++;
               }
                                        
          else if (answer == 3){
               option3++;
               }
               else {
                    cout<< "please enter a number 1-3\n";
                    }                  
          }   
          cout<< "obama- " << obama << "\n";
          cout<< "romney- " << romney << "\n";
          cout<< "doesn't matter- " << option3 << endl;
                         
                                 
           
        
        
                          cin.ignore ();
                          cin.get ();
                           
    }
    Now, compare it with my version. What does it do that your code does not (other than some minor changes of what the program outputs, and that I've removed the cin.get()/cin.ignore() closing)? How easy it it to extend?

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main ()
    {
       vector<string> label;
       label.push_back("obama");     // Add strings to label to enable options
       label.push_back("romney");
       label.push_back("doesn't matter");
    
       size_t i, size = label.size();
       vector<int> result(size, 0);
    
       while (*max_element(result.begin(), result.end()) < 5)
       {
            cout<< "Who would you like to see become president?\n";
    
            for (i = 0; i < size; ++i) cout<< (i + 1) << ". " << label[i] << '\n';
    
            size_t answer;
            cin >> answer;
    
            if (answer <= 0 || answer > size)
                cout<< "Invalid input.  Please enter a value in the range 1-" << size << '\n';
            else
                 ++result[answer-1];
       }
    
       for (i = 0; i < size; ++i) cout<< label[i] << "- " << result[i] << '\n';
    }
    Now, okay, I've used a few things from the standard library you probably don't know about yet. But this could be adapted to not use the standard library (other than for output to cout). Instead of a vector of strings, for example, it is possible to use an array of char *.

    I haven't even gone out of my way to optimise this code. For example, I have not eliminated unnecessary variables, and have not bothered to break the code up into separate functions, which could make the code even simpler.

    I would also personally remove the "using namespace std;" directive (and use std::cout rather than cout) but that's a discussion for another day.
    Last edited by grumpy; 07-16-2012 at 03:49 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I would also personally remove the "using namespace std;" directive (and use std::cout rather than cout) but that's a discussion for another day.
    i am intrigued - without wanting to divert this thread would like to hear your thinking on that.
    Last edited by rogster001; 07-16-2012 at 04:42 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple check/deposit exercise in C
    By edishuman in forum C Programming
    Replies: 10
    Last Post: 09-15-2011, 10:49 AM
  2. Simple login/password check
    By ceevee in forum C++ Programming
    Replies: 26
    Last Post: 01-17-2005, 12:05 AM
  3. Simple check program
    By Shadow in forum C Programming
    Replies: 3
    Last Post: 06-05-2002, 06:20 PM