Thale13
Thale13

Reputation: 23

Issue in outputting into a .csv file using a c++ program

My program will prompt user to input numbers and the program will calculate the multiplication of the numbers. I want to store my data into a csv file in this format ""00:01AM/02/05/14.csv""

This is to ensure that I can store my data into a new .csv file instead of the existing one each and every time I terminate my program.But my program is not outputting into a csv file due to some error that I could not fix.Im not sure what is causing this problem. Here is my code :

#include <iostream>
#include <fstream>
#include <iomanip>
#include <locale>       
#include <string>        
#include <sstream> 
#include <time.h>
using namespace std;

string get_time();
string get_date();

int main()
{   //I can compile the codes but it is not outputting into a csv file 
    string date,time,out; 
    float a,b,c;
    ifstream indata;
    ofstream outdata;

    date=get_date();
    time=get_time();
    time=time+'/';
    out=time+date;//combines data&time into 12:01AM/02/05/14.csv string form

    cout<<out<<endl;

    //outputs the data into a csv file--but it is not working   
    outdata.open(out.c_str());
    outdata << "Num1,Num2,Answer" << endl;

    while (!(a ==-100))
   {    
    cout<<"Enter Num1:";
    cin>>a;
    if(a==-100)break;
    cout<<"Enter Num2:";
    cin>>b;
    c=a*b;
    outdata<<a<<","<<b<<","<<c<< endl;
   }    

    system("pause");
    return 0;
}

 string get_date()//converts date to string
{
   time_t now;
   char the_date[15];

   the_date[0] = '\0';

   now = time(NULL);

   if (now != -1)
   {
      strftime(the_date,15, "%d/%m/%y.csv", gmtime(&now));
   }

   return string(the_date);
}

string get_time()//converts time to stirng
{
   time_t currtime;
   struct tm * timeinfo;
   char the_time [12];

   time (&currtime);
   timeinfo = localtime (&currtime);

   strftime (the_time,12,"%I:%M%p",timeinfo);
   return string(the_time);
}   

Upvotes: 0

Views: 232

Answers (1)

Zan Lynx
Zan Lynx

Reputation: 54325

Did you know that slash is a character that you cannot use in filenames? Slash aka '/' is the directory separator.

Even on Windows, because Windows tries to be at least a little bit compatible with Unix.

Also, this is a good lesson for you. Always check for error codes. You should have a check to see if your outdata.open succeeded.

Upvotes: 3

Related Questions