PHP Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#define NAME_LEN 21
#define BUF_SIZE 21
#define AGE_MIN 10
#define AGE_MAX 100
#define ID_MIN 1
#define ID_MAX 999999
#define QUIT_STRING "exit"
void get_string(char* prompt, char* string, unsigned int string_length)
{
char buffer[BUF_SIZE];
char c;
printf("%s", prompt);
/* Read in BUF_SIZE characters from standard input and store it in buffer. */
fgets(buffer, BUF_SIZE, stdin);
/* If the user types "exit" at any point, then that's what we need to do. */
if (strncmp(buffer, QUIT_STRING, strlen(QUIT_STRING)) == 0)
exit(EXIT_SUCCESS);
/* This code could use some explaining. What happens when you type in a
string longer than the buffer is that it copies over the length of the
buffer, but leaves the rest of what was entered still in the input queue.
For most compilers, fflush() is only designed for output buffers, and
using it on input buffers has an unknown effect. (However, I believe that
MS VC++'s version of fflush may be used on stdin.) What this code does is
(1) checks to see if our local buffer string is full, and (2) if this is
the case, then that means we have some "overflow" still in the stdin
queue. The while loop then reads in the rest of the unwanted characters
from the queue and discards them, so we won't have problems when we try
and read new user input in the future. */
if (strlen(buffer) >= BUF_SIZE - 1)
while ((c = fgetc(stdin)) && c != '\n');
/* Copies our local copy to the string whose address was passed in. This
only copies up to string_length characters. */
strncpy(string, buffer, string_length);
}
void get_username(char* prompt, char* string, unsigned int string_length)
{
char username[BUF_SIZE];
int hasChars = 0;
int i;
do {
get_string(prompt, username, BUF_SIZE);
if (username[0] == ' ') {
printf("Sorry, your username cannot begin with a space.\n");
continue;
}
/* This scans the string to see if there is at least one character (as
opposed to all numbers) in the string. In addition, it gets rid of the
newline character which is kept on from the fgets() function called in
the get_string function. */
for (i = 0; i < BUF_SIZE; i++) {
if (username[i] == '\n'){
username[i] = '\0';
break;
}
if (isalpha(username[i])) {
hasChars = 1;
}
}
if (! hasChars)
printf("Sorry, your username must contain at least 1 character.\n");
} while ((! hasChars) || username[0] == ' ');
strncpy(string, username, string_length);
}
unsigned int get_number(char* prompt, unsigned int min, unsigned int max)
{
char input_string[BUF_SIZE];
unsigned int number = 0;
do {
printf("%s (%d-%d): ", prompt, min, max);
get_string("", input_string, BUF_SIZE);
/* Converts the string representation of the number to an actual integer
stored value. */
number = (unsigned int)atoi(input_string);
/* atoi will return 0 if an error has occured, so we must check the first
character to see if it is actually a 0, or if atoi just returned with an
error. */
if (number == 0 && input_string[0] != '0') {
printf("That wasn't a number. Please try again.\n");
continue;
}
} while (number < min || number > max);
return number;
}
int main(int argc, char* argv[])
{
char name[NAME_LEN]; name[0] = '\0';
unsigned int age, id;
/* We want to make sure that the quit string can actually fit inside the
buffer. Otherwise, all hell may break lose. */
assert(strlen(QUIT_STRING) < BUF_SIZE);
printf("Type '%s' at any time to quit.\n\n", QUIT_STRING);
/* Retrieve and validate all input */
age = get_number("Please enter your age", AGE_MIN, AGE_MAX);
id = get_number("Please enter your user id", ID_MIN, ID_MAX);
get_username("Please enter your forum name: ", name, NAME_LEN);
/* Output the final results */
printf("You are %s, aged %d next year you will be %d, with user id %d, the next user is %d.\n", name, age, age+1, id, id+1);
/* We are home at last! */
return EXIT_SUCCESS;
}
EDIT: I changed the CODE tags to PHP tags, so the code is easier on your eyes, even though the OCD side of me is making me twitch because it's labeled something that it's not.
Bookmarks