Thread: How to wait until a condition is met before proceeding

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    1

    How to wait until a condition is met before proceeding

    I want to waituntil both a and b and thus c are a number before being able to exit.

    Code:
    #include<stdio.h>
    #include <conio.h> 
    
    int main() {
          int a, b, c;
          printf("Please enter any two numbers: \n");
          scanf_s("%d %d", &a, &b);
              c = a + b; 
            //waitUntil {
                    if (a = numeric && b = numeric) then {;
                        printf_s("The sum of two numbers is: %d \n",c);
                        printf_s("Press any key to exit");
                        _getch();
                    } else {
                         printf_s("You need to enter a numerical value");
                         waituntil {
                                a = numeric && b = numeric
                    }; 
                         printf_s("The sum of two numbers is: %d \n",c);
              };
        };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I'd suggest is something like this:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int a, b;
        char line[BUFSIZ];
    
        printf("Please enter any two numbers:\n> ");
        while (fgets(line, sizeof(line), stdin)) {
            if (sscanf(line, "%d %d", &a, &b) == 2) {
                printf("The sum of two numbers is: %d\n", a + b);
                break;
            }
    
            printf("You need to enter two integer values\n> ");
        }
    
        return 0;
    }
    The idea is that you read a line of input from the user, so you don't have to worry about discarding invalid input. (Well, technically you still have to handle a case where the user enters more input than can fit in the array of char, but you can do that as a more advanced exercise.)

    Then, to determine if the input is valid, you parse the line read with sscanf and check the return value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2013, 04:44 PM
  2. Replies: 7
    Last Post: 01-03-2007, 08:58 PM
  3. how do u say like H (wait 1 second) I
    By Prodigy in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2002, 05:05 AM
  4. Wow, I can't wait to see what this will be like.
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-13-2002, 12:44 PM

Tags for this Thread