Skip to content

Commit 4692380

Browse files
committed
added support for optional arguments, but relying on std=gnu99
1 parent b4de9b3 commit 4692380

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ DESCRIPTION = CS50 Library for C
2525
MAINTAINER = CS50 <[email protected]>
2626
NAME = libcs50
2727
OLD_NAMES = lib50-c library50-c
28-
VERSION = 7.2.0
28+
VERSION = 8.0.0
2929

3030
.PHONY: bash
3131
bash:
@@ -111,6 +111,7 @@ rpm: build
111111
# TODO: improve test suite
112112
.PHONY: test
113113
test: build hackerrank
114-
#clang -ggdb3 -I "$(INCLUDE_DIR)" -O0 -std=c99 -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/eprintf.c" -L "$(LIB_DIR)" -lcs50 -o "$(EPRINTF_EXE)"
115-
#clang -I "$(BUILD_DIR)" -std=c99 -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/hackerrank.c" -o "$(HR_EXE)"
116-
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
114+
#clang -ggdb3 -I "$(INCLUDE_DIR)" -O0 -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/eprintf.c" -L "$(LIB_DIR)" -lcs50 -o "$(EPRINTF_EXE)"
115+
#clang -I "$(BUILD_DIR)" -Wall -Werror -Wno-deprecated-declarations "$(TESTS_DIR)/hackerrank.c" -o "$(HR_EXE)"
116+
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
117+
LD_LIBRARY_PATH="$(LIB_DIR)" "$(BUILD_DIR)"/get_int

src/cs50.c

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ char get_char(string prompt)
9494
while (true)
9595
{
9696
// get line of text, returning CHAR_MAX on failure
97-
string line = get_string();
97+
string line = get_string(prompt);
9898
if (line == NULL)
9999
{
100100
return CHAR_MAX;
@@ -106,12 +106,17 @@ char get_char(string prompt)
106106
{
107107
return c;
108108
}
109-
printf("%s", prompt);
109+
110+
// temporarily here for backwards compatibility
111+
if (prompt == NULL)
112+
{
113+
printf("Retry: ");
114+
}
110115
}
111116
}
112117
char GetChar(void)
113118
{
114-
return get_char();
119+
return get_char(NULL);
115120
}
116121

117122
/**
@@ -126,7 +131,7 @@ double get_double(string prompt)
126131
while (true)
127132
{
128133
// get line of text, returning DBL_MAX on failure
129-
string line = get_string();
134+
string line = get_string(prompt);
130135
if (line == NULL)
131136
{
132137
return DBL_MAX;
@@ -147,12 +152,17 @@ double get_double(string prompt)
147152
}
148153
}
149154
}
150-
printf("%s", prompt);
155+
156+
// temporarily here for backwards compatibility
157+
if (prompt == NULL)
158+
{
159+
printf("Retry: ");
160+
}
151161
}
152162
}
153163
double GetDouble(void)
154164
{
155-
return get_double();
165+
return get_double(NULL);
156166
}
157167

158168
/**
@@ -167,7 +177,7 @@ float get_float(string prompt)
167177
while (true)
168178
{
169179
// get line of text, returning FLT_MAX on failure
170-
string line = get_string();
180+
string line = get_string(prompt);
171181
if (line == NULL)
172182
{
173183
return FLT_MAX;
@@ -188,12 +198,17 @@ float get_float(string prompt)
188198
}
189199
}
190200
}
191-
printf("%s", prompt);
201+
202+
// temporarily here for backwards compatibility
203+
if (prompt == NULL)
204+
{
205+
printf("Retry: ");
206+
}
192207
}
193208
}
194209
float GetFloat(void)
195210
{
196-
return get_float();
211+
return get_float(NULL);
197212
}
198213

199214
/**
@@ -208,7 +223,7 @@ int get_int(string prompt)
208223
while (true)
209224
{
210225
// get line of text, returning INT_MAX on failure
211-
string line = get_string();
226+
string line = get_string(prompt);
212227
if (line == NULL)
213228
{
214229
return INT_MAX;
@@ -225,12 +240,17 @@ int get_int(string prompt)
225240
return n;
226241
}
227242
}
228-
printf("%s", prompt);
243+
244+
// temporarily here for backwards compatibility
245+
if (prompt == NULL)
246+
{
247+
printf("Retry: ");
248+
}
229249
}
230250
}
231251
int GetInt(void)
232252
{
233-
return get_int();
253+
return get_int(NULL);
234254
}
235255

236256
/**
@@ -262,12 +282,17 @@ long long get_long_long(string prompt)
262282
return n;
263283
}
264284
}
265-
printf("%s", prompt);
285+
286+
// temporarily here for backwards compatibility
287+
if (prompt == NULL)
288+
{
289+
printf("Retry: ");
290+
}
266291
}
267292
}
268293
long long GetLongLong(void)
269294
{
270-
return get_long_long();
295+
return get_long_long(NULL);
271296
}
272297

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

336+
// prompt user
337+
if (prompt != NULL)
338+
{
339+
printf("%s", prompt);
340+
}
341+
311342
// iteratively get characters from standard input, checking for CR (Mac OS), LF (Linux), and CRLF (Windows)
312343
while ((c = fgetc(stdin)) != '\r' && c != '\n' && c != EOF)
313344
{

tests/get_int.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
#include "cs50.h"
3+
4+
int main(void)
5+
{
6+
int i = get_int();
7+
printf("%i\n", i);
8+
int j = get_int("Foo: ");
9+
printf("%i\n", j);
10+
}

0 commit comments

Comments
 (0)