a shorter code of checking if user inputs the terminating value (-1) : int main() { double buffer[1024]; double input=0; double total=0; while(scanf("%lf",&input)==1 && input != -1) { buffer[total++] = input; } }
Thanks for sharing, though for those reading the comments, the buffer here is fixed in size which is a bit different than a buffer that re-sizes as needed.
Can the program be reworked so that the sentinel is either an empty input string when soliciting input from a user, or when the end of the file is reached, the buffer is closed to new entries, so to speak, and the buffer's contents are displayed to screen?
Yes, it could be re-worked like that if we used a char array to store the string as input, strcmp() to check for an empty input string, and atof() to convert the string to a double. It would look like this: #include #include #include #include int main(int argc, char *argv[]) { double *buffer; size_t buffer_size = 32768; buffer = malloc(sizeof(double) * buffer_size); size_t total = 0; char string_input[1024]; double input = 0; while (true) { fgets(string_input, 1024, stdin); if (strcmp(string_input, " ") == 0) break; input = atof(string_input); buffer[total] = input; total++; if (total >= buffer_size) { buffer_size += 32768; buffer = realloc(buffer, buffer_size * sizeof(double)); } } for (size_t i = 0; i < total; i++) printf("%f ", buffer[i]); free(buffer); return 0; }
I have tried this but it's not taking input from command line and if I run using gcc -o main main.c and click enter to give input as 2 4.4 6 7 Then its directly giving 0.000000 0.000000
Just because it's not "free" to call realloc(), the function will take time to execute, so we try to call it less frequently. If this isn't a concern, then yes we could just use realloc() for each int. :-)
Hey, how would I restructure this to get an infinite number of strings?
a shorter code of checking if user inputs the terminating value (-1) :
int main()
{
double buffer[1024];
double input=0;
double total=0;
while(scanf("%lf",&input)==1 && input != -1)
{
buffer[total++] = input;
}
}
Thanks for sharing, though for those reading the comments, the buffer here is fixed in size which is a bit different than a buffer that re-sizes as needed.
Can the program be reworked so that the sentinel is either an empty input string when soliciting input from a user, or when the end of the file is reached, the buffer is closed to new entries, so to speak, and the buffer's contents are displayed to screen?
Yes, it could be re-worked like that if we used a char array to store the string as input, strcmp() to check for an empty input string, and atof() to convert the string to a double. It would look like this:
#include
#include
#include
#include
int main(int argc, char *argv[])
{
double *buffer;
size_t buffer_size = 32768;
buffer = malloc(sizeof(double) * buffer_size);
size_t total = 0;
char string_input[1024];
double input = 0;
while (true)
{
fgets(string_input, 1024, stdin);
if (strcmp(string_input, "
") == 0) break;
input = atof(string_input);
buffer[total] = input;
total++;
if (total >= buffer_size)
{
buffer_size += 32768;
buffer = realloc(buffer, buffer_size * sizeof(double));
}
}
for (size_t i = 0; i < total; i++)
printf("%f
", buffer[i]);
free(buffer);
return 0;
}
I have tried this but it's not taking input from command line and if I run using
gcc -o main main.c and click enter to give input as 2 4.4 6 7
Then its directly giving 0.000000 0.000000
Great question Geeta. :-) The program requires the user to hit enter (or a newline
) after each number so giving the input like that won't work.
Why dont use realloc every time that user adds an int, instead that using a limited size buffer
Just because it's not "free" to call realloc(), the function will take time to execute, so we try to call it less frequently. If this isn't a concern, then yes we could just use realloc() for each int. :-)
@@PortfolioCourses wow thanks for answering I wasn't expecting that, lov your content BTW keep going