Thread: while(true) loop trouble

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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