Thread: Getting the user to break a look

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Getting the user to break a look

    Hi
    I'm a bit of a newbie but I have checked out the BREAK and the various types of LOOPs but cannot seem to get what I want.

    I want the user to press a key to start the loop and another one to end it.

    Inside the loop I want the program to count to ten seconds then beep (\a). I want it to keep on doing this until the user presses a key.

    Here's what I've done so far - it's a bit messy and clearly doesn't work but here goes:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char yn;
    int x, y;
    int main()
    {
        printf("Ready to go?  Y/N\n");
        scanf(yn)
     
        // loop 500 times
        for (int y = 0; y < 10; y++)
        {
            printf("\a")
            getchar(a); // read a char from user
     
            // exit loop if user hits enter
            if (a == '\n')
                break;
        }
    return 0;
    }

    Any help gratefully received - thanks

    Dave

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    getchar doesn't work that way. Read about it: getchar

    Your compiler should be yelling at you.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Loops

    Yes the compiler is of course yelling at me but I am having trouble understanding the concept of how the user can break the for loop.


  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This:
    Code:
        printf("Ready to go?  Y/N\n");
        scanf(yn)
    is a compile error, but if you meant:
    Code:
        scanf("%c", &yn);
    There will be a '\n' left in the stdin buffer afterward, which may have consequences in the first iteration of your loop.

    This:
    Code:
        for (int y = 0; y < 10; y++)
    would only be valid under c99. It's also problematic because you have redeclared an identifier (y).

    This:

    Code:
            getchar(a); // read a char from user
      
            // exit loop if user hits enter
            if (a == '\n')
                break;
    Is not a legal getchar() call, and 'a' is not declared anywhere. In short your code is not compilable for a number of reasons, which would explain why you "cannot seem to get what [you] want". You need to start with some working code, eg:

    Code:
    #include <stdio.h>
    
    int main() {
    	printf("Ready to go?  Y/N\n");
    	return 0;
    }
    And make sure it still compiles and runs when you add something new. Bonus point if it actually does what you want/expect.

    Quote Originally Posted by dwilliams View Post
    Yes the compiler is of course yelling at me
    That's like "go to jail" in monopoly. Do not pass GO. Do not collect $200.

    but I am having trouble understanding the concept of how the user can break the for loop.

    It will not be possible for you to come to an understanding if you can't write compilable code. First, it has to run. Then you can try and make it run the way you want. Not the other way around. Otherwise you might as well just use:

    Code:
    break here if the user wants to;
    Makes sense...but the compiler will yell Do not pass GO, do not collect...

    Code:
    Inside the loop I want the program to count to ten seconds then beep (\a). I want it to keep on doing this until the user presses a key.
    I don't know how many consoles will actually beep, don't be surprised if it doesn't -- that's kind of an atavism. WRT to timing, you can do this with sleep(), but it is a non-standard function and its definition differs from implementation to implementation (eg, on linux the parameter is in seconds, on windows in milliseconds).
    Last edited by MK27; 02-25-2012 at 12:43 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    would only be valid under c99. It's also problematic because you have redeclared an identifier (y).
    That part is okay due to scope, but the global variable is redundant to begin with.
    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

  6. #6
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63
    Edit:

    I see that MK27 has already responded, so ignore this post. His contains much more relevant information.

    Quote Originally Posted by dwilliams View Post
    Yes the compiler is of course yelling at me but I am having trouble understanding the concept of how the user can break the for loop.

    I don't have the answer, and haven't tried learning C programming in ages, but I noticed a few basic errors in your code:

    • Missing semicolon after scanf(yn)
    • Invalid use of getchar (as quzah mentioned)
    • You're not checking the results of the input from the user whether they wish to start the loop
    • Not an error per se, but GCC complained that I needed to use the --std=c99 option for the loop initial declaration
    • You'll also want to handle converting the user's response to upper or lower case and compare appropriately to make sure they gave a valid response


    I haven't looked up the syntax for scanf to fully understand it, but after looking up the info for getchar, I have the following code which still doesn't fully function, but may be closer to what you're looking for. At the very least I hope it illustrates the points I mentioned:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    char yn;
    
    int x, y;
    int main()
    {
        printf("Ready to go?  Y/N");
        yn = getchar();
        
        if ('Y' == toupper(yn))
        {
            
            printf("Press Enter or Return to Quit");
     
            // loop 500 times
            for (int y = 0; y < 10; y++)
            {
                printf("\a");
                yn = getchar(); // Toss first input as it was likely the
                                // Enter/Return key for getchar() input
                yn = getchar(); // read a char from user     
                  
                // exit loop if user hits enter
                if (yn == '\n')
                    break;
            }
        }
        
        return 0;
    }
    Here is a diff in case that makes it any easier to work with:

    Code:
    --- original-loop.c	2012-02-25 12:38:26 -0600
    +++ loop.c	2012-02-25 12:36:41 -0600
    @@ -1,22 +1,33 @@
     #include <stdio.h>
     #include <stdlib.h>
    - 
    +#include <ctype.h>
    +
     char yn;
    +
     int x, y;
     int main()
     {
    -    printf("Ready to go?  Y/N\n");
    -    scanf(yn)
    -  
    -    // loop 500 times
    -    for (int y = 0; y < 10; y++)
    +    printf("Ready to go?  Y/N");
    +    yn = getchar();
    +    
    +    if ('Y' == toupper(yn))
         {
    -        printf("\a")
    -        getchar(a); // read a char from user
    -  
    -        // exit loop if user hits enter
    -        if (a == '\n')
    -            break;
    +        
    +        printf("Press Enter or Return to Quit");
    + 
    +        // loop 500 times
    +        for (int y = 0; y < 10; y++)
    +        {
    +            printf("\a");
    +            yn = getchar(); // Toss first input as it was likely the
    +                            // Enter/Return key for getchar() input
    +            yn = getchar(); // read a char from user     
    +              
    +            // exit loop if user hits enter
    +            if (yn == '\n')
    +                break;
    +        }
         }
    -return 0;
    +    
    +    return 0;
     }
    Again, this is still not functional, but hopefully it'll be useful in some way.
    Last edited by deoren; 02-25-2012 at 12:51 PM. Reason: Saw that my post isn't nearly as helpful as MK27's and wanted to flag it as such
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    loops

    Thank you all so much for your help. Deoren - yours was particularly helpful. I believe I can move on with my little program now.
    I hope to improve my coding quite quickly.

    Thanks again

    David

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there something like break(2) (multiple break) ?
    By Phuzzillogic in forum C++ Programming
    Replies: 11
    Last Post: 05-11-2009, 04:05 AM
  2. illegal break?!?!?
    By GiantFurby in forum C Programming
    Replies: 2
    Last Post: 10-13-2008, 11:53 AM
  3. disable user break
    By Hurley in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2002, 09:04 AM
  4. Take a break....
    By NANO in forum C++ Programming
    Replies: 9
    Last Post: 06-25-2002, 11:17 AM
  5. break == bad
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2002, 01:08 AM