Reading and guessing a number using the 'start' and 'goto' C keyword
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Reading and guessing a number using the 'start' and 'goto' C keyword
To show: How to use the 'goto' and 'start' C keyword in guessing a number input by user
// Demonstrates the 'goto' usage
#include <stdio.h>
void main(void)
{
int n;
start: ;
puts("Enter a number between 0 and 10: ");
puts("(Try 0, 1, > 10 and between 0 and 10)");
/* scanf("%d", &n); */
scanf_s("%d", &n, 1);
if ((n < 0) || (n > 10))
goto start;
else if (n == 0)
goto location0;
else if (n == 1)
goto location1;
else
goto location2;
location0: ;
{
puts("You entered 0.");
}
goto end;
location1: ;
{
puts("You entered 1.");
}
goto end;
location2: ;
{
puts("You entered something between 2 and 10.");
}
end: ;
}
Output example:
Enter a number between 0 and 10:
(Try 0, 1, > 10 and between 0 and 10)
11
Enter a number between 0 and 10:
(Try 0, 1, > 10 and between 0 and 10)
-3
Enter a number between 0 and 10:
(Try 0, 1, > 10 and between 0 and 10)
7
You entered something between 2 and 10.
Press any key to continue . . .
Enter a number between 0 and 10:
(Try 0, 1, > 10 and between 0 and 10)
1
You entered 1.
Press any key to continue . . .