1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <locale>
#include <string>
#include <sstream>
#include <time.h>
using namespace std;
string get_date();
int main(){
string date,out;
string name,id,age,x,y;
int no=0,store=0,a=0,b=0;
float num1,num2,num3;
date=get_date();
out=".csv";
out=date+out;
ifstream indata;
ofstream outdata;
indata.open(out.c_str());
if(!(indata.is_open()))//if the file cannot be opened,Enter name & personal details
{
outdata.open(out.c_str(), ios::app);
cout<<"enter name:";
cin>>name;
cout<<"enter id:";
cin>>id;
cout<<"Enter age:";
cin>>age;
//outputting all the data into a csv file
outdata<<"Number Test\n"<<endl;
outdata<<"Name:"<<","<<name<<endl;
outdata<<"Id:"<<","<<id<<endl;
outdata<<"Age:"<<","<<age<<endl;
outdata<<"\n"<< endl;
outdata << "No,Num1,Num2,Product" << endl;
while (!(num1 ==-11))
{
cout<<"Enter Num1:";
cin>>num1;
if(num1==-11)
{
indata.open(out.c_str());
string cell1;
indata >> cell1;
break;
}
cout<<"Enter Num2:";
cin>>num2;
num3=num1*num2;
no++;
outdata<<no<<","<<num1<<","<<num2<<","<<num3<< endl;
}
}
else //if the file can be opened,continue prompting the user for numbers
{
while(!indata.eof())
{
indata >> x;
a++;
}
no=a-7;
outdata.open(out.c_str(), ios::app);
while (!(num1 ==-11))
{
cout<<"Enter Num1:";
cin>>num1;
if(num1==-11)break;
cout<<"Enter Num2:";
cin>>num2;
num3=num1*num2;
no++;
outdata<<no<<","<<num1<<","<<num2<<","<<num3<< endl;
indata.open(out.c_str());
}
}
system("pause");
return 0;
}
string get_date()//function to 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", gmtime(&now));
}
return string(the_date);
}
|