Question: I need help with my code and how to fix it. It is showing me this: 1>------ Rebuild All started: Project: Practice, Configuration: Debug Win32

I need help with my code and how to fix it. It is showing me this:

1>------ Rebuild All started: Project: Practice, Configuration: Debug Win32 ------ 1> Practice.cpp 1>c:\users\owner\desktop\practice\practice.cpp(91): error C2131: expression did not evaluate to a constant 1> c:\users\owner\desktop\practice\practice.cpp(91): note: failure was caused by non-constant arguments or reference to a non-constant symbol 1> c:\users\owner\desktop\practice\practice.cpp(91): note: see usage of 'line_count'

#include

#include

#include

#include

using namespace std;

struct person

{

char name[20];

int dd;

int mm;

int yy;

};

int counter()

{

char str[255];

ifstream file_ptr("C:/Users/temp-01968/Desktop/person.txt");

int line_count = 0;

if (!file_ptr)

{

cout << "Cannot open input file. ";

return 0;

}

while (file_ptr.getline(str, 255))

{

line_count++;

}

file_ptr.close();

return line_count;

}

int get_age(struct person obj[], int i)

{

int base_yy = 2018;

int base_mm = 3;

int base_dd = 3;

int age = 0;

age = base_yy - (obj[i].yy);

if (!(obj[i].mm >= base_mm&&obj[i].dd >= base_mm))

{

age--;

}

return age;

}

int getdata(struct person obj[])

{

char str_buff[255], *token;

char date[11];

int i = 0;

ifstream file_ptr("C:/Users/temp-01968/Desktop/person.txt");

if (!file_ptr)

{

cout << "Cannot open input file. ";

return 0;

}

while (file_ptr.getline(str_buff, 255))

{

token = strtok(str_buff, ",");

strcpy(obj[i].name, token);

token = strtok(NULL, ",");

strcpy(date, token);

token = strtok(date, "/");

obj[i].mm = atoi(token);

token = strtok(NULL, "/");

obj[i].dd = atoi(token);

token = strtok(NULL, "/");

obj[i].yy = atoi(token);

i++;

}

file_ptr.close();

return 1;

}

void print(struct person obj[], int line_count)

{

int i = 0;

while (i

{

cout << "Name:" << obj[i].name << "\t";

cout << "Birthday:" << obj[i].dd << "/" << obj[i].mm << "/" << obj[i].yy << "\t";

cout << "Age:" << get_age(obj, i) << endl;

i++;

}

}

int main()

{

int line_count = 0;

line_count = counter();

if (line_count>0)

{

struct person obj[line_count];

if (getdata(obj))

{

cout << "NAME\t\tDate Of Birth\t\tAge ";

cout << "_______________________________________________________________________________ ";

print(obj, line_count);

}

else

{

cout << " \tData not fetched..!";

}

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!