Hey you all!
I'm a beginner in C programming and I've a little big problem in a project with the printf() function (I mean, I'm almost sure about that). The function just has to print some fields of an array of emails stored in a databse.
I'll try to explain it. The problem is in these lines:
Code:
//LIST ALL EXISTING EMAILS
for (int i = 0; i < db->email_count; ++i) {
printf("\n");
printf("%d.", i);
printf(" %s ", db->emails[i].from);
printf(" %s", db->emails[i].subject);
printf("%s",db->emails[i].date); //PROBLEM
}
printf(NEW_LINE NEW_LINE);
//SHOW THE CONTENT OF THE SELECTED EMAIL
int select_num = 0;
printf(INSTRUCTION1);//asks the user a number
scanf("%d", select_num);
//ERROR
printf("\nID: %s", db->emails[select_num].id);
printf("\n\nDate: %s", db->emails[select_num].date);
printf("\nFrom: %s", db->emails[select_num].from);
printf("\nTo: %s", db->emails[select_num].to);
printf("\nSubject:%s", db->emails[select_num].subject);
printf("\n\nBody: %s", db->emails[select_num].body);
These lines appear just like that, in the same function in my program. But the printfs obey only until the scanf(). After take the number from the user (a number smaller than the email_count), the program finishes with a: "RUN FAILED (exit value 1, total time: 7s)". That's the "ERROR". You will notice that there is no difference between the first section of printfs and the second one. The question is: WHY?
I have to say one more thing: the date, in the first section of printfs, it just not appears despite the printf("PROBLEM"). It has the format "Tue, 07 Feb 2017 21:32:46 +0100 (CET)" and I'd swear that's it's correctly saved in the string "db->emails[i].date", because of that:
I want to make clear that:
- Firstly, the format (%s or %d) is correct in all cases.
- The content I want to print is loaded in every address (when I print these fields(date,body,subject) into the function which fills them, the content appears without any problem). In addition, note that in this case, the first section of printfs wouldn't work.
- There is no special case with the db->emails.date. It follows exactly the same patron as the other strings when I fill it, and I can also print it in the function that put the date in the address.
- If I don't print the date in both sections, the "ERROR" persists.
PD: I have a strange problem with the printf and the '\n'. Sometimes, and it depens on the place where I put the '\n' in the printf(), the strings that I want to print appear cut.
Excuse my poor English, and thank you VERY much.This problem is infuriating!