So I have this code:
void read_string(char *dest, int size) {
fgets(dest, size, stdin);
unsigned int i;
for(i=0; i<strlen(dest); i++)
if(dest[i]=='\n')
dest[i]='\0';
}
int main() {
char arr[1000][64];
int i=0;
while(i<1000) {
printf("Enter filename: ");
read_string(arr[i], 62);
if(strlen(arr[i])==0)
break;
i++;
}
}
However, if I copy a string such as:
a
(excluding this line)
...and paste it into the command prompt, the output is as follows:
Enter filename: a
Enter filename:
The loop breaks and the program goes on. My desired outcome in that situation would be for the program to break the loop without displaying the next "Enter filename" message. However, if the user ends their input with a single linefeed, the loop should simply continue.
I'm aware why it doesn't work like intended in that form, but I'm out of ideas at this point. Whatever I try to do, it still works the same.
In case you're wondering why I want it to work in such a weird way, this is a part of my homework assignment, completely unrelated to the main purpose of the exercise. However, my program is failing a unit test because of this.