Need help Scanf problem

FedericoGB25

New Coder
Oct 10, 2019
5
0
2
Hello guys :)
I am new in this Forum and programming. I have a problem with my C "program":

I am trying to make a program where user can insert the current date. But at the final result, my output is this:
Code:
** Data corrente **                                                                                                   
                                                                                                                      
Type the current day:                                                                                         
5                                                                                                                     
                                                                                                                      
Type the current month:                                                                                 
10                                                                                                                   
                                                                                                                      
Type the current year:                                                                                           
2019                                                                                                                 
                                                                                                                      
Current date:                                                                                               
*Day: 5                                                                                                           
*Month: 4                                                                                                             
*Year: 2019
My code:
C:
#include <stdio.h>

int main() {
    int day, month, year;
  
    printf("** Current date **\n");
  
    printf("\nType the current day:\n");
    scanf("%d", &day);
    if(day>31) {
        printf("Invalid day");
        return 0;
    }

    printf("\nType the current month:\n");
    scanf("%d", &month);
    if(month>12) {
        printf("Invalid month");
        return 0;
    }
    if(month=2 && day>29) {
        printf("Invalid month");
        return 0;
    }
    if(month=4,6,9,11 && day>=31) {
        printf("Invalid month");
        return 0;
    }
  
    printf("\nType the current year\n");
    scanf("%d", &year);
  
    printf("\nCurrent date:\n *Day: %d\n *Month: %d\n *Year: %d\n", day, month, year);

}

Thanks for help :)
 
Last edited:

Crow

Active Coder
Sep 7, 2019
118
16
28
Hey there.

Right now, I'm currently in the process of trying to Debug this Code that you've shared with us. Unfortunately, I'm not getting any results so far that can consider everything to be fixed.

But in the meantime, look at this Line of Code:
if(mese>12)
Change mese to month. If you don't change that, you'll get this Error(GCC):
date.c: In function ‘main’:
date.c:17:8: error: ‘mese’ undeclared (first use in this function)
if(mese>12) {
That's the error I got when I first ran the Program. Changed mese to month and then I could run it.

Again, I'm not having any luck so far with Debugging it. But, I have had the idea of re-writing everything to see if that produces a result.

I'll update you soon!
 

FedericoGB25

New Coder
Oct 10, 2019
5
0
2
Hello :)
I am italian and I translated my code in english before posting this message. I forgot to translate the variable :laugh:
 

Crow

Active Coder
Sep 7, 2019
118
16
28
Ah, no worries. Make sure you do next time as a lot of us here are English and if a part of the Code isn't Translated then it can cause Errors when we try to run it.
 
1 comments

FedericoGB25

New Coder
Oct 10, 2019
5
0
2
The main code is in Italian language, the error is caused by condition (if), if I remove the conditions, the variables works well without changing :Rofl:
 
1 comments

Crow

Active Coder
Sep 7, 2019
118
16
28
Yes, I do understand that the Code was originally in Italian. But when writing an If-Statement, you do not remove the Conditions inside of it unless you're changing something(Most of the time, changing a Condition would mainly be to fix an Issue or fix Compatibility). You'd instead have to change all references and uses of the Variable inside the Code.

And if you could, please update your original Post to fix mese and Translate it to month so others don't avoid the exact same Error as I did. Thanks.
 

Crow

Active Coder
Sep 7, 2019
118
16
28
Hey there.

A bit of an update on what's going on with this.

So, I sort of managed to fix the Issue with the Code. It involved removing these two Blocks:
Code:
if(month=2 && day>29) { 
printf("Invalid month"); 
return 0; 
} 

if(month=4,6,9,11 && day>=31) { 
printf("Invalid month"); 
return 0; 
}


Removing those two Blocks stopped the Issue. But, it causes another Issue. Say, I assigned month the Value of 2 and date the Value of 30. The Program sees it as fine and then continues along. Thus' you no longer have the Logic that is used to stop something like that happening.

Feel free to test out removing those Blocks. Again, it gets rid of the Issue but you're removing important Logic that allows the Program to have an invalid Date on a Month that doesn't have anymore than 28/29 Days.

I'll update you again soon.
 
  • Like
Reactions: FedericoGB25

Crow

Active Coder
Sep 7, 2019
118
16
28
Hey there. Another Update this time.

But first of all, let me tell you something that has been happening with me recently(It's not dramatic and it's not depressing so don't think a Family Member of mine has died or something. Everybody is still alive and kicking, got it? Good, let me get started). So, recently, I've been looking at Programmer-Jobs and while I was looking at them, I only seemed to see Jobs that required C++ but not C. Because of that, I asked on Reddit whether I should learn the Language. Somebody said that I won't know if I like it until I try it. And as of today, I decided to give C++ a shot.

But what has this got to do with Federico's Problem, you may ask? Well, I mentioned that I would re-write the Program, didn't I? Since today was when I started learning C++. I thought to myself: "Why don't I try to re-write it using C++? Yeah, there won't be a lot of differences but it could still work out and maybe even give the desired results".

So, @FedericoGB25, here is your Program, re-written by somebody who has just started learning C++:

date.cpp:
// Version of date.c but written in C++ instead
// Original Version in C done by FedericoGB25, C++ Version by Dan-Kode
#include <iostream>

using namespace std;

int main(void) {
     int Day, Month, Year;

     std::cout << "** Current Date **\n";

     // Ask the User to insert the current Day
     std::cout << "Insert the current Day:\n";
     cin >> Day;

     // Determine if the inserted Value of Day was higher than 31
     if(Day > 31) {
      std::cout << "Invalid Day\n";
     }

     // Ask the User to insert the current Month
     std::cout << "Insert the current Month:\n";
     cin >> Month;
    
     // Determine if the inserted the Value of Month was higher than 12
     if(Month > 12) {
      std::cout << "Invalid Month";
     }

     // If the Value of Month was 2 but the Value of Day is over 29, tell the User it's Invalid
     if(Month == 2 && Day > 29) {
      std::cout << "Invalid Date.\n";
      // Terminate the Program after this
      return 0;
     }

     /* If the Value of Month is either
      * - 4
      * - 6
      * - 9
      * - 11
      * And the Value of Day is over 31, then tell the User it's Invalid
     */
     if(Month == 4 || Month == 6 || Month == 9 || Month == 11 & Day > 31) {
      std::cout << "Invalid Date.\n";
      // Terminate the Program after this
      return 0;
     }
    
     // Ask the User to insert the current Year
     std::cout << "Insert the current Year:\n";
     cin >> Year;

     // Print out all of the inserted Values
     std::cout << "Current Date: " << Day << "/" << Month << "/" << Year << "\n";
    
     return 0;
}
As you can see, the difference here is that:
  1. It's in C++.
  2. There is no printf()[For Printing out Text and Statements] and there is no scanf()[For getting Input from the User]. Instead, std::cout for Printing Statements and cin for getting Input.
  3. In the If-Statements, I have used the And(&&) and the Or(||) Operators. Something makes me believe this is why the original Program didn't work. @FedericoGB25 didn't use Logical-Operators in his If-Statements.
So, @FedericoGB25, give the C++ Version a run in your IDE/Compiler and tell me if you're happy with the result and if it works properly. I myself, am happy with the result and I have tested it a few times and everything seems to be working. I Compiled the Program using GCC/G++ and never got any errors at all when Compiling.

But, if you don't want it in C++, then that's okay. I'm happy to see if my changes done in the C++ Version, can apply to the C Version.
 

FedericoGB25

New Coder
Oct 10, 2019
5
0
2
Running in C++ works well :)
Thanks a lot for your time and suggestion about C++ language. I am starting to study C (C++) and PHP languages :)
 

Crow

Active Coder
Sep 7, 2019
118
16
28
Running in C++ works well :)
Good to hear that.

Thanks a lot for your time and suggestion about C++ language. I am starting to study C (C++) and PHP languages
I'm happy to have helped you. Besides, us Programmers, Software-Engineers, Hackers, Data-Scientists and Free-Software Activists all need to stick together and help each other, don't we?

I've been learning C for a year and it's definitely one of my favourite Languages. I originally intended to learn C++ and skip C but I'm glad I began with C. Besides, if I learn C first, I can transfer some of my knowledge over to C++ when I do get around to focusing on it.

I wish you the best of luck with future Learning and Careers.