-6

This is a program to find age of a person .C++ is showing me an "expected" error in line 33. Error is present function definition of calculate(). Can you please help me fix it. I can't understand what is the error.

#include<iostream.h>
#include<conio.h>
struct date
{
    int day;
    int month;
    int year;
};
date birth;
date current;

void main()
{
    void calculate(int,int,int,int,int,int);
    cout<<"\nEnter your date of birth";
    cout<<"\nDay: ";
    cin>>birth.day;
    cout<<"\nMonth: ";
    cin>>birth.month;
    cout<<"\nYear: ";
    cin>>birth.year;
    cout<<"\nEnter current date";
    cout<<"\nDay: ";
    cin>>current.day;
    cout<<"\nMonth: ";
    cin>>current.month;
    cout<<"\nYear: ";
    cin>>current.year;
    calculate     (birth.day,birth.month,birth.year,current.day,current.month,current.year);
    getch();
}

// Error on line below
void calculate(int birth.day,int birth.month,int birth.year,int   current.day,int current.month,int current.year)
{
    int monthdays[]={31,28,31,30,31,30,31,31,30,31,30,31};
    if(birth.day>current.day)
    {
        current.day=current.day=monthdays[birth.month-1];
        current.month=current.month-1;
    }
    else if(birth.month>current.month)
    {
        current.year=current.year-1;
        current.month=current.month+12;
    }
    int calculated_date=current.date-birth.date;
    int calculated_month=current.month-birth.month;
    int calculated_year=current.year=birth.year;

    cout<<"Present age= "<<calculated_date<<calculated_month<<calculated_year;
}

There is error in (33,27)

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 4
    To give it what it is expecting... – Eugene Sh. Dec 19 '18 at 18:24
  • 3
    When asking about the error messages: Please copy-paste the whole message, without paraphrasing it. – Algirdas Preidžius Dec 19 '18 at 18:25
  • 1
    @Achal "_`date birth;` ? It should be `struct date birth;` or typedef the structure._" No it shouldn't. This is C++, not C. – Algirdas Preidžius Dec 19 '18 at 18:33
  • `#include` and `#include` mean this is likely an ancient prestandard `c++` implementation. – drescherjm Dec 19 '18 at 18:35
  • so how do I fix this – Ashmita Saha Dec 19 '18 at 18:36
  • 1
    This is a fundamental misunderstanding of how to pass structures into functions. I recommend reviewing your programming text or some other [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Dec 19 '18 at 18:37
  • @AlgirdasPreidžius True. I misread the tag. – Achal Dec 19 '18 at 18:38
  • 2
    To be brutally honest, @Achal the C++ variant in use here is closer to C than it is to Modern C++. – user4581301 Dec 19 '18 at 18:40
  • Semi-related: [Why doesn't a simple “Hello World”-style program compile with Turbo C++?](https://stackoverflow.com/questions/44863062/why-doesnt-a-simple-hello-world-style-program-compile-with-turbo-c) outlines some of the problems you will run into, Ashmita. The tools you are using are 30-or-so years out of date. – user4581301 Dec 19 '18 at 18:42
  • 2
    ... and they force you to use an operating system (i.e. DOS via DOSBox) that is 30-or-so years out of date, which is the bigger problem. – Lightness Races in Orbit Dec 19 '18 at 18:43

3 Answers3

2

In C++ one cannot pass parameters as member variables of a class. In

void calculate(int birth.day, ...

birth.day is not valid.

One can, however, pass the whole class and then use the member variables.

Change

void calculate(int,int,int,int,int,int);

into

void calculate(date, date); 

and then

calculate       (birth.day,birth.month,birth.year,current.day,current.month,current.year);

into

calculate(birth, current);

and finally

void calculate(int birth.day,int birth.month,int birth.year,int   current.day,int current.month,int current.year)

into

void calculate(date birth, date current)

There are number of ways to improve upon this, passing by reference

void calculate(const date & birth, date current) 

(Note that current is not a reference because it will be modified in the function) and cleaning up a couple typos in calculate

current.day=current.day=monthdays[birth.month-1]; 

should probably be

current.day=current.day+monthdays[birth.month-1];

or

current.day+=monthdays[birth.month-1];

and

int calculated_date=current.date-birth.date;

should be more like

int calculated_day=current.day-birth.day;

The compiler will catch the second typo, but probably not the first. I'm also not sold on the logic used in calculate, but fortunately TurboC++ came with Turbo Debugger, one of the best debuggers available at the time and, in my opinion, it still holds up well.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • I take the risk to up vote, you used more time than me to explain :) I hope the very silent Ashmita Saha will read and all of that wasn't for nothing. I do not understand the down votes in a lot of cases, for me it would be better if an explanation was mandatory and it was not anonymous ... – bruno Dec 19 '18 at 20:05
  • @bruno all of the downvotage seems to have been cleared up, but yeah. It was looking toxic in here for a while. – user4581301 Dec 19 '18 at 23:15
  • @user4581301 Downvotes are not "toxic", they are a core part of the quality control mechanisms here and shouldn't be taken personally. – Lightness Races in Orbit Dec 19 '18 at 23:21
  • Toxic was a poor word choice, @LightnessRacesinOrbit but anytime I hesitate about making an answer because I'm worried it'll be instantly downvoted, it's something. – user4581301 Dec 19 '18 at 23:29
  • @user4581301 If you're so unsure of your answer that you think it'll be instantly downvoted, then it's probably best not to post it - the system works! :) – Lightness Races in Orbit Dec 19 '18 at 23:35
  • @LightnessRacesinOrbit I was sure of the answer. Not so sure of the people. People not always logical. – user4581301 Dec 19 '18 at 23:39
  • @user4581301 Then, considering this answer has no downvotes and some upvotes, I suggest casting fewer aspersions against "the people" and assume good faith!! – Lightness Races in Orbit Dec 19 '18 at 23:40
  • @LightnessRacesinOrbit answer's here, isn't it? Faith in humanity won out. – user4581301 Dec 19 '18 at 23:41
  • @user4581301 Yep! Good times! – Lightness Races in Orbit Dec 19 '18 at 23:43
0

probably the error is from the line void calculate(int birth.day,int birth.month,int birth.year,int current.day,int current.month,int current.year)

just replace the '.' by '_' or something like including in its body

[edit]

Except that point I encourage you modify your function to receive in parameter only the birth date and the current date, it is useless and extract their fields while this can be made by the function itself.

Warning because you modify current into, you have to receive it current by value while you can receive birth by a const reference. Of course you can also use a local variable rather than to modify current ...

bruno
  • 32,421
  • 7
  • 25
  • 37
  • True but misses the crux of it, which is that the OP seems to have misunderstood how variables work when passed to a function. Not only is the name within the function different from the name at the callsite, so it can be spelt differently, but it certainly _can't_ take the form of an arbitrary expression like `birth.month`. Your solution is correct though, just needs a bit more explanation. – Lightness Races in Orbit Dec 19 '18 at 18:45
  • 1
    If you're going to pass in all six items individually, what's the point of declaring a structure to hold the three parts of a date? The structure should be passed to the function, not its members – Tim Randall Dec 19 '18 at 18:47
  • @LightnessRacesinOrbit I don't undertand why needs more explanation, is just the same syntax error several times, the guy supposed it is valid to have a dot in a name, that's all, no ? – bruno Dec 19 '18 at 18:47
  • @TimRandall absolutely, I did not understood myself why :) – bruno Dec 19 '18 at 18:47
  • @bruno: I'm saying I don't think the OP just randomly decided to name variables with dots in them, it looks more like they think the parameters _have_ to look like the way the function is called, which is (a) not true, and (b) not possible here -- for the best answer I think you should dispel this misunderstanding. – Lightness Races in Orbit Dec 19 '18 at 18:48
  • @bruno I think that a good, useful answer is one that sees past the immediate problem the OP is having, and guides them along the path to better coding. I realize it can be difficult to do so without being patronizing, but I think it's worth a try – Tim Randall Dec 19 '18 at 18:49
  • @LightnessRacesinOrbit I think also the error is coming from the use of the struct, when I written the answer I started to say he reused a call of the function to make the function definition, but after I see the body of the function also having the same problem. Perhaps the function was added after and he just copy paste the body from the main code – bruno Dec 19 '18 at 18:50
  • @TimRandall the problem he having is 'syntax error', my answer about it, no ? If not I am lost :) – bruno Dec 19 '18 at 18:53
  • Just because your answer isn't wrong doesn't mean it can't be even better ;) – Lightness Races in Orbit Dec 19 '18 at 18:56
  • ok I added more material, hope it is good because I am late to diner and I go :))) – bruno Dec 19 '18 at 19:07
0

While it's hard to tell what the error means without seeing the error, and hard to correlate its line number with the posted code given the formatting, the likeliest cause of the error is that '.' is not a valid character for an identifier, so all the parameter names in the definition of the calculate function are invalid identifiers. This could lead to an error message containing the word "expected" (e.g. "expected an identifier).

Consider using '_' or camelCase for your parameter names instead, if you must pass them all individually. However, you have declared this convenient date structure to pass around, so you can just have your function take parameters date birth and date current instead of each member of each of the two date instances.

manveti
  • 1,691
  • 2
  • 13
  • 16