Thread: How to make the function being able to to continue a while loop while being outside

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    5

    Question How to make the function being able to to continue a while loop while being outside

    I already learned that continue or break works only while being inside the loop.
    My question is that is there any way to outsmart this or is there any way besides continue to make it work...

    In the code I'm talking about the function q_what_to_do_next, how to make this outside function do the continue work?
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <limits.h>
    #include <string.h>
    #include <math.h>
    
    
    int q_what_to_do_next();
    
    
    int main()
    {
        int a = INT_MAX;
        while (1)
        {
            if (a != INT_MAX)
            {
                //nuline programa, jei nori baigti programa
                if (a == 0)
                {
                    system("cls");
                    char p;
                    printf("Ar tikrai norite iseiti is programos?\n");
                    printf("Jei taip, spauskite y\n");
                    printf("Jei ne, spauskite n\n");
                    scanf(" %c", &p);
                    if(p != 'y' && p!='n')
                    {
                        system("cls");
                        int i;
                        printf("Kadangi pasirinkote ne y arba n busite automatiskai ismestas is programos uz 10 sekundziu\n");
                        Sleep(5000);
                        for (i = 1; i <= 10; i++)
                        {
                            printf("%d\n", i);
                            Sleep(1000);
                        }
                        break;
                    }
                    if (p == 'y')
                        break;
                    else if(p =='n')
                    {
                        a = INT_MAX;
                        system("cls");
                        continue;
                    }
    
    
                }
                //pirmoji programa
                else if (a == 1)
                {
                    system("cls");
                    int binary;
                    int bp = 0; //bp - isaugojimas pradiniu duomenu
                    int decimal = 0;
                    int k = 0; //pagalbinis skaiciuojant desimtaini
                    int m = 1; //kitas pagalbinis, kuris dides
                    printf("***********************************************************************************\n");
                    printf("**  Iveskite savo norima dvejetaini koda, kuri noretumete paversti desimtainiu:  **\n");
                    printf("***********************************************************************************\n");
    
    
                    while (scanf(" %9d", &binary) != 1 || getchar() != '\n')
                    {
                        printf("Iveskite dvejetaini skaiciu!\n");
                        while (getchar() != '\n')
                            ;
                    }
    
    
                    bp = binary;
    
    
                    while (bp > 0)
                    {
                        k = bp % 10;
                        decimal = decimal + k * m;
                        bp = bp / 10;
                        m = m * 2;
                    }
                    system("cls");
                    //pagrindas 40 H raidziu
                    double sk = 0, sk1 = 0;
                    sk = (decimal == 0) ? 1 : log10(decimal) + 1;
                    sk1 = (bp == 0) ? 1 : log10(bp) + 1;
                    double n = sk + 5;
                    printf("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
                    while (n > 0)
                    {
                        printf("H");
                        n--;
                    }
                    printf("\n");
    
    
                    printf("HH  Jusu pasirinktas dvejetainis kodas: %d", binary);
    
    
    
    
                    n = sk + 5;
                    while (n > 0)
                    {
                        printf(" ");
                        n--;
                    }
                    printf("HH\n");
    
    
                    n = sk + 5;
                    printf("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
                    while (n > 0)
                    {
                        printf("H");
                        n--;
                    }
                    printf("\n");
    
    
    
    
                    printf("HH  Gautas desimtainis kodas, konvertutoas is dvejetainio: %d", decimal);
                    printf("  HH\n");
    
    
    
    
                    n = sk + 5;
                    printf("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
                    while (n > 0)
                    {
                        printf("H");
                        n--;
                    }
                    printf("\n");
    
    
                    a = q_what_to_do_next();
                }
                else
                    {
                    system("cls");
                    int b = 0;
                    printf("Nera tokios programos\n");
                    printf("Paspauskite 1, kad galetumete is naujo pasirinkti\n");
                    while (scanf(" %d", &b) != 1 || getchar() != '\n' && b!=1)
                    {
                        printf("NU siupele tu siupele!\n");
                        printf("VIENETAAA, ne kazka kito\n");
                        while (getchar() != '\n')
                        ;
                    }
                    if (b == 1)
                    {
                        system("cls");
                        a = INT_MAX;
                        continue;
                    }
                }
            }
            else
            {
                printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n");
                printf("|         Programos veiksmu pasirinkimas:         |\n");
                printf("|irasykite skaiciu, kurios programos dalies norite|\n");
                printf("///////////////////////////////////////////////////\n");
                printf("|1 - is dvejetaines i desimtaine                  |\n");
                printf("|2 - is dvejetaines i sesioliktaine               |\n");
                printf("|3 - is desimtaines i dvejetaine                  |\n");
                printf("|4 - is desimtaines i sesioliktaine               |\n");
                printf("|5 - is sesioliktaines i dvejetaine               |\n");
                printf("|6 - is desioliktaines i desimtaine               |\n");
                printf("|0 - iseiti is programos                          |\n");
                printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n");
                while (scanf(" %d", &a) != 1 || getchar() != '\n')
                {
                    printf("LOPETA TU!\n");
                    printf("TIK VIENA SKAICIU, BE JOKIO TARPO, BE RAIDZIU!\n");
                    printf("Nebuk kaip Grazulis ble...\n");
                    while (getchar() != '\n')
                        ;
                }
                continue;
            }
            
            system("cls"); //clear screen
        }
        exit(0);
        return 0;
    }
    
    
    int q_what_to_do_next()
    {
        int p;
        printf("Jie norite pakartoti programa spauskite 1\n");
        printf("Jei norite grizti i programu pasirinkimo langa spauskite 2\n");
        printf("Jei norite baigti programa spauskite 3\n");
        while (scanf(" %d", &p) != 1 || getchar() != '\n')
        {
            printf("Iveskite skaiciu is pasirinkimu, ne kazka kito\n");
            while (getchar() != '\n')
                ;
        }
        if (p == 1)
        {
            //a = 1;
            return 1;
            continue;
        }
        if (p == 2)
        {
            system("cls");
            //a = INT_MAX;
            return INT_MAX;
            continue;
        }
        else
        {
            system("cls");
            //a = 0;
            return 0;
            continue;
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,440
    The only way to run more than one thing at once is to use a thread.

    Here's a trivial example.
    Code:
    #include <stdio.h>
    #include <threads.h>
    #include <stdbool.h>
    #include <unistd.h>
    
    int watch(void *p) {
        int *ptr = p;
        while ( true ) {
            printf("Enter number > ");
            fflush(stdout);
            scanf("%d",ptr);
        }
    }
    
    int main()
    {
        int answer = 0;
        thrd_t t;
        thrd_create(&t, watch, &answer);
        while ( true ) {
            printf("Answer = %d\n", answer);
            sleep(1);
        }
        return 0;
    }
    
    
    $ gcc -std=c11 bar.c -pthread
    $ ./a.out 
    Answer = 0
    Enter number > Answer = 0
    Answer = 0
    Answer = 0
    Answer = 0
    42Answer = 0
    
    Enter number > Answer = 42
    Answer = 42
    Answer = 42
    Answer = 42
    Answer = 42
    Answer = 42
    ^C
    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
    Banned
    Join Date
    Jul 2022
    Posts
    112
    while (1) is an infinite loop, breaking out of it is considered as side-effect.

  4. #4
    Registered User
    Join Date
    Oct 2022
    Posts
    5
    Thank you

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    399
    I think I get what you are asking...

    "continue" can not reach outside of a function. To do what you want, the function has to signal back to the caller that it should continue:

    Code:
    int my_founction(int x, int y, int z) {
         if( x == y)
             return 0;   // Failure
         return 1;  // Success
    }
    
    ...
    
    void main_loop(void) {
        while(1) {
            // Set x,y,z to values
            if(my_function(x,y,z)) {
                continue;
            }
        }
    }
    The only other way is to use things like "setjump()" and "longjump()" -but that is not the solution you should be using.

  6. #6
    Registered User
    Join Date
    Oct 2022
    Posts
    5
    Thanks, thats what I needed, you are right on the spot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2017, 08:05 PM
  2. Help with the y/n continue loop
    By abujuguluy in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2015, 01:37 AM
  3. do while loop to continue program
    By zfite in forum C Programming
    Replies: 5
    Last Post: 02-26-2011, 12:15 AM
  4. continue in for loop
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 04-09-2009, 03:51 PM
  5. continue loop
    By romeoz in forum C++ Programming
    Replies: 7
    Last Post: 09-25-2003, 05:25 PM

Tags for this Thread