Skip to main content
added 7 characters in body
Source Link
jdt
  • 2.4k
  • 5
  • 20

Portability

The following functions are not portable: sleep() and system("cls"). I had to add this to get it to compile with MSVC

#ifdef _WIN32
#include <Windows.h>
#define sleep(x) Sleep(x * 1000)
#endif

system("cls"); will clear screen on Windows but fail on all other operating systems.


Uninitialized variables

Consider the following line:

unsigned short PNT = 0, Counter;

Why did you give an initial value for PT but not for Counter?


Input validation
See what happens if you enter a non-numeric value. The program goes into a crazy loop. Consider creating a function to validate your input instead of using `scanf`:
int readNumber(int minVal, int maxVal)
{
retry_input:
    char buf[BUFLEN];
    if (fgets(buf, BUFLEN, stdin) == 0)
        return -1;
    int value = strtol(buf, 0, 10);
    if (value < minVal || value > maxVal)
    {
        printf("Please enter a number between %d and %d: ", minVal, maxVal);
        goto retry_input;
    }
    return value;
}

And changing main to the following:

printf("\nEnter an X value: ");
INPUT1 = readNumber(1, 4);
if (INPUT1 == -1)
    return EXIT_FAILURE;

printf("\nEnter a Y value: "); 
INPUT2 = readNumber(1, 4);
if (INPUT2 == -1)
    return EXIT_FAILURE;

if (INPUT1 == RND1 && INPUT2 == RND2)
{
    ...

Portability

The following functions are not portable: sleep() and system("cls"). I had to add this to get it to compile with MSVC

#ifdef _WIN32
#include <Windows.h>
#define sleep(x) Sleep(x)
#endif

system("cls"); will clear screen on Windows but fail on all other operating systems.


Uninitialized variables

Consider the following line:

unsigned short PNT = 0, Counter;

Why did you give an initial value for PT but not for Counter?


Input validation
See what happens if you enter a non-numeric value. The program goes into a crazy loop. Consider creating a function to validate your input instead of using `scanf`:
int readNumber(int minVal, int maxVal)
{
retry_input:
    char buf[BUFLEN];
    if (fgets(buf, BUFLEN, stdin) == 0)
        return -1;
    int value = strtol(buf, 0, 10);
    if (value < minVal || value > maxVal)
    {
        printf("Please enter a number between %d and %d: ", minVal, maxVal);
        goto retry_input;
    }
    return value;
}

And changing main to the following:

printf("\nEnter an X value: ");
INPUT1 = readNumber(1, 4);
if (INPUT1 == -1)
    return EXIT_FAILURE;

printf("\nEnter a Y value: "); 
INPUT2 = readNumber(1, 4);
if (INPUT2 == -1)
    return EXIT_FAILURE;

if (INPUT1 == RND1 && INPUT2 == RND2)
{
    ...

Portability

The following functions are not portable: sleep() and system("cls"). I had to add this to get it to compile with MSVC

#ifdef _WIN32
#include <Windows.h>
#define sleep(x) Sleep(x * 1000)
#endif

system("cls"); will clear screen on Windows but fail on all other operating systems.


Uninitialized variables

Consider the following line:

unsigned short PNT = 0, Counter;

Why did you give an initial value for PT but not for Counter?


Input validation
See what happens if you enter a non-numeric value. The program goes into a crazy loop. Consider creating a function to validate your input instead of using `scanf`:
int readNumber(int minVal, int maxVal)
{
retry_input:
    char buf[BUFLEN];
    if (fgets(buf, BUFLEN, stdin) == 0)
        return -1;
    int value = strtol(buf, 0, 10);
    if (value < minVal || value > maxVal)
    {
        printf("Please enter a number between %d and %d: ", minVal, maxVal);
        goto retry_input;
    }
    return value;
}

And changing main to the following:

printf("\nEnter an X value: ");
INPUT1 = readNumber(1, 4);
if (INPUT1 == -1)
    return EXIT_FAILURE;

printf("\nEnter a Y value: "); 
INPUT2 = readNumber(1, 4);
if (INPUT2 == -1)
    return EXIT_FAILURE;

if (INPUT1 == RND1 && INPUT2 == RND2)
{
    ...
deleted 2 characters in body
Source Link
jdt
  • 2.4k
  • 5
  • 20

PortablyPortability

The following functions are not portable: sleep() and system("cls"). I had to add this hack to get it to compile with MSVC

#ifdef _WIN32
#include <Windows.h>
#define sleep(x) Sleep(x)
#endif

system("cls"); will clear screen on Windows but fail on all other operating systems.


Uninitialized variables

Consider the following line:

unsigned short PNT = 0, Counter;

Why did you give an initial value for PT but not for Counter?


Input validation
See what happens if you enter a non-numeric value. The program goes into a crazy loop. Consider creating a function to validate your input instead of using `scanf`:
int readNumber(int minVal, int maxVal)
{
retry_input:
    char buf[BUFLEN];
    if (fgets(buf, BUFLEN, stdin) == 0)
        return -1;
    int value = strtol(buf, 0, 10);
    if (value < minVal || value > maxVal)
    {
        printf("Please enter a number between %d and %d: ", minVal, maxVal);
        goto retry_input;
    }
    return value;
}

And changing main to the following:

printf("\nEnter an X value: ");
INPUT1 = readNumber(1, 4);
if (INPUT1 == -1)
    return EXIT_FAILURE;

printf("\nEnter a Y value: "); 
INPUT2 = readNumber(1, 4);
if (INPUT2 == -1)
    return EXIT_FAILURE;

if (INPUT1 == RND1 && INPUT2 == RND2)
{
    ...

Portably

The following functions are not portable: sleep() and system("cls"). I had to add this hack to get it to compile with MSVC

#ifdef _WIN32
#include <Windows.h>
#define sleep(x) Sleep(x)
#endif

system("cls"); will clear screen on Windows but fail on all other operating systems.


Uninitialized variables

Consider the following line:

unsigned short PNT = 0, Counter;

Why did you give an initial value for PT but not for Counter?


Input validation
See what happens if you enter a non-numeric value. The program goes into a crazy loop. Consider creating a function to validate your input instead of using `scanf`:
int readNumber(int minVal, int maxVal)
{
retry_input:
    char buf[BUFLEN];
    if (fgets(buf, BUFLEN, stdin) == 0)
        return -1;
    int value = strtol(buf, 0, 10);
    if (value < minVal || value > maxVal)
    {
        printf("Please enter a number between %d and %d: ", minVal, maxVal);
        goto retry_input;
    }
    return value;
}

And changing main to the following:

printf("\nEnter an X value: ");
INPUT1 = readNumber(1, 4);
if (INPUT1 == -1)
    return EXIT_FAILURE;

printf("\nEnter a Y value: "); 
INPUT2 = readNumber(1, 4);
if (INPUT2 == -1)
    return EXIT_FAILURE;

if (INPUT1 == RND1 && INPUT2 == RND2)
{
    ...

Portability

The following functions are not portable: sleep() and system("cls"). I had to add this to get it to compile with MSVC

#ifdef _WIN32
#include <Windows.h>
#define sleep(x) Sleep(x)
#endif

system("cls"); will clear screen on Windows but fail on all other operating systems.


Uninitialized variables

Consider the following line:

unsigned short PNT = 0, Counter;

Why did you give an initial value for PT but not for Counter?


Input validation
See what happens if you enter a non-numeric value. The program goes into a crazy loop. Consider creating a function to validate your input instead of using `scanf`:
int readNumber(int minVal, int maxVal)
{
retry_input:
    char buf[BUFLEN];
    if (fgets(buf, BUFLEN, stdin) == 0)
        return -1;
    int value = strtol(buf, 0, 10);
    if (value < minVal || value > maxVal)
    {
        printf("Please enter a number between %d and %d: ", minVal, maxVal);
        goto retry_input;
    }
    return value;
}

And changing main to the following:

printf("\nEnter an X value: ");
INPUT1 = readNumber(1, 4);
if (INPUT1 == -1)
    return EXIT_FAILURE;

printf("\nEnter a Y value: "); 
INPUT2 = readNumber(1, 4);
if (INPUT2 == -1)
    return EXIT_FAILURE;

if (INPUT1 == RND1 && INPUT2 == RND2)
{
    ...
Source Link
jdt
  • 2.4k
  • 5
  • 20

Portably

The following functions are not portable: sleep() and system("cls"). I had to add this hack to get it to compile with MSVC

#ifdef _WIN32
#include <Windows.h>
#define sleep(x) Sleep(x)
#endif

system("cls"); will clear screen on Windows but fail on all other operating systems.


Uninitialized variables

Consider the following line:

unsigned short PNT = 0, Counter;

Why did you give an initial value for PT but not for Counter?


Input validation
See what happens if you enter a non-numeric value. The program goes into a crazy loop. Consider creating a function to validate your input instead of using `scanf`:
int readNumber(int minVal, int maxVal)
{
retry_input:
    char buf[BUFLEN];
    if (fgets(buf, BUFLEN, stdin) == 0)
        return -1;
    int value = strtol(buf, 0, 10);
    if (value < minVal || value > maxVal)
    {
        printf("Please enter a number between %d and %d: ", minVal, maxVal);
        goto retry_input;
    }
    return value;
}

And changing main to the following:

printf("\nEnter an X value: ");
INPUT1 = readNumber(1, 4);
if (INPUT1 == -1)
    return EXIT_FAILURE;

printf("\nEnter a Y value: "); 
INPUT2 = readNumber(1, 4);
if (INPUT2 == -1)
    return EXIT_FAILURE;

if (INPUT1 == RND1 && INPUT2 == RND2)
{
    ...