Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 20-Jun-22 17:02pm
v3
Comments
OriginalGriff 20-Jun-22 12:32pm    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 20-Jun-22 12:47pm    
Interesting, this was previously posted by Jacklyn Nillama - Professional Profile[^], under the title "Can you check what's wrong with my code (C language)?", which has now been deleted. Probably didn't like my comment.
OriginalGriff 20-Jun-22 12:50pm    
Or two students found the same code on the internet and want it fixed to hand in ... :sigh:
Jarah Lourine 20-Jun-22 14:02pm    
Oh that was my other account. I've decided to use this account instead because I use this specifically for school purposes :)
Richard MacCutchan 21-Jun-22 3:35am    
Please delete the account that you are not using.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Quote:
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"
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C
int Double(int value)
   {
   return value * value;
   }


Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

So start with the debugger, and find out exactly what is going on with yoru code while it is running!
 
Share this answer
 
The compiler comments below code as follows:
"note: placeholders and their parameters expect 0 variadic arguments, but 1 variadic arguments were provided."
C
for (int i = 0; i<multiplier; i++) {
  fscanf(fp, "%*[^\n]\n", buff);
}

The function trans_cons_int(char* conste) is missing a return.

The data is read from data.in, but should be read from data.out?
The file is missing and so nor clear what happens.

The result is obviously appended to the data.in file again afterwards.
But at least the line break is missing here.

Same Question found here:
https://stackoverflow.com/questions/72690772/can-you-help-me-solve-the-problem-in-my-code (Jarah, New contributor; asked 14 mins ago)
 
Share this answer
 
Comments
Jarah Lourine 20-Jun-22 22:27pm    
Thank you. I revised my code and got the expected output ♡

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900