Thread: while(true) loop trouble

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    6

    while(true) loop trouble

    I'm trying to create a simple calculator that keeps prompting the user for input until the user enters the standard input ("number" "operator") and the operator = 'E'.

    I know in java I'd just use a while(true) loop (as I did in here), but I get the error "true undeclared." I'm new to C and am not sure what I'm doing wrong .. thanks for any help!

    Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main() {
    
      double num; double accum;
      char op;
    
      printf("Initialize your Accumulator with data of the form 'number' 'S' which sets the Accumulator \
    to the value of your number\n");
      scanf("%f %c", &num, &op);
    
      while (1) {
    
        if (op == 'S') {
        accum = num;
    }
    
        else if (op == '+') {
          accum = accum+num;
        }
    
        else if (op == '*') {
          accum = accum*num;
        }
    
        else if (op == '/') {
          accum = accum/num;
        }
    
        else if (op == '-') {
          accum = accum-num;
    }
    
        else if ((num == 0) && (op == '/')) {
          printf("Can not divide by 0.\n");
    }
    
    
        else if (op == '-') {
          accum = accum-num;
    }
    
        else if ((num == 0) && (op == '/')) {
          printf("Can not divide by 0.\n");
    }
    
        else if (op == 'E') {
          printf("Value in the Accumulator = %d \n", accum);
          printf("End of Calculations.\n");
          exit(EXIT_SUCCESS); /*exits program if user enters E*/
        }
    
        else {
          printf("Unknown operator.\n");
        }
    
        printf("Value in the Accumulator = %f \n", accum);
        scanf("%f %c", &num, &op);
    
      }
    Last edited by Michele Degges; 09-24-2011 at 03:50 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try it as ... while (1)

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    Try it as ... while (1)
    uhh no, that doesn't work.

    while(true) loop trouble-z-png

    Edit: to clarify, I just need the user to keep being prompted to enter a new value. The output should look like this:

    Initialize your Accumulator with data of the form 'number' 'S' which sets the Accumulator to the value of your number
    123123 S /*user enters this*/
    Value in the Accumulator = 123123.000000
    234 + /*user enters this*/
    Value in the Accumulator = 123357.000000
    123.5234523432 * /*user enters this*/ ..etc
    Last edited by Michele Degges; 09-24-2011 at 01:33 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Michele Degges View Post
    uhh no, that doesn't work.
    Of course it worked... your loop is running along just fine, exactly as you programmed it...

    Edit: to clarify, I just need the user to keep being prompted to enter a new value. The output should look like this:
    Then you need to move the code asking for the new value inside the loop...

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    Ok, I see what you mean. I edited my code to scan user input after it prints "Value in the accum = n" .. so now I am being prompted for more input like I wanted..
    but for some reason it always prints "Value in the accum = 2" , for any number I input.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In one of your scanf's you are correctly using %d and in the rest you are incorrectly usinf %f. That's probably the main issue.
    Note that you are using different data types for accum and num where it would probably make sense for them to be the same.
    Note that the same line, plus the printf appear in about 7 places in that code. The best thing to do there would be to remove the duplication. There should only be one scanf line in the whole program. Once you sort out where that one line should go and remove the duplication, you'll have a simpler program and you'll be much happier with the clearer code.
    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"

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    Quote Originally Posted by iMalc View Post
    In one of your scanf's you are correctly using %d and in the rest you are incorrectly usinf %f. That's probably the main issue.
    Note that you are using different data types for accum and num where it would probably make sense for them to be the same.
    Note that the same line, plus the printf appear in about 7 places in that code. The best thing to do there would be to remove the duplication. There should only be one scanf line in the whole program. Once you sort out where that one line should go and remove the duplication, you'll have a simpler program and you'll be much happier with the clearer code.
    Thanks for your response. I wanted the calculations to be as accurate as possible so I changed the input from an int to a double. I also tried to clean up my code a bit but I'm still not getting the right numbers. I have two scanf's in the updated code, one that takes the original value and then one that keeps prompting the user in the loop.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Michele Degges View Post
    Ok, I see what you mean. I edited my code to scan user input after it prints "Value in the accum = n" .. so now I am being prompted for more input like I wanted..
    but for some reason it always prints "Value in the accum = 2" , for any number I input.
    Post your new code, lets see what's going on...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Michele Degges View Post
    Thanks for your response. I wanted the calculations to be as accurate as possible so I changed the input from an int to a double. I also tried to clean up my code a bit but I'm still not getting the right numbers. I have two scanf's in the updated code, one that takes the original value and then one that keeps prompting the user in the loop.
    And only one of those should be inside the loop...

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    And only one of those should be inside the loop...
    There's only one inside the loop.
    I updated my code in the first post-

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Michele Degges View Post
    I updated my code in the first post-
    PLEASE don't do that... you just made the first half of this conversation in to a bunch of meaningless blather. People are going to look at your code, read the thread and wonder what the $%$% we're talking about!

    Next time... post your updated code in a new message at the END of the thread.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Very good. You're doing well. Sometimes you do want the thing before the loop as well, but in thise case you can get away with just one. Notice that the last thing in the loop is the same a the thing done just before it. As the user running the program, they can't tell the difference between something executed at the end of the loop and something executed at the start of the loop (provided it always loops of course). So by moving it to the start of the loop you can remove the other scanf since you'd then have two in a row first time through.

    As for the real problem I'm quite puzzled because the code shown doesn't look like it can cause that.
    Can you give an example of what you typed in?
    Also, perhaps it isn't recompiling the program and instead just running the code from some time ago. You could try changing the text and see if it is different when you run it again, just to be sure.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why do i always get true in the loop?
    By Masterx in forum C++ Programming
    Replies: 21
    Last Post: 09-13-2008, 01:20 PM
  2. While loop trouble!!
    By Astra in forum C Programming
    Replies: 6
    Last Post: 10-21-2006, 11:11 AM
  3. best way to brake a while(TRUE) loop?
    By g1i7ch in forum C Programming
    Replies: 6
    Last Post: 07-10-2006, 02:58 AM
  4. while loop trouble
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 09-19-2005, 02:59 AM
  5. ***trouble with a loop***
    By JohnMayer in forum C Programming
    Replies: 9
    Last Post: 08-05-2002, 07:13 AM

Tags for this Thread