Thread: help in c++ while

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    help in c++ while

    i have this program when i press 6 the program should exit why is this not working. when i press 6 i get garbage value is my logic right

    Code:
    #include <iostream> 
    
    using namespace std; 
    template <typename mytype>
    mytype fun (mytype a, mytype b, mytype c)
    {
          if(b>c)
          {
                 
            return(b);     
          }
          
          
    
    }
    
    int main () 
    { 
    
    int a,b,c,select;
    cout << "Types of gates available:"<<'\n';
    cout << "1. And"<<'\n';
    cout << "2. Or"<<'\n';
    cout << "3. And"<<'\n';
    cout << "4. Or"<<'\n';
    cout << "5. And"<<'\n';
    cout << "6. exit"<<'\n';
    
    cout << "Select gate #1"<<'\n'; 
    cin>>a;
    while(!a==6)
    {
    cout<<"Enter two inputs for gates =>"<<'\n';
    cin>>b;
    cin>>c;
    }
    
    
    
    cout<<fun(a,b,c);
    
    
              cin.sync();
              cin.get();
    
              return 0; 
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while(!a==6)

    Maybe
    a != 6

    or

    ! ( a == 6 )

    And would it kill you to indent you code?
    http://sourceforge.net/apps/mediawik...le=Indentation
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    it doesnt work. you just guessing

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's just rude. And Salem was absolutely right in that was an error in your code. It's just not the only one. For example, your while-loop, once entered, cannot possibly terminate. Also, your program flow is a mess and doesn't do what you want at all, because your loop spans the wrong instructions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    if you start a loop
    Code:
    while(a != 6) // salem was right about how to put that
    {
      cout<<"Enter two inputs for gates =>"<<'\n';
      cin>>b;
      cin>>c;
    }
    and never assign a new variable to (a) within the loop. Then you are never going to leave that loop.
    1. Initial requirement a doesn't equal 6
    2. cout stuff
    3. change b value
    4. change c value
    5. end of loop and a still doesn't equal 6 start loop again
    6. cout stuff
    7. change b value
    8. change c value
    9. a still doesn't equal 6
    ......... ect.
    See where thats goin? (somewhere there needs to be a ... change a value)
    You have to be careful with any while loop to set a condition that can actually allow you to exit the loop, or the computer gets stuck there indefinitely.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    On the other hand, if a is 6 the first time through, you never read values for b and c, which is why they're garbage.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    indeed

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by mouse666666 View Post
    it doesnt work. you just guessing
    So you think because he used the word "maybe" he's guessing?! You're wrong in that assumption. It looks like "maybe" was being used in a sarcastic tone. I say that because he is in fact completely right.
    Here's a table of operator precedence for you to look at.

    What you wrote was equivalent to (!a) == 6 and your logic is most definitely wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by mouse666666 View Post
    it doesnt work. you just guessing
    ah...what a very interesting response, now I can't wait to see what will happen
    Last edited by cph; 09-23-2010 at 02:20 AM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like I'm going to pay any attention to a coder who's approach to programming is "smash face into keyboard until knowledge is absorbed".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed