The problem I'm having is converting my time into a time_t object that gmtime needs. In my code below I have time being read into a string 'timestamp', I need to get this to print out, I thought maybe if I typecasted to a long int I could get this to work, but that didn't seem to do anything.
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <time.h>
#include <sstream>
using std::strtod;
using namespace std;
double stringtodouble(char*);
main(){
double runningbalance;
double lastmonthbalance; // to see if debt has been paid
string logline; // line of transaction logs
ifstream logfile("creditcardlog.txt");
if (logfile.fail()) {
cout << "Could not open creditcardlog.txt";
exit(1);
} else if (logfile.is_open()) {
if (logfile.good()){
/* first logline contains user input options: payment day, credit limit, apr, late fee, overcharge fee */
getline(logfile, logline);
}
cout << logline << endl;
while (logfile.good()) {
getline(logfile, logline);
cout << logline << endl;
/* [0] : Transaction type: p-payment(+), c-charge(-), i-interest fee(-),
o-overcharge fee(-), l-late fee(-) */
char transactiontype; // get first char in logline,
/* [1]-[10] : timestamp */
string timestamp;
timestamp.append(logline, 1, 10);
long int time = atol(timestamp);
time_t tim;
tim = static_cast<time_t>(time);
struct tm *timeinfo;
timeinfo = timestamp(&tim);
int y = timeinfo->tm_year+1900;
int m = timeinfo->tm_mon;
int d = timeinfo->tm_mday;
int h = timeinfo->tm_hour;
int min = timeinfo->tm_min;
int sec = timeinfo->tm_sec;
cout << asctime(timeinfo);
cout << m << "/" << d << "/" << y << endl;
// cout << timestamp << endl; //debug only
/* convert timestamp into readable format */
/* [11] - [20] : amount - in pennies - 0000000000 - 4294967295 */
char strchar[100];
string amountstr;
double amount;
amountstr.append(logline, 11, 10);
strcpy(strchar, amountstr.c_str());
amount = stringtodouble(strchar);
/* [21] - [49] : payee info - string */
string payee;
payee.append(logline, 21, 5);
if (transactiontype == 'p') {
runningbalance += amount;
} else if (transactiontype == 'c' ||
transactiontype == 'i' ||
transactiontype == 'o' ||
transactiontype == 'l') {
runningbalance -= amount;
} else { //error
}
cout << timestamp << " \t " << amount << " \t " << payee << " \t " << runningbalance << endl;
logfile.close();
}
}
return 0;
}
double stringtodouble(char* strinput) {
char* str = strinput;
double d;
const char *string1 = str;
char *stringPtr;
d = strtod(string1, &stringPtr);
return d;
}
These are a few lines stored in creditcardlog.txt that I created for test purposes.
1212345678911.23412.34123.4
c13428415150123456789Entry one
p13429819190123456789Entry two
~
This post has been edited by Atli: 01 August 2012 - 09:17 PM
Reason for edit:: Use [code] tags when posting code.

New Topic/Question
Reply



MultiQuote




|