Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m fairly new to C++ and just need some help writing code. I\'m using some dat

ID: 3697309 • Letter: I

Question

I'm fairly new to C++ and just need some help writing code. I'm using some data from twitter stored in a .cvs file that I will include a link. The program has to be able to extract data from the .cvs file and display the amount of tweets that were tweeted during the first 12 hours, 24 hours, 1 week, and 2 weeks after the start of the data(it was data collected from a fire). I have the code written (I think) but I just need someone to put the functions into a class so I can review their code to see how it works.

My Code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>

using namespace std;

void parseTime(string tweet, tm *t){
   string temp;
   int row, day, month, year, hour, minute;
   stringstream ss(tweet);
   getline(ss, temp, ',');
   row = atoi(temp.c_str());
   getline(ss, temp, '/');
   month = atoi(temp.c_str());
   getline(ss, temp, '/');
   day = atoi(temp.c_str());
   getline(ss, temp, ',');
   year = atoi(temp.c_str());
   getline(ss, temp, ',');
   hour = atoi(temp.c_str());
   getline(ss, temp, ',');
   minute = atoi(temp.c_str());

   t->tm_mon = month;
   t->tm_mday = day;
   t->tm_year = year - 1900;
   t->tm_hour = hour;
   t->tm_min = minute;
   t->tm_sec = 0;
}

int main(){
   string fName, line;
   vector<tm *> tweets;
   double hours_12 = 12 * 60 * 60, hours_24 = 2 * hours_12, week_1 = 7 * hours_24, week_2 = 2 * week_1;
ifstream infile;
infile.open("allTweets.cvs");
if (infile.good())
{


       getline(in, line);
       while(getline(in, line)){
           tm *t = new tm;
           parseTime(line, t);
           tweets.push_back(t);
       }
       int count = 0;
       for(int i = 0; i < tweets.size(); ++i){
           if(difftime(mktime(tweets[i]), mktime(tweets[0])) <= hours_12){
               ++count;
           }
           else{
               break;
           }
       }
       cout << count << " tweets in 12 hours" << endl;
       count = 0;
       for(int i = 0; i < tweets.size(); ++i){
           if(difftime(mktime(tweets[i]), mktime(tweets[0])) <= hours_24){
               ++count;
           }
           else{
               break;
           }
       }
       cout << count << " tweets in 24 hours" << endl;
       count = 0;
       for(int i = 0; i < tweets.size(); ++i){
           if(difftime(mktime(tweets[i]), mktime(tweets[0])) <= week_1){
               ++count;
           }
           else{
               break;
           }
       }
       cout << count << " tweets in 1 week" << endl;
       count = 0;
       for(int i = 0; i < tweets.size(); ++i){
           if(difftime(mktime(tweets[i]), mktime(tweets[0])) <= week_2){
               ++count;
           }
           else{
               break;
           }
       }
       cout << count << " tweets in 2 weeks" << endl;
       in.close();
   }
   else{
       cout << "can not open " << fName << endl;
   }
   return 0;
}

Explanation / Answer

Try the below code changes made, I think it may help you to an extent.

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

class CalculateTweets
{
public:
       string temp;
       int row, day, month, year, hour, minute;
       double hours_12, hours_24, week_1, week_2;

// CONSTRUCTOR
CalculateTweets()
   {
           hours_12 = 12 * 60 * 60;
           hours_24 = 2 * hours_12;
           week_1 = 7 * hours_24;
           week_2 = 2 * week_1;
   }

   void parseTime(string tweet, tm *t)
   {
   stringstream ss(tweet);
   getline(ss, temp, ',');

   row = atoi(temp.c_str());
   getline(ss, temp, '/');
     
   month = atoi(temp.c_str());
   getline(ss, temp, '/');
     
   day = atoi(temp.c_str());
   getline(ss, temp, ',');
     
   year = atoi(temp.c_str());
   getline(ss, temp, ',');
     
   hour = atoi(temp.c_str());
   getline(ss, temp, ',');
     
   minute = atoi(temp.c_str());
     
   t->tm_mon = month;
   t->tm_mday = day;
   t->tm_year = year - 1900;
   t->tm_hour = hour;
   t->tm_min = minute;
   t->tm_sec = 0;
   }

   void countTweets(vector<tm *> tweets)
   {
       int count = 0;

       // COUNT TWEETS WITHIN 12 HRS
       for(int i = 0; i < tweets.size(); ++i){
if(difftime(mktime(tweets[i]), mktime(tweets[0])) <= hours_12){
++count;
}
else{
break;
}
}
   cout << count << " tweets in 12 hours" << endl;

       // COUNT TWEETS WITHIN 24 HRS
count = 0;
for(int i = 0; i < tweets.size(); ++i){
if(difftime(mktime(tweets[i]), mktime(tweets[0])) <= hours_24){
++count;
}
else{
break;
}
}
cout << count << " tweets in 24 hours" << endl;

       // COUNT TWEETS IN 1 WEEK
count = 0;
for(int i = 0; i < tweets.size(); ++i){
if(difftime(mktime(tweets[i]), mktime(tweets[0])) <= week_1){
++count;
}
else{
break;
}
}
       cout << count << " tweets in 1 week" << endl;

       // COUNT TWEETS IN 2 WEEKS
       count = 0;
for(int i = 0; i < tweets.size(); ++i){
if(difftime(mktime(tweets[i]), mktime(tweets[0])) <= week_2){
++count;
}
else{
break;
}
}
cout << count << " tweets in 2 weeks" << endl;


   }


};


int main(){

string fName, line;
vector<tm *> tweets;

// CONSTRUCTOR AUTOMATICALLY CALLED HERE DURING CLASS INSTANCE CREATION
CalculateTweets calc_tweet;


ifstream infile;
infile.open("allTweets.cvs");

if (infile.good())
{

       getline(infile, line);
       while(getline(infile, line)){
tm *t = new tm;
calc_tweet.parseTime(line, t);
tweets.push_back(t);
       }

       // COUNT THE TWEETS
       calc_tweet.countTweets(tweets);

       infile.close();
   }
   else{
cout << "can not open " << fName << endl;
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote