CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    6

    While loop only runs once

    Hello everyone. I am stuck and have an issue that I'm hoping someone will be able to help me with. I'm writing a simple integer calculator that supports addition, subtraction, multiplication, modulus, and division. I have to implement this by defining a class. The program is suppose to 1. request and accept a number, 2. apply the stored operation (of which the first is addition and after it should be whatever the user enters), 3. request and accept the next operation. When the operation entered is '=', the result should be printed and the accumulator reset, when '!' is entered as the operator, the same should happen except the program should now terminate.

    Here is a sample run of what I'm trying to accomplish


    Welcome to the calculator
    ------
    Input: 10
    Operation: *
    Input: 2
    Operation +
    Input: -5
    Operation: =
    Result: 15
    ------
    Input: 12
    Operation: /
    Input: 0
    **Division by zero - start over
    ------
    Input: 12
    Operation: >
    **Invalid operation - ignored
    Operation: /
    Input: 5
    Operation: =
    Result: 2
    ------
    Input: 10
    Operation: %
    Input: 6
    Operation: *
    Input: 3
    Operation: !
    Result: 12
    ------
    Calculator Off!


    The issue I am having is when I run the program, the while loop is not continuing as it should and exits right away without op being '='. I have attached the files with the code I have so far because I didn't want to make this post any longer than it already is.

    Any help would be greatly appreciated. Also feel free to make any comments on anything else I could improve on.
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: While loop only runs once

    Look at your main() function:
    Code:
    while (!(op = '!')); //while op is not '!' keep going
    "=" in "op = '!'" is an asignment operator, NOT compare. Did you mean op == '!' ?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: While loop only runs once

    Quote Originally Posted by slyslick View Post
    The issue I am having is when I run the program, the while loop is not continuing as it should and exits right away without op being '='.
    Please, show us this loop.

    Quote Originally Posted by slyslick View Post
    I have attached the files with the code I have so far because I didn't want to make this post any longer than it already is.
    Use Code tags around the code snippets and your post will never be too long.
    Victor Nijegorodov

  4. #4
    Join Date
    Mar 2009
    Posts
    6

    Re: While loop only runs once

    I have made the change suggested by VladimirF and also the if statement 4 lines below and now the loop will not execute at all, the program freezes with a flashing cursor.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: While loop only runs once

    Use the debugger to figure out where it's stuck and why.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: While loop only runs once

    Quote Originally Posted by VladimirF View Post
    Look at your main() function:
    Code:
    while (!(op = '!')); //while op is not '!' keep going
    "=" in "op = '!'" is an asignment operator, NOT compare. Did you mean op == '!' ?
    He's right, the value of op = '!' is '!'. An assignment always returns the assigned value. to compare op to '!' us while (!(op == '!')); Or, even better while(op != '!');

  7. #7
    Join Date
    Mar 2009
    Posts
    6

    Re: While loop only runs once

    i had a ; after the while statement. it gets further nowbut still sticks somewhere. not sure y. i will see if i can resolve this one on my own before i ask for help but thank you guys for your help so far, cant beleive i missed something so simple

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured