![]() |
|
[Help] Help with C input handling - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5) +---- Thread: [Help] Help with C input handling (/showthread.php?tid=6288659) |
Help with C input handling - TomDH - Jan 2, 2025 hey, I'm writing a C program that simulates a "low cost" spotify, only storing names, artists, lyrics, and streams in the program the code works fine but there's one key issue if I enter the input in cmd line by line, it works perfectly if I take the input from a file, it works perfectly but if I paste it all at the same time, it won't register the last input unless there's a whitespace after it this is my main loop: Code int main() {and in the functions themselves I do not clear the buffer unless after I get a scanf to get a choice in inner-menus when I try to get the input with getchar() instead of scanf it just breaks the code entirely and removes first characters from longer inputs in the future, and prints the menu twice sometimes. would appreciate some help on how to get the last input even when it isn't followed by a whitespace TLDR : pasting multiple lines of input into cmd and it stalls when input doesn't end with whitespace Thanks in advance to anyone helping out! RE: Help with C input handling - Bastille - Jan 2, 2025 Hello, I'm not into programming but this is what I've got with ChatGPT, could you try ? AI Quote: The issue you're facing is due to how scanf interacts with the input buffer. When you paste multiple lines into the terminal, scanf can leave a newline (\n) or other characters in the input buffer. This often results in the last input being unprocessed if not followed by a whitespace or newline, especially when using scanf("%d", &choice). Here's how you can fix or mitigate the issue: Key Points:
Updated Main Loop with Buffer Management: Replace your scanf("%d", &choice) with a safer approach: Code #include <stdio.h>Why This Works: fgets Reads Entire Line: Ensures that any leftover newline characters are included in the input, preventing issues when pasting multiple lines of input. sscanf for Parsing: Safely converts the input string into an integer while avoiding issues caused by stray characters in the buffer. Buffer Size: Use a sufficiently large buffer (char buffer[100]) to handle user input, but ensure it aligns with your maximum expected input size. Notes: Menu Loop Safety: Adding clear prompts and handling invalid inputs ensures your program doesn’t break when unexpected input is given. Test Edge Cases: Paste inputs with trailing newlines, without trailing spaces, or even with invalid characters to verify robustness. RE: Help with C input handling - HFDone - Jan 2, 2025 so put check for the last line RE: Help with C input handling - Phobia_ - Jan 3, 2025 Couldn’t this be done more easily with Spotify API? |