Haven't used C in a while and get -1073741819 (0xC0000005) after I ask a user for the d, m, y inputs. I assume it's something to do with how I use pointers. I have created a different program using the pointers similarly, and it all worked fine and dandy. Don't really know what else to say. Why would the program not work. I feel like it's something ridiculously obvious and stupid, but I just can't see it. :D
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void date (int *d, int *m, int *y);
void name (char vards[20], char uzvards[20]);
void income (float *ienakums);
void writefile(int *y,int *m, int *d, char vards[20],char uzvards[20],float *ienakums);
int main(void) {
char Name[20], last[20];
int diena,menesis,gads;
float cash;
// step 1 - get name check
name(&Name[20], &last[20]);
// step 2 - get date check
date(&diena,&menesis,&gads);
// step 3 - get income check
income(&cash);
// step 4 - write the file
writefile(&gads,&menesis,&diena,&Name[20],&last[20],&cash);
}
void name(char vards[20], char uzvards[20]){
int check = 1;
int counter = 0;
do{
printf("Ievadiet jusu vardu\n");
gets(vards);
for(int i=0; i<strlen(vards); i )
{
if(isdigit(vards[i]) > 0)
{
counter ;
}
}
if(counter == 0)
{
check = 0;
}
else{
check = 1;
}
}while(check == 1);
check = 0;
do{
printf("Ievadiet jusu uzvardu\n");
gets(uzvards);
for(int i=0; i<strlen(vards); i )
{
if(isdigit(vards[i]) > 0)
{
counter ;
}
}
if(counter == 0)
{
check = 0;
}
else{
check = 1;
}
}while(check == 1);
printf("\nJusu vards ir %s %s",vards,uzvards);
}
void date (int *d, int *m, int *y){
*d = 0;
*m = 0;
*y = 0;
do{
printf("\nLudzu ievadiet jusu dzimsanas dienu: ");
fflush(stdin);
scanf("%d", &d);
}while((d<=0) || (d>31));
do{
printf("\nLudzu ievadiet jusu dzimsanas menesi: ");
fflush(stdin);
scanf("%d", &m);
}while((m<=0) || (m>12));
do{
printf("\nLudzu ievadiet jusu dzimsanas gadu: ");
fflush(stdin);
scanf("%d", &y);
}while((y<1900) || (y>2021));
printf("\n %d %d %d",*d,*m,*y);
}
void income (float *ienakums){
//do{
printf("\nIevadiet videjos ienakumus: ");
fflush(stdin);
scanf("%f", &ienakums);
//}while(isdigit(ienakums) <= 0);
printf("%f",*ienakums);
}
void writefile(int *y,int *m, int *d, char vards[20],char uzvards[20],float *ienakums)
{
FILE *file = fopen("dati.txt", "a ");
fprintf(file, "\n\tVards:%s Uzvards:%s Dzimsanas Datums:%d %d %d Ienakums %.2f EUR \n", vards, uzvards, *d, *m, *y, *ienakums);
fclose(file);
}
The code now looks like this:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void date (int *d, int *m, int *y);
void name (char vards[20], char uzvards[20]);
void income (float *ienakums);
void writefile(int *y,int *m, int *d, char vards[20],char uzvards[20],float *ienakums);
int main(void) {
char Name[20], last[20];
int diena,menesis,gads;
float cash;
// step 1 - get name check
name(Name, last);
// step 2 - get date check
date(&diena,&menesis,&gads);
// step 3 - get income check
income(&cash);
// step 4 - write the file
writefile(&gads,&menesis,&diena,Name,last,&cash);
}
void name(char vards[20], char uzvards[20]){
int check = 1;
int counter = 0;
do{
printf("Ievadiet jusu vardu\n");
gets(vards);
for(int i=0; i<strlen(vards); i )
{
if(isdigit(vards[i]) > 0)
{
counter ;
}
}
if(counter == 0)
{
check = 0;
}
else{
check = 1;
}
}while(check == 1);
check = 0;
do{
printf("Ievadiet jusu uzvardu\n");
gets(uzvards);
for(int i=0; i<strlen(vards); i )
{
if(isdigit(vards[i]) > 0)
{
counter ;
}
}
if(counter == 0)
{
check = 0;
}
else{
check = 1;
}
}while(check == 1);
printf("\nJusu vards ir %s %s",vards,uzvards);
}
void date (int *d, int *m, int *y){
*d = 0, *m = 0, *y =0;
do{
printf("\nLudzu ievadiet jusu dzimsanas dienu: ");
fflush(stdin);
scanf("%d", d);
}while((*d<=0) || (*d>31));
do{
printf("\nLudzu ievadiet jusu dzimsanas menesi: ");
fflush(stdin);
scanf("%d", m);
}while((*m<=0) || (*m>12));
do{
printf("\nLudzu ievadiet jusu dzimsanas gadu: ");
fflush(stdin);
scanf("%d", y);
}while((*y<1900) || (*y>2021));
printf("\n %d %d %d",d,m,y);
}
void income (float *ienakums){
//do{
printf("\nIevadiet videjos ienakumus: ");
fflush(stdin);
scanf("%f", ienakums);
//}while(isdigit(ienakums) <= 0);
printf("%f",ienakums);
}
void writefile(int *y,int *m, int *d, char vards[20],char uzvards[20],float *ienakums)
{
FILE *file = fopen("dati.txt", "a ");
fprintf(file, "\n\tVards:%s Uzvards:%s Dzimsanas Datums:%d %d %d Ienakums %.2f EUR \n", vards, uzvards, d, m, y, ienakums);
fclose(file);
}
CodePudding user response:
&Name[20]
is the address of element 20 of the array. But there is no element 20, the array indexes go from 0
to 19
. So the function is writing past the end of the array.
You should just pass the array itself, not the address of a particular element.
name(Name, last);
When an array is used as a function argument, it's automatically converted to the address of the first element; you don't need an explicit &
.
Also, don't use gets()
. It's a dangerous, obsolete function that has been removed from the language because you can't tell it the size of the input buffer. Use fgets()
and then remove the newline at the end. See Removing trailing newline character from fgets() input
CodePudding user response:
There are too many issues in your code that need to be addressed:
- Never use
gets()
. As already mentioned, it has been completely removed from the C standard. Use a combination offgets()
,strcspn()
andsscanf()
to read and parse your input. - Avoid using
scanf()
to read input too. It is very error-prone and shouldn't be used to read user input. Again, usefgets()
,strcspn()
andsscanf()
. fflush(stdin)
is undefined behaviour. Just remove that line from your code.- When you open a file with
fopen()
, always check for its return value. It could be that the file couldn't be read normally or doesn't exist (if the mode is"r"
).
To read one input line:
char *readline(char *line, int size, FILE *stream)
{
if (!fgets(line, size, stream)) // Check for errors
return NULL;
line[strcspn(line, "\n")] = '\0';
return line;
}
To read an integer/float:
int readint(int *i) // readfloat(float *f)
{
char line[255];
if (!fgets(line, sizeof line, stdin))
return 0;
line[strcspn(line, "\n")] = '\0';
return sscanf(line, "%d", i) == 1; // sscanf(line, "%f", f)
}
To check if your file has been correctly opened:
FILE *file = fopen("dati.txt", "a ");
if (!file) {
fprintf(stderr, "Could not open file\n");
return 1;
}
fprintf(file, "\n\tVards:%s Uzvards:%s Dzimsanas Datums:%d %d %d Ienakums %.2f EUR \n", vards, uzvards, *d, *m, *y, *ienakums);
fclose(file);