C++ programming C++ programming ------------------------------------------------
ID: 3802531 • Letter: C
Question
C++ programming
C++ programming
-----------------------------------------------------------
Write a program that opens a file of the users choice that contains a list of birthdays. Extract from this file two things: (1) the date with the most common birthday (all of them) and (2) the month with the most people born. We will not test for a tie in either of these statistics. The file tested will always be in the format: mm/dd/yyyy
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample birthday files:
bday1.txt
-------------------------------------------------------------------------------------------------------------------------------------------
bday2.txt
------------------------------------------------------------------------------------------------------------------
Example 1(user input is Italic):
Enter file name:
bday1.txt
Most common birthday:
1/1
Most common birthday month: 3
Example 2(user input is underlined):
Enter file name:
bday2.txt
Most common birthday:
8/15
Most common birthday month:
8
------------------------------------------------------
C++ programming
C++ programming
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
using namespace std;
int main() {
std::map<string,int> first;
std::map<string,int> dayMonth;
string input = "";
// How to get a string/sentence with spaces
cout << "Please enter a valid sentence (with spaces): >";
getline(cin, input);
string STRING;
ifstream infile;
infile.open (input.c_str());
int a=0;
string previousLine="";
while(!infile.eof()) // To get you all the lines.
{
getline(infile,STRING); // Saves the line in STRING.
if (STRING != previousLine)
{
previousLine=STRING;
string month,daymonth;
std::istringstream is(STRING);
char delimiter;
int d,m,y;
if (is >> m >> delimiter >> d >> delimiter >> y) {
stringstream ss;
ss << m;
month = ss.str();
stringstream sd;
sd << d << delimiter << m;
daymonth = sd.str();
}
int value = 1;
if (first.count(month)>0){
value = first.find(month)->second;
value = value+1;
first[month] = value;
}else{
first[month] = value;
}
int dvalue = 1;
if (dayMonth.count(daymonth)>0){
dvalue = dayMonth.find(daymonth)->second;
dvalue = dvalue+1;
dayMonth[daymonth] = dvalue;
}else{
dayMonth[daymonth] = dvalue;
}
}
}
infile.close();
map<string,int>::const_iterator it;
string keyMax="";
int valMax=0;
for (it = first.begin(); it!= first.end(); it++){
string key = it->first;
int val = it->second;
if(val > valMax){
keyMax = key;
valMax = val;
}
}
map<string,int>::const_iterator its;
string keyMaxs="";
int valMaxs=0;
for (its = dayMonth.begin(); its!= dayMonth.end(); its++){
string key = its->first;
int val = its->second;
if(val > valMaxs){
keyMaxs = key;
valMaxs = val;
}
}
cout<<"Most common birthday :"<<endl<<keyMaxs<<endl;
cout<<"Most common birthday month:"<<keyMax<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.