Hi. I am having a hard time figuring out what's wrong in my code. After inputting, for example, 2000 02 10, the output must be "Aquarius: (and any phrase in the text file)". However, after running the code, the output is just
"Invalid month
Invalid month
Invalid day
Invalid month
Invalid day"
Can you help me please? Here is the instruction given to us.
The file data.for contains 60 lines. Every 12 lines represents a fortune for one of the 12 signs.
Additionally, write the date inputs of the user to a file called data.in. One line per entry. This allows us to track which fortunes have already been given out.
data.in should have the following format: YYYY-mm-dd.
Here is the code I've made:
#include <stdio.h>
#include <string.h>
#define m_size 12
#define max_line 200
int leapnt(int y);
void grab_f(int cons_num, char* receive, int* mem);
char* conste_grab(int cons);
char* zodiac(int y, int m, int d);
void memory_up(int cons_num, int* mem_arr);
int trans_cons_int(char* conste);
void read_mem(int* mem_arr);
void save(int year, int month, int day);
int main(){
int d,m,y;
int mem[m_size];
for(int i=0;i<m_size;i++){mem[i]=0;}
int cons_num;
char* conste;
char fortune[160];
read_mem(mem);
scanf("%d %02d %02d",&y, &m, &d);
conste = zodiac(y,m,d);
cons_num = trans_cons_int(conste);
grab_f(cons_num, fortune, mem);
printf("%s: %s", conste, fortune);
save(y,m,d);
return 0;
}
int leapnt(int y){
return(((y%4)==0)&&((y%100)!=0)||((y%400)==0));
}
void grab_f(int cons_num, char* receive, int* mem){
char *error = "File opening error";
char fortune[max_line];
char buff[max_line];
int multiplier;
multiplier = cons_num+(12 * mem[cons_num]);
FILE* fp = fopen("data.for", "r");
if(fp == NULL){
printf("\nerror!\n\n");return;
}
for(int i=0; i<multiplier;i++){
fscanf(fp, "%*[^\n]\n", buff);
}
fscanf(fp, "%[^\n]", fortune);
strcpy(receive, fortune);
fclose(fp);
}
char* conste_grab(int cons){
char* starsn[] = {"Aquarius","Pisces","Aries","Taurus","Gemini", "Cancer","Leo","Virgo","Libra","Scorpio", "Saggitarius","Capricorn"};
return starsn[cons-1];
}
char* zodiac(int y, int m, int d){
char* error = "constellation error";
int max[12] = {31,(leapnt(y)?29:28),31,30,31,30,31,31,30,31,30,31};
if(d < 1 || d > max[m-1]){
printf("\nInvalid day"); return error;
}
if(m < 1 || m > 12){
printf("\nInvalid month");
return error;
}
char* conste;
switch(m){
case 1:
if(d > 19 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(12);
}
break;
case 2:
if(d > 18 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(m-1);
}
break;
case 3:
if(d > 20 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(m-1);
}
break;
case 4:
if(d > 19 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(m-1);
}
break;
case 5:
if(d > 20 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(m-1);
}
break;
case 6:
if(d > 21 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(m-1);
}
break;
case 7:
if(d > 22 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(m-1);
}
break;
case 8:
if(d > 22 && d < max[m-1])
{ conste = conste_grab(m); }
else
{ conste = conste_grab(m-1);}
break;
case 9:
if(d > 22 && d < max[m-1])
{ conste = conste_grab(m); }
else
{ conste = conste_grab(m-1);}
break;
case 10:
if(d > 23 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(m-1);
}
break;
case 11:
if(d > 21 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(m-1);
}
break;
case 12:
if(d > 21 && d < max[m-1]){
conste = conste_grab(m);
}
else{
conste = conste_grab(m-1);
}
break;
default: return error;
}
return conste;
}
void memory_up(int cons_num, int* mem_arr){
mem_arr[cons_num]++;
}
int trans_cons_int(char* conste){
int cons_num;
char* starsn[] = {"Aquarius","Pisces","Aries","Taurus","Gemini", "Cancer","Leo","Virgo","Libra","Scorpio", "Saggitarius","Capricorn"};
for(int i=0; i<m_size; i++){
if(strcmp(starsn[i], conste)==0){
cons_num=i;
return cons_num;
}
}
}
void read_mem(int* mem_arr){
int year, month, day;
char* conste;
int cons_num;
FILE* fp = fopen("data.in", "r");
if(fp == NULL){
printf("\n\tfopen error!\n");
}
while(fscanf(fp,"%d-%d-%d", &year, &month, &day) != EOF){
conste = zodiac(year,month,day);
cons_num = trans_cons_int(conste);
memory_up(cons_num, mem_arr);
}
fclose(fp);
}
void save(int year, int month, int day){
FILE* fp = fopen("data.in", "ab");
if(!fp){
printf("\n\tFile open error\n");
return;
}
fprintf(fp,"%d-%02d-%02d",year,month,day);
fprintf(fp,"\n");
fclose(fp);
}
What I have tried:
I have googled and watched youtube video tutorials for a solution but I still can't solve it.