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.
Instructions:
Given a birth year, month, and day, display the zodiac sign and print out a fortune. The fortunes must be read from a file called data.for.
The file data.for contains 60 lines. Every 12 lines represents a fortune for one of the 12 signs. The 12 zodiac signs are:
Aquarius Pisces Aries Taurus Gemini Cancer Leo Virgo Libra Scorpio Sagittarius Capricorn
This means the 1st, 13th, 25th, ..., line contains a fortune for Aquarius. Pisces' fortunes are in the 2nd, 14th, 26th, ..., line and so on.
Do not use the same fortune once given out, use the next fortune instead. For example, if you've already given out an Aquarius fortune the first time, use the 13th line instead.
Do not store these lines in a list. Instead, use file operations to jump around the file and get the line you want.
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.
And 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 d d",&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)&&((y0)!=0)||((y@0)==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-d-d",year,month,day);
fprintf(fp,"\n");
fclose(fp);
}
CodePudding user response:
I tried out your code and when I figured out I also needed to create a file known as "data.in" I was able to run your code. I could not replicate the issue when entering the year, month, and day you tried. But, having a look at your code, I would suggest a small tweak to the place where you scan in user input. Usually, it is helpful to provide textual information as a prompt prior to requesting the user to input a value. With that in mind, here is the revision to your "scanf" block of code.
//scanf("%d d d",&y, &m, &d);
printf("Please enter year of birth: ");
scanf("%d", &y);
printf("Please enter month of birth: ");
scanf("d", &m);
printf("Please enter day of birth: ");
scanf("d", &d);
When I ran the program with those revisions, here is what I saw on my terminal.
Please enter year of birth: 1955
Please enter month of birth: 11
Please enter day of birth: 4
Scorpio: Today is your lucky day
And here is the data that went into file "data.in".
1955-11-04
You might try out the "printf/scanf" combo and see if you still get an issue.
Regards.
CodePudding user response:
Everyone who is reading this is having a hard time finding out what is wrong with your code too.
Here is why -- you have not taken a modular approach to solving this problem.
Let me explain. First we need to step back an see what is needed to solve this problem
A way to read in the user input, parse it and understand what date and what sign that input represents
A way to (given that sign) read a fortune from the data file
A way to write out what fortune we sent to the user
A way to make sure that we don't give the same fortune twice
This list gives you a sense of parts of this program and also a plan to solve it. Solve the first first step first and make sure it is working. Then move on to the next step.
I built this list from experience so I know what steps to do first --- as you gain more programming experience knowing what modules to write first and what to have in them will become clear.
In your code I see as the 2nd line code that is trying to do step 4 while at the same time is clear (from the error messages) that you never solved step 1.
Start a new program and solve step 1. Then add in in step order each of the next steps. It might seem now like that is going backwards but I can assure you if you don't you will never be able to finish.