Store An Unknown "Infinite" Amount Of Numbers From User Input Into A Buffer | C Programming Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ธ.ค. 2024

ความคิดเห็น • 10

  • @pusetsogabert9776
    @pusetsogabert9776 8 หลายเดือนก่อน

    Hey, how would I restructure this to get an infinite number of strings?

  • @drig7847
    @drig7847 ปีที่แล้ว

    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;
    }
    }

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      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.

  • @Mnogojazyk
    @Mnogojazyk 2 ปีที่แล้ว

    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?

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +2

      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;
      }

  • @geetachavan2299
    @geetachavan2299 2 ปีที่แล้ว

    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

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      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.

  • @lukfiorentino9028
    @lukfiorentino9028 ปีที่แล้ว

    Why dont use realloc every time that user adds an int, instead that using a limited size buffer

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +2

      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. :-)

    • @lukfiorentino9028
      @lukfiorentino9028 ปีที่แล้ว

      @@PortfolioCourses wow thanks for answering I wasn't expecting that, lov your content BTW keep going