53
53
#include "cs50.h"
54
54
55
55
/**
56
- * Prints an error message, formatted like printf, to standard error, prefixing it with program's
57
- * name as well as the file and line number from which function was called, which a macro is
58
- * expected to provide.
56
+ * Prints an error message, formatted like printf, to standard error, prefixing it with
57
+ * file name and line number from which function was called (which a macro provides).
59
58
*
60
59
* This function is not intended to be called directly. Instead, call the macro of the same name,
61
60
* which expects fewer arguments.
@@ -84,18 +83,17 @@ void eprintf(const char *file, int line, const char *format, ...)
84
83
}
85
84
86
85
/**
87
- * Reads a line of text from standard input and returns the equivalent
88
- * char; if text does not represent a char, user is prompted to retry.
89
- * Leading and trailing whitespace is ignored. If line can't be read,
90
- * returns CHAR_MAX.
86
+ * Prompts user for a line of text from standard input and returns the
87
+ * equivalent char; if text is not a single char, user is prompted
88
+ * to retry. If line can't be read, returns CHAR_MAX.
91
89
*/
92
- char get_char (void )
90
+ char get_char (string prompt )
93
91
{
94
92
// try to get a char from user
95
93
while (true)
96
94
{
97
95
// get line of text, returning CHAR_MAX on failure
98
- string line = get_string ();
96
+ string line = get_string (prompt );
99
97
if (line == NULL )
100
98
{
101
99
return CHAR_MAX ;
@@ -107,24 +105,32 @@ char get_char(void)
107
105
{
108
106
return c ;
109
107
}
110
- printf ("Retry: " );
108
+
109
+ // temporarily here for backwards compatibility
110
+ if (prompt == NULL )
111
+ {
112
+ printf ("Retry: " );
113
+ }
111
114
}
112
115
}
113
- char (* GetChar )(void ) = get_char ;
116
+ char GetChar (void )
117
+ {
118
+ return get_char (NULL );
119
+ }
114
120
115
121
/**
116
- * Reads a line of text from standard input and returns the equivalent
117
- * double as precisely as possible; if text does not represent a
118
- * double or if value would cause underflow or overflow, user is
122
+ * Prompts user for a line of text from standard input and returns the
123
+ * equivalent double as precisely as possible; if text does not represent
124
+ * a double or if value would cause underflow or overflow, user is
119
125
* prompted to retry. If line can't be read, returns DBL_MAX.
120
126
*/
121
- double get_double (void )
127
+ double get_double (string prompt )
122
128
{
123
129
// try to get a double from user
124
130
while (true)
125
131
{
126
132
// get line of text, returning DBL_MAX on failure
127
- string line = get_string ();
133
+ string line = get_string (prompt );
128
134
if (line == NULL )
129
135
{
130
136
return DBL_MAX ;
@@ -145,24 +151,32 @@ double get_double(void)
145
151
}
146
152
}
147
153
}
148
- printf ("Retry: " );
154
+
155
+ // temporarily here for backwards compatibility
156
+ if (prompt == NULL )
157
+ {
158
+ printf ("Retry: " );
159
+ }
149
160
}
150
161
}
151
- double (* GetDouble )(void ) = get_double ;
162
+ double GetDouble (void )
163
+ {
164
+ return get_double (NULL );
165
+ }
152
166
153
167
/**
154
- * Reads a line of text from standard input and returns the equivalent
155
- * float as precisely as possible; if text does not represent a float
156
- * or if value would cause underflow or overflow, user is prompted to
157
- * retry. If line can't be read, returns FLT_MAX.
168
+ * Prompts user for a line of text from standard input and returns the
169
+ * equivalent float as precisely as possible; if text does not represent
170
+ * a float or if value would cause underflow or overflow, user is prompted
171
+ * to retry. If line can't be read, returns FLT_MAX.
158
172
*/
159
- float get_float (void )
173
+ float get_float (string prompt )
160
174
{
161
175
// try to get a float from user
162
176
while (true)
163
177
{
164
178
// get line of text, returning FLT_MAX on failure
165
- string line = get_string ();
179
+ string line = get_string (prompt );
166
180
if (line == NULL )
167
181
{
168
182
return FLT_MAX ;
@@ -183,24 +197,32 @@ float get_float(void)
183
197
}
184
198
}
185
199
}
186
- printf ("Retry: " );
200
+
201
+ // temporarily here for backwards compatibility
202
+ if (prompt == NULL )
203
+ {
204
+ printf ("Retry: " );
205
+ }
187
206
}
188
207
}
189
- float (* GetFloat )(void ) = get_float ;
208
+ float GetFloat (void )
209
+ {
210
+ return get_float (NULL );
211
+ }
190
212
191
213
/**
192
- * Reads a line of text from standard input and returns it as an
193
- * int in [-2^31, 2^31 - 1), if possible; if text does not represent
194
- * such an int or if value would cause underflow or overflow, user is
195
- * prompted to retry. If line can't be read, returns INT_MAX.
214
+ * Prompts user for a line of text from standard input and returns the
215
+ * equivalent int; if text does not represent an int in [-2^31, 2^31 - 1)
216
+ * or would cause underflow or overflow, user is prompted to retry. If line
217
+ * can't be read, returns INT_MAX.
196
218
*/
197
- int get_int (void )
219
+ int get_int (string prompt )
198
220
{
199
221
// try to get an int from user
200
222
while (true)
201
223
{
202
224
// get line of text, returning INT_MAX on failure
203
- string line = get_string ();
225
+ string line = get_string (prompt );
204
226
if (line == NULL )
205
227
{
206
228
return INT_MAX ;
@@ -217,18 +239,26 @@ int get_int(void)
217
239
return n ;
218
240
}
219
241
}
220
- printf ("Retry: " );
242
+
243
+ // temporarily here for backwards compatibility
244
+ if (prompt == NULL )
245
+ {
246
+ printf ("Retry: " );
247
+ }
221
248
}
222
249
}
223
- int (* GetInt )(void ) = get_int ;
250
+ int GetInt (void )
251
+ {
252
+ return get_int (NULL );
253
+ }
224
254
225
255
/**
226
- * Reads a line of text from standard input and returns an equivalent
227
- * long long in [-2^63, 2^63 - 1), if possible ; if text does not
228
- * represent such a long long or if value would cause underflow or overflow,
229
- * user is prompted to retry. If line can't be read, returns LLONG_MAX.
256
+ * Prompts user for a line of text from standard input and returns the
257
+ * equivalent long long ; if text does not represent a long long in
258
+ * [-2^63, 2^63 - 1) or would cause underflow or overflow, user is
259
+ * prompted to retry. If line can't be read, returns LLONG_MAX.
230
260
*/
231
- long long get_long_long (void )
261
+ long long get_long_long (string prompt )
232
262
{
233
263
// try to get a long long from user
234
264
while (true)
@@ -251,10 +281,18 @@ long long get_long_long(void)
251
281
return n ;
252
282
}
253
283
}
254
- printf ("Retry: " );
284
+
285
+ // temporarily here for backwards compatibility
286
+ if (prompt == NULL )
287
+ {
288
+ printf ("Retry: " );
289
+ }
255
290
}
256
291
}
257
- long long (* GetLongLong )(void ) = get_long_long ;
292
+ long long GetLongLong (void )
293
+ {
294
+ return get_long_long (NULL );
295
+ }
258
296
259
297
/**
260
298
* Number of strings allocated by get_string.
@@ -267,14 +305,14 @@ static size_t allocations = 0;
267
305
static string * strings = NULL ;
268
306
269
307
/**
270
- * Reads a line of text from standard input and returns it as
271
- * a string (char *), sans trailing line ending. Supports
308
+ * Prompts user for a line of text from standard input and returns
309
+ * it as a string (char *), sans trailing line ending. Supports
272
310
* CR (\r), LF (\n), and CRLF (\r\n) as line endings. If user
273
- * inputs only "\n" , returns "", not NULL. Returns NULL upon
274
- * error or no input whatsoever (i.e., just EOF). Stores string
311
+ * inputs only a line ending , returns "", not NULL. Returns NULL
312
+ * upon error or no input whatsoever (i.e., just EOF). Stores string
275
313
* on heap, but library's destructor frees memory on program's exit.
276
314
*/
277
- string get_string (void )
315
+ string get_string (string prompt )
278
316
{
279
317
// check whether we have room for another string
280
318
if (allocations * sizeof (string ) == SIZE_MAX )
@@ -294,6 +332,12 @@ string get_string(void)
294
332
// character read or EOF
295
333
int c ;
296
334
335
+ // prompt user
336
+ if (prompt != NULL )
337
+ {
338
+ printf ("%s" , prompt );
339
+ }
340
+
297
341
// iteratively get characters from standard input, checking for CR (Mac OS), LF (Linux), and CRLF (Windows)
298
342
while ((c = fgetc (stdin )) != '\r' && c != '\n' && c != EOF )
299
343
{
0 commit comments