IHYPRESS.NET
C
HOME | ASP | C | CSS | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CLoops ❯ Input Validation Loop
<04.10>
/* Input validation loop (do-while) */
#include <stdio.h> int main (void) { char color; /* ask user for color */ do { printf ("Enter the color of the light (R,G,Y,A): "); scanf (" %c", &color); /* very important to leave space before %c here */ }while (color!='R' && color!='r' && color!='G' && color!='g' && color!='Y' && color!='y' && color!='A' && color!='a'); /* test the alternatives */ switch (color) { /* red light */ case 'R': case 'r': printf ("STOP! \n"); break; /* yellow or amber light */ case 'Y': case 'y': case 'A': case 'a': printf ("CAUTION! \n"); break; /* green light */ case 'G': case 'g': printf ("GO! \n"); break; /* no default case necessary here */ } return (0); }
Hergestellt in Deutschland / Made in Germany
Enter the color of the light (R,G,Y,A): b Enter the color of the light (R,G,Y,A): x Enter the color of the light (R,G,Y,A): G GO!
COPYRIGHT © 2015-2024 IHYPRESS.NET. A DIVISION OF IHY PRESS. ALL RIGHTS RESERVED.