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: 3696752 • 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've been told the easiest way to read the .csv files is to use getline() for each line in the file and then use stringstream to parse the line. If you could use these methods that would be great. Thanks in advanced! LINK TO CVS FILE(on my google drive) - https://drive.google.com/folderview?id=0ByJ78XQLvHamcC1qZTZIdFBCUmM&usp=sharing

Explanation / Answer

#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;
   cout << "Enter file name: ";
   cin >> fName;
   ifstream in(fName.c_str());
   if(in.is_open()){
       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;
}

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