Home » Archimedes archive » Acorn User » AU 1995-04.adf » !C_C » c/dowhile

c/dowhile

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1995-04.adf » !C_C
Filename: c/dowhile
Read OK:
File size: 039F bytes
Load address: 0000
Exec address: 0000
File contents
/* Program to demonstrate the use of do-while */
/* Acorn User 'C for yourself' demonstration C program - April 1995 */

#include    <stdio.h>

main()
{
    int choice = 0;

    do {
        printf("1: Do this\n");
        printf("2: Do that\n");
        printf("3: Do the other\n\n");
        printf("Please enter a choice from the menu:\n");
        scanf("%d",&choice);
        while (getchar() != '\n') {
           /* Do nothing in the while loop! */
        }

        /* The above statement cleans up the information produced by
        scanf() by skipping through characters remaining in the input
        stream until a newline ('Return' character) is reached. */

        if (choice < 1 || choice > 3) {
            printf("Sorry - that option does not exist.\n");
            printf("Please pick another option.\n\n");
        }
    } while (choice < 1 || choice > 3);

    printf("You picked option %d\n",choice);
}
00000000  2f 2a 20 50 72 6f 67 72  61 6d 20 74 6f 20 64 65  |/* Program to de|
00000010  6d 6f 6e 73 74 72 61 74  65 20 74 68 65 20 75 73  |monstrate the us|
00000020  65 20 6f 66 20 64 6f 2d  77 68 69 6c 65 20 2a 2f  |e of do-while */|
00000030  0a 2f 2a 20 41 63 6f 72  6e 20 55 73 65 72 20 27  |./* Acorn User '|
00000040  43 20 66 6f 72 20 79 6f  75 72 73 65 6c 66 27 20  |C for yourself' |
00000050  64 65 6d 6f 6e 73 74 72  61 74 69 6f 6e 20 43 20  |demonstration C |
00000060  70 72 6f 67 72 61 6d 20  2d 20 41 70 72 69 6c 20  |program - April |
00000070  31 39 39 35 20 2a 2f 0a  0a 23 69 6e 63 6c 75 64  |1995 */..#includ|
00000080  65 20 20 20 20 3c 73 74  64 69 6f 2e 68 3e 0a 0a  |e    <stdio.h>..|
00000090  6d 61 69 6e 28 29 0a 7b  0a 20 20 20 20 69 6e 74  |main().{.    int|
000000a0  20 63 68 6f 69 63 65 20  3d 20 30 3b 0a 0a 20 20  | choice = 0;..  |
000000b0  20 20 64 6f 20 7b 0a 20  20 20 20 20 20 20 20 70  |  do {.        p|
000000c0  72 69 6e 74 66 28 22 31  3a 20 44 6f 20 74 68 69  |rintf("1: Do thi|
000000d0  73 5c 6e 22 29 3b 0a 20  20 20 20 20 20 20 20 70  |s\n");.        p|
000000e0  72 69 6e 74 66 28 22 32  3a 20 44 6f 20 74 68 61  |rintf("2: Do tha|
000000f0  74 5c 6e 22 29 3b 0a 20  20 20 20 20 20 20 20 70  |t\n");.        p|
00000100  72 69 6e 74 66 28 22 33  3a 20 44 6f 20 74 68 65  |rintf("3: Do the|
00000110  20 6f 74 68 65 72 5c 6e  5c 6e 22 29 3b 0a 20 20  | other\n\n");.  |
00000120  20 20 20 20 20 20 70 72  69 6e 74 66 28 22 50 6c  |      printf("Pl|
00000130  65 61 73 65 20 65 6e 74  65 72 20 61 20 63 68 6f  |ease enter a cho|
00000140  69 63 65 20 66 72 6f 6d  20 74 68 65 20 6d 65 6e  |ice from the men|
00000150  75 3a 5c 6e 22 29 3b 0a  20 20 20 20 20 20 20 20  |u:\n");.        |
00000160  73 63 61 6e 66 28 22 25  64 22 2c 26 63 68 6f 69  |scanf("%d",&choi|
00000170  63 65 29 3b 0a 20 20 20  20 20 20 20 20 77 68 69  |ce);.        whi|
00000180  6c 65 20 28 67 65 74 63  68 61 72 28 29 20 21 3d  |le (getchar() !=|
00000190  20 27 5c 6e 27 29 20 7b  0a 20 20 20 20 20 20 20  | '\n') {.       |
000001a0  20 20 20 20 2f 2a 20 44  6f 20 6e 6f 74 68 69 6e  |    /* Do nothin|
000001b0  67 20 69 6e 20 74 68 65  20 77 68 69 6c 65 20 6c  |g in the while l|
000001c0  6f 6f 70 21 20 2a 2f 0a  20 20 20 20 20 20 20 20  |oop! */.        |
000001d0  7d 0a 0a 20 20 20 20 20  20 20 20 2f 2a 20 54 68  |}..        /* Th|
000001e0  65 20 61 62 6f 76 65 20  73 74 61 74 65 6d 65 6e  |e above statemen|
000001f0  74 20 63 6c 65 61 6e 73  20 75 70 20 74 68 65 20  |t cleans up the |
00000200  69 6e 66 6f 72 6d 61 74  69 6f 6e 20 70 72 6f 64  |information prod|
00000210  75 63 65 64 20 62 79 0a  20 20 20 20 20 20 20 20  |uced by.        |
00000220  73 63 61 6e 66 28 29 20  62 79 20 73 6b 69 70 70  |scanf() by skipp|
00000230  69 6e 67 20 74 68 72 6f  75 67 68 20 63 68 61 72  |ing through char|
00000240  61 63 74 65 72 73 20 72  65 6d 61 69 6e 69 6e 67  |acters remaining|
00000250  20 69 6e 20 74 68 65 20  69 6e 70 75 74 0a 20 20  | in the input.  |
00000260  20 20 20 20 20 20 73 74  72 65 61 6d 20 75 6e 74  |      stream unt|
00000270  69 6c 20 61 20 6e 65 77  6c 69 6e 65 20 28 27 52  |il a newline ('R|
00000280  65 74 75 72 6e 27 20 63  68 61 72 61 63 74 65 72  |eturn' character|
00000290  29 20 69 73 20 72 65 61  63 68 65 64 2e 20 2a 2f  |) is reached. */|
000002a0  0a 0a 20 20 20 20 20 20  20 20 69 66 20 28 63 68  |..        if (ch|
000002b0  6f 69 63 65 20 3c 20 31  20 7c 7c 20 63 68 6f 69  |oice < 1 || choi|
000002c0  63 65 20 3e 20 33 29 20  7b 0a 20 20 20 20 20 20  |ce > 3) {.      |
000002d0  20 20 20 20 20 20 70 72  69 6e 74 66 28 22 53 6f  |      printf("So|
000002e0  72 72 79 20 2d 20 74 68  61 74 20 6f 70 74 69 6f  |rry - that optio|
000002f0  6e 20 64 6f 65 73 20 6e  6f 74 20 65 78 69 73 74  |n does not exist|
00000300  2e 5c 6e 22 29 3b 0a 20  20 20 20 20 20 20 20 20  |.\n");.         |
00000310  20 20 20 70 72 69 6e 74  66 28 22 50 6c 65 61 73  |   printf("Pleas|
00000320  65 20 70 69 63 6b 20 61  6e 6f 74 68 65 72 20 6f  |e pick another o|
00000330  70 74 69 6f 6e 2e 5c 6e  5c 6e 22 29 3b 0a 20 20  |ption.\n\n");.  |
00000340  20 20 20 20 20 20 7d 0a  20 20 20 20 7d 20 77 68  |      }.    } wh|
00000350  69 6c 65 20 28 63 68 6f  69 63 65 20 3c 20 31 20  |ile (choice < 1 |
00000360  7c 7c 20 63 68 6f 69 63  65 20 3e 20 33 29 3b 0a  ||| choice > 3);.|
00000370  0a 20 20 20 20 70 72 69  6e 74 66 28 22 59 6f 75  |.    printf("You|
00000380  20 70 69 63 6b 65 64 20  6f 70 74 69 6f 6e 20 25  | picked option %|
00000390  64 5c 6e 22 2c 63 68 6f  69 63 65 29 3b 0a 7d     |d\n",choice);.}|
0000039f