hi im still finishing a calculator program. the problem is i am wondering what command to use to break out of multiple loops. my code is

#include <stdio.h>
#include <ctype.h>
#include <math.h>

double do0p(char op, float acc, float num) {
    switch (op) {
        case '+' : return acc+num;
            break;
        case '-' : return acc-num;
            break;
        case '*' : return acc*num;
            break;
        case '/' : return acc/num;
            break;    
        case '^' : return pow(acc,num);
            break;
        default: return acc;
    }
}


int main(int argc, char *argv[]) {
    printf("ECE Assignment 3\n");
    printf("Richard Fang\n");
    
    float acc, num;
    char op;

    printf("Welcome to calculator!\n");
    while(1) {
        printf("enter first number: ");
        scanf("%f", &acc);
        printf("\n");
        printf("number entered:");
        printf("%f\n", acc);
        printf("\n");
        
        printf("enter operand:");
            while(1) {
                     
                do op = getchar(); while(isspace(op));
                    if(op == 'q') return 0;
                    else 
                                    
                    if(op == 'c') {printf("Calculator Cleared\n");
                       break;}
                    else if(op == 'i') {
                          printf("entering integer mode\n");
                          //inside integer mode
                          printf("enter first number: ");
                          scanf("%f", &acc);
                          printf("\n");
                          printf("number entered:");
                          printf("%f\n", acc);
                          printf("\n");
                          printf("enter operand:");
                                         while(1) {
                     
                                             do op = getchar();              while(isspace(op));
                                               if(op == 'q') return 0;
                                               else                                
                                            if(op == 'c') {printf("Calculator Cleared\n");
                                               break;}
                                             else 
                                            if (op == 'f') {printf("Entering Floating mode");
                                            
                                               // start of integer mode 2nd half
                                                    printf("enter second number:");
                                                      scanf("%f", &num);
                                                    acc = do0p(op, acc, num);
                                                    printf("%f\n", acc);
                                                   printf("press q to quit or c to start over: ");
                                                      }}
                                                      //end integer mode
                                
                printf("enter second number:");
                scanf("%f", &num);
            acc = do0p(op, acc, num);
            printf("%f\n", acc);
            printf("press q to quit or c to start over: ");
        }
    }






    return 0;

}

im trying to break out of the inner most look which is in red
any advice?

also i was wondering how to make it so that the program only takes integers and disregards any decimal values

Recommended Answers

All 5 Replies

Why are you starting a new thread to ask the same question? The old thread was fine. And using the PREVIEW button would show you that coloring code doesn't work

Breaking out of all loops from an inner loop is simply placing if statements at the end of the loops:

while (a < 20)
{
//  some statements
    while (b < 10)
    {
    //  first part of loop  
    if (b == 99) break
    //  second part of loop  
    }
    if (b == 99) break
//  even more statements
}

also i was wondering how to make it so that the program only takes integers and disregards any decimal values

Didn't my hint help in your other thread?

Also your formatting is dreadful. It's very hard to follow the program because of the placement of the braces and indentation. Try formatting more like:

printf("number entered:");
    printf("%f\n", acc);
    printf("\n");
    printf("enter operand:");
    while(1) 
    {
        do op = getchar();     // what is this statement supposed to do?
        while(isspace(op));    // what about this one?
        if(op == 'q') return 0;
        else                                
        if(op == 'c') 
        {
            printf("Calculator Cleared\n");
            break;
        }
        else 
        if (op == 'f') 
        {
            printf("Entering Floating mode");
                                            
            // start of integer mode 2nd half
            printf("enter second number:");
            scanf("%f", &num);
            acc = do0p(op, acc, num);
            printf("%f\n", acc);
            printf("press q to quit or c to start over: ");
        }
    }
    //end integer mode

Didn't really look at your code, nor do I intend to, but I'll propose a few methods for breaking out of nested loops:
- LIke WaltP suggested, use a variable as a break condition. Check it in your loop condition statement or in an if statement at the end of the loop.
- use a goto. This can be dangerous if not done properly. But it can also be the easiest way to get the job done. If you can't think of why it could be bad, it's likely you shouldn't use it. If you know what the problems with using a goto are, but you've decided your specific code segment is safe, go for it.
- similar to above, but use an exception instead. There are some similar consequences if you're not neat about it (e.g. if you have some dynamic allocation inside the loop, it won't be deleted unless you do that when you handle the exception). Still a little more safe than a goto though.

The last two will probably receive some criticism, but they are possible ways of solving the problem.

goto is widely criticized from where it was rightfully criticized -- BASIC. C and C++ are not BASIC. The same rules don't really apply.

It can be abused in C and C++, but this is not 1979, and the languages are different. So avoiding it for historical reasons in an unrelated language really loses more weight as time passes.

Most often (97% of the time, given that 86.7% of statistics are made up on the spot), the avoidance of goto is justified by a better code construct. But there is that remaining percentage that don't obtain any benefit -- avoiding the goto adds confusion, extra unnecessary memory usage, or what-not (the very things it is supposed to be the sole pariah of).

Breaking a loop from a switch, breaking nested loops, sharing common exit code, or jumping into the middle of a loop are a few reasons to use it -- which is why it is probably still available to the language.

im trying to break out of the inner most look which is in red
any advice?

Turn the inner loop into a function and just break or return from it normally. With a suitable return value, you can then break from the outer loop without using goto or state flags. That's my favorite method because it ends up refactoring the code into something better anyway, but you can do it however you want.

/* goto */
while ( condition ) {
  while ( condition ) {
    if ( condition )
      goto done;
  }
}
done:


/* State flags */
int done = 0;

while ( condition && !done ) {
  while ( condition ) {
    if ( condition ) {
      done = 1;
      break;
    }
  }
}


/* Function */
int myFunction( void )
{
  while ( condition ) {
    if ( condition )
      return 1;
  }

  return 0;
}

while ( condition ) {
  if ( myFunction() )
    break;
}

also i was wondering how to make it so that the program only takes integers and disregards any decimal values

I got this in another thread. :)

Turn the inner loop into a function and just break or return from it normally. With a suitable return value, you can then break from the outer loop without using goto or state flags.

Brilliant! Why didn't I (and the rest) think of that? Good catch, Ravalon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.