Hi, Im new to C++ coding and need some help on a project. I have example code i\
ID: 3696841 • Letter: H
Question
Hi, Im new to C++ coding and need some help on a project. I have example code i'll provide, and if you could code this so I can see how its done that would be awesome. I'm using some data from twitter stored in a .cvs file that I will include in 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.
LINK TO CVS FILE(on my google drive) - https://drive.google.com/folderview?id=0ByJ78XQLvHamcC1qZTZIdFBCUmM&usp=sharing
main.cpp:
#include // Standard In/Out to terminal (cin/cout)
#include // File stream (ifstream/ofstream)
#include "TwitterUser.h"
#include "TwitterUsers.h"
/* Version 6: Code separated into separate class and header files
*/
using namespace std;
int main()
{
// Step 1: Read in the data
string data;
TwitterUsers userRecords;
// declare and open the input file.
ifstream infile;
infile.open("twitterers.csv");
if (infile.good())
{
cout << "Reading in data..." << endl;
// Read in the headings and ignore.
getline(infile, data);
bool good;
// Get lines from infile until the end-of-file character is reached (EOF)
while (getline(infile,data))
{
good = userRecords.addTwitterUser(TwitterUser(data));
if (!good)
break;
}
}
// Step 2: Welcome message
cout << "Welcome. This program prints out the most active Twitterers from the " << endl;
cout << "2014 Carlton Complex Wildfire. You can select top tweeters overall or " << endl;
cout << "by user classification (e.g. Media or Official Organizations)." << endl;
// Step 3: Menu loop
int menuOption = 0;
while (menuOption != 7)
{
// Display the menu
cout << endl;
cout << "PROGRAM MENU" << endl;
cout << "--------------------------" << endl;
cout << "1) Top Tweeters Overall" << endl;
cout << "2) Top Official Sources" << endl;
cout << "3) Top EM/Fire Tweeters" << endl;
cout << "4) Top Media Sources" << endl;
cout << "5) Top Businesses & Organizations" << endl;
cout << "6) Top Individual Tweeters" << endl;
cout << "7) QUIT" << endl;
cout << "Enter the desired option (7 to quit): ";
cin >> menuOption;
if (menuOption == 1)
{
userRecords.printN(10);
}
else if (menuOption == 2)
{
userRecords.printNbyClass(10,"official");
}
else if (menuOption == 3)
{
userRecords.printNbyClass(10,"em/fire tweeter");
}
else if (menuOption == 4)
{
userRecords.printNbyClass(10,"media");
}
else if (menuOption == 5)
{
userRecords.printNbyClass(10,"business");
}
else if (menuOption == 6)
{
userRecords.printNbyClass(10,"individual");
}
}
// Step 4: Final Message
cout << "Here is where you need to explain the easiest/hardest, most interesting/enjoyable,"< cout << "least interest/enjoyable elements of this project." << endl;
return 0;
}
TwitterUser.cpp:
#include "TwitterUser.h"
#include
#include
using namespace std;
TwitterUser::TwitterUser(string s)
{
// Parse s and put value into correct private fields for this instance
stringstream datastream(s); // create a stringstream from the current line of data
getline(datastream,screenName,','); // parse characters up to first comma into screenName
getline(datastream,userClass,','); // parse characters up to the second comma into userClass
string idstr;
getline(datastream,idstr,','); // parse characters up to the third comma into idstr
stringstream idss(idstr); // create a stringstream from idstr in char form
idss>>userId; // direct contents of idss into the variable userID to convert it to int value
getline(datastream, userLink, ',');
string tweetsstr;
getline(datastream,tweetsstr); // get the remainder of the data into tweetsstr
stringstream numss(tweetsstr); // create a stringstream from tweetsstr that contains numTweets in char form
numss>>numTweets;
}
TwitterUser::TwitterUser()
{
}
void TwitterUser::print()
{
// print partial results (name, class, #tweets)
cout << "Twitter User: " << screenName << " User Class: " << userClass << " NumTweets: " << numTweets << endl;
}
string TwitterUser::getUserClass()
{
return userClass;
}
TwitterUser.h:
#ifndef TWITTERUSER_H
#define TWITTERUSER_H
#include
class TwitterUser
{
private:
std::string screenName;
std::string userClass;
int userId;
std::string userLink;
int numTweets;
public:
TwitterUser(std::string);
TwitterUser();
void print();
std::string getUserClass();
};
#endif // TWITTERUSER_H
TwitterUsers.cpp:
#include
#include "TwitterUser.h"
#include "TwitterUsers.h"
using namespace std;
TwitterUsers::TwitterUsers()
{
numTwitterUsers = 0;
}
TwitterUsers::~TwitterUsers()
{
//dtor
}
bool TwitterUsers::addTwitterUser(TwitterUser user)
{
if (numTwitterUsers >= 8000)
{
cout << "Array limit reached." << endl;
return false; // failure
}
collection[numTwitterUsers] = user; // add the new record
numTwitterUsers++; // increment the count
return true; // success
}
void TwitterUsers::print()
{
for (int i=0; i < numTwitterUsers; i++)
{
collection[i].print(); // call TwitterUser print method
}
}
void TwitterUsers::printN(int n)
{
if (n > numTwitterUsers)
{
cout << "Setting maximum to " << numTwitterUsers << endl;
n = numTwitterUsers;
}
cout << endl << "TOP " << n << " TWEETERS:" << endl;
for (int i=0; i < n; i++)
{
collection[i].print();
}
cout << endl;
}
void TwitterUsers::printNbyClass(int n, string userClass)
{
if (n > numTwitterUsers)
{
cout << "Setting maximum to " << numTwitterUsers << endl;
n = numTwitterUsers;
}
int currN = 0;
cout << endl << "Top " << n << " " << " for class: " << userClass << endl;
for (int i=0; (i {
string currClass = collection[i].getUserClass();
if (currClass.find(userClass) == 0)
{
collection[i].print();
currN++;
}
}
}
TwitterUsers.h:
#ifndef TWITTERUSERS_H
#define TWITTERUSERS_H
#include
class TwitterUsers
{
public:
TwitterUsers();
~TwitterUsers();
bool addTwitterUser(TwitterUser);
void print();
void printN(int);
void printNbyClass(int,std::string);
protected:
private:
TwitterUser collection[8000];
int numTwitterUsers;
};
#endif // TWITTERUSERS_H
(Sorry it's so long, thanks in advanced!)
Explanation / Answer
#include #include #include std::string month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; std::string day[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; int main() { time_t timer; tm * time; const int BASE_YEAR = 1900; std::time(&timer); time = localtime(&timer); std::coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.