Help Me Debug My C Program Input Issue

0
4
Asked By CleverCoder123 On

I'm working on a C programming project that involves a movie database management system. After compiling, when I run the program, it prompts for user input. However, no matter what I enter, I get an "invalid input" error. I've tried debugging on my own and even used AI tools, but the problem still persists. Could someone take a look at my code and help identify the issue? Here's the relevant part of my code:

```c
#include

struct Movie{
char Title[100];
char Director[100];
int Year;
float Rating;
};

#define Max_Movies 100
struct Movie database[Max_Movies];
int movie_count=0;

void add_movie() {
if(movie_count >= Max_Movies) {
printf("nError: The Database is Full.");
return;
}
printf("Enter Movie Title:");
scanf("%[^n]", database[movie_count].Title);
...
}

int main() {
int choice;
while(1) {
printf("...n");
if(scanf("%d", &choice) != 1) {
printf("Invalid input. Please enter a number between 1 and 4.n");
// Error handling
continue;
}
switch(choice) {
case 1:
add_movie();
break;
// other cases
}
}
}
```

I'd really appreciate any assistance!

4 Answers

Answered By LogicGuru On

I noticed some formatting warnings in your code as well. Like for example, you're using `&database[i].Year` in your print statements while it should just be `database[i].Year`. The same goes for your floating-point numbers. You might want to check those to avoid errors during execution.

TechieTim -

Good catch! Output-format mismatches can definitely cause issues. Always make sure to match variable types with the correct format specifiers.

Answered By SyntaxNinja On

Before diving too deep, make sure your code is properly formatted. Use code blocks when posting, so it’s easier for others to read and debug. This small edit might save everyone a lot of time and confusion!

CodeCrafter -

Definitely! Proper indentation and formatting can make a huge difference when debugging, so it's worth the effort.

Answered By BugSquasher42 On

It looks like one of the issues might be in your handling of user input in the `main` function. Specifically, the line `if(scanf("%d", &choice) != 1)` is checking the return value of `scanf`. If the format doesn't match, `scanf` won't convert anything and will return 0 instead of 1. You should also clear the input buffer to avoid leftovers from invalid input. Try modifying how you handle this part and make sure to validate inputs properly!

InputNinja99 -

Yeah, also check that you properly clear the input buffer after any invalid input, otherwise the next read can fail again. You could use a loop to keep reading until you get a newline.

Answered By DebuggingDiva On

You may have also missed something in the `fprintf` calls in your `load_database` function. You're trying to use `fprintf` where you should be using `fscanf` to read data from the file. Make sure you switch those up!

CodeWhisperer -

Exactly! Using `fprintf` suggests you're trying to write data to a file, not reading from it. Be sure to correct that!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.