Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds support for optional args, with backwards compatibility #64

Merged
merged 12 commits into from
May 15, 2017
Prev Previous commit
Next Next commit
added support for optional arguments, but relying on std=gnu99
dmalan committed Apr 9, 2017
commit 4692380c9d53303c82f736bc90b8722a1d4e90bf
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ DESCRIPTION = CS50 Library for C
MAINTAINER = CS50 <[email protected]>
NAME = libcs50
OLD_NAMES = lib50-c library50-c
VERSION = 7.2.0
VERSION = 8.0.0

.PHONY: bash
bash:
@@ -111,6 +111,7 @@ rpm: build
# TODO: improve test suite
.PHONY: test
test: build hackerrank
#clang -ggdb3 -I "$(INCLUDE_DIR)" -O0 -std=c99 -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/eprintf.c" -L "$(LIB_DIR)" -lcs50 -o "$(EPRINTF_EXE)"
#clang -I "$(BUILD_DIR)" -std=c99 -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/hackerrank.c" -o "$(HR_EXE)"
clang -ggdb3 -I "$(INCLUDE_DIR)" -O0 -std=gnu99 -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/get_int.c" -L "$(LIB_DIR)" -lcs50 -o "$(BUILD_DIR)"/get_int
#clang -ggdb3 -I "$(INCLUDE_DIR)" -O0 -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/eprintf.c" -L "$(LIB_DIR)" -lcs50 -o "$(EPRINTF_EXE)"
#clang -I "$(BUILD_DIR)" -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/hackerrank.c" -o "$(HR_EXE)"
clang -ggdb3 -I "$(INCLUDE_DIR)" -O0 -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/get_int.c" -L "$(LIB_DIR)" -lcs50 -o "$(BUILD_DIR)"/get_int
LD_LIBRARY_PATH="$(LIB_DIR)" "$(BUILD_DIR)"/get_int
59 changes: 45 additions & 14 deletions src/cs50.c
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@ char get_char(string prompt)
while (true)
{
// get line of text, returning CHAR_MAX on failure
string line = get_string();
string line = get_string(prompt);
if (line == NULL)
{
return CHAR_MAX;
@@ -106,12 +106,17 @@ char get_char(string prompt)
{
return c;
}
printf("%s", prompt);

// temporarily here for backwards compatibility
if (prompt == NULL)
{
printf("Retry: ");
}
}
}
char GetChar(void)
{
return get_char();
return get_char(NULL);
}

/**
@@ -126,7 +131,7 @@ double get_double(string prompt)
while (true)
{
// get line of text, returning DBL_MAX on failure
string line = get_string();
string line = get_string(prompt);
if (line == NULL)
{
return DBL_MAX;
@@ -147,12 +152,17 @@ double get_double(string prompt)
}
}
}
printf("%s", prompt);

// temporarily here for backwards compatibility
if (prompt == NULL)
{
printf("Retry: ");
}
}
}
double GetDouble(void)
{
return get_double();
return get_double(NULL);
}

/**
@@ -167,7 +177,7 @@ float get_float(string prompt)
while (true)
{
// get line of text, returning FLT_MAX on failure
string line = get_string();
string line = get_string(prompt);
if (line == NULL)
{
return FLT_MAX;
@@ -188,12 +198,17 @@ float get_float(string prompt)
}
}
}
printf("%s", prompt);

// temporarily here for backwards compatibility
if (prompt == NULL)
{
printf("Retry: ");
}
}
}
float GetFloat(void)
{
return get_float();
return get_float(NULL);
}

/**
@@ -208,7 +223,7 @@ int get_int(string prompt)
while (true)
{
// get line of text, returning INT_MAX on failure
string line = get_string();
string line = get_string(prompt);
if (line == NULL)
{
return INT_MAX;
@@ -225,12 +240,17 @@ int get_int(string prompt)
return n;
}
}
printf("%s", prompt);

// temporarily here for backwards compatibility
if (prompt == NULL)
{
printf("Retry: ");
}
}
}
int GetInt(void)
{
return get_int();
return get_int(NULL);
}

/**
@@ -262,12 +282,17 @@ long long get_long_long(string prompt)
return n;
}
}
printf("%s", prompt);

// temporarily here for backwards compatibility
if (prompt == NULL)
{
printf("Retry: ");
}
}
}
long long GetLongLong(void)
{
return get_long_long();
return get_long_long(NULL);
}

/**
@@ -308,6 +333,12 @@ string get_string(string prompt)
// character read or EOF
int c;

// prompt user
if (prompt != NULL)
{
printf("%s", prompt);
}

// iteratively get characters from standard input, checking for CR (Mac OS), LF (Linux), and CRLF (Windows)
while ((c = fgetc(stdin)) != '\r' && c != '\n' && c != EOF)
{
10 changes: 10 additions & 0 deletions tests/get_int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include "cs50.h"

int main(void)
{
int i = get_int();
printf("%i\n", i);
int j = get_int("Foo: ");
printf("%i\n", j);
}