[Info-vax] ChatGPT solved it for me, again...
bill
bill.gunshannon at gmail.com
Fri Feb 17 10:48:14 EST 2023
On 2/17/2023 9:45 AM, Slo wrote:
> Me: But fgets() is not guaranteed to return a newline character. Can you improve the code?
> ==========================
> You're correct that fgets() may not return the newline character if the
> input buffer is filled before encountering a newline. Here's an updated
> version of the code that handles this case:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
> #include <string.h>
>
> int get_wattage(char *sys) {
> return (rand() % 20) * 50 + 200;
> }
>
> int main() {
> srand(time(NULL));
> char sys1[256], sys2[256];
> int watt1, watt2;
>
> printf("Utility to check power supply compatibility.\n");
> printf("Enter name of system #1: ");
> if (fgets(sys1, 256, stdin) == NULL) {
> perror("fgets failed");
> exit(1);
> }
> sys1[strcspn(sys1, "\n")] = '\0';
> if (sys1[0] == '\0') {
> fprintf(stderr, "Invalid input: system name cannot be empty\n");
> exit(1);
> }
> printf("Enter name of system #2: ");
> if (fgets(sys2, 256, stdin) == NULL) {
> perror("fgets failed");
> exit(1);
> }
> sys2[strcspn(sys2, "\n")] = '\0';
> if (sys2[0] == '\0') {
> fprintf(stderr, "Invalid input: system name cannot be empty\n");
> exit(1);
> }
> watt1 = get_wattage(sys1);
> watt2 = get_wattage(sys2);
>
> if (watt1 == watt2) {
> printf("%s and %s can use the same power supply (%d watt)\n", sys1, sys2, watt1);
> } else {
> printf("%s and %s cannot use the same power supply as they use %d and %d watt\n", sys1, sys2, watt1, watt2);
> }
> return 0;
> }
> ==========================
>
And people are already talking about trusting the code written by
the robot. :-) :-) :-)
bill
More information about the Info-vax
mailing list