can anyone there help me about my problem in displaying a record of names,gender,annual salary and age..the inputs are the first name,lastname,gender,birthday and monthly income...i just have a hard time implementing it coz i have to use a string to deal with the name..how will i make it???just a hint....

Dani AI

Generated

As recommended, break the task into parsing, computing derived fields, and formatting output. Since confirmed the data comes from a file, the practical work is: choose a simple, consistent file format (recommended: one record per line, fields delimited and birthdate in YYYY-MM-DD), parse each line with std::getline, validate fields, compute age and annual salary, then print with fixed formatting.

Keep parsing robust: read each input line with std::getline, then split into fields with a std::istringstream or a small CSV-safe parser. Use std::getline(fieldStream, name) to capture names with spaces. Parse monthly income with std::stod inside a try/catch and normalize gender strings (uppercase first char or map to an enum). Using a clear date format like YYYY-MM-DD makes parsing trivial and unambiguous.

A compact, reliable way to compute age and annual salary in C++ (works with C++11+) — parse YYYY-MM-DD into integers, get the current local date, then subtract years and adjust if the birthday hasn’t happened yet this year:

#include <string>
#include <sstream>
#include <chrono>
#include <ctime>
#include <iostream>
#include <iomanip>

bool parse_ymd(const std::string &s, int &y, int &m, int &d) {
  char sep1, sep2;
  std::istringstream iss(s);
  return (iss >> y >> sep1 >> m >> sep2 >> d) && sep1=='-' && sep2=='-';
}

int calc_age(int by, int bm, int bd) {
  auto now = std::chrono::system_clock::now();
  std::time_t t = std::chrono::system_clock::to_time_t(now);
  std::tm tm = *std::localtime(&t);
  int cy = tm.tm_year + 1900, cm = tm.tm_mon + 1, cd = tm.tm_mday;
  int age = cy - by;
  if (cm < bm || (cm == bm && cd < bd)) --age;
  return age;
}

// annual = monthly * 12.0; print with std::fixed << std::setprecision(2)

Watch edge cases: Feb 29 birthdays, negative or zero income, malformed dates, and thread-safety of std::localtime (use localtime_r/localtime_s in multi-threaded code). Test with a dozen records including boundary dates and odd names to ensure parsing and formatting meet your requirements.

Recommended Answers

All 2 Replies

can anyone there help me about my problem in displaying a record of names,gender,annual salary and age..the inputs are the first name,lastname,gender,birthday and monthly income...i just have a hard time implementing it coz i have to use a string to deal with the name..how will i make it???just a hint....

Split the problem into the different sub-problems. I would create a struct called "person" first. Then I'd probably create a vector of type person to store the data. Then I'd tackle the issue of getting the data from wherever it is and storing it in the person vector, then I'd display it. Read in the data one person at a time and push it back into the vector. You're going to have to get a lot more specific if you want more specific advice because there are a million questions. Where is the data? Is it in a file? Does the user enter it? How are you supposed to display it? Etcetera, etcetera.

hi..uhm..i have solved the problem and i used file to handle the data.thanks anyway....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.