can someone edit my c++ code where it will output to a file. I am currently using xcode.
#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
bool inputNum(int [],int&,istream&);
void multiply(int[],int,int[],int,int[],int&);
void print(int[],int,int,int);
int main()
{ifstream input;
int num1[35],num2[35],len1,len2,num3[60],len3=10,i;
input.open("multiplyV2.txt"); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it\n";
system("pause");
return 1;
}
while(inputNum(num1,len1,input))
{inputNum(num2,len2,input);
multiply(num1,len1,num2,len2,num3,len3);
print(num1,len1,len3,1);
print(num2,len2,len3,2);
for(i=0;i<len3;i++)
cout<<"-";
cout<<endl;
print(num3,len3,len3,1);
//cout<<len1<<" "<<len2<<" "<<len3<<endl;
cout<<endl;
}
system("pause");
}
void print(int num[],int len, int maxlen,int n)
{int i,count;
count=maxlen-len;
if(n==2)
count--;
for(i=0;i<count;i++)
cout<<" ";
if(n==2)
cout<<"x";
for(i=len-1;i>=0;i--)
cout<<num[i];
cout<<endl;
}
void multiply(int num1[],int len1,int num2[],int len2,int num3[],int& len3)
{int i,j,tmp ;
for(i=0;i<60; i++)
num3[i]=0;
for(i=0;i<len2;i++)
{ len3=i;
for(j=0;j<len1;j++)
{tmp=num1[j]*num2[i];
num3[len3]=num3[len3]+tmp;
num3[len3+1]=num3[len3+1]+num3[len3]/10;
num3[len3]%=10;
len3++;
}
}
if(num3[len3]!=0)
len3++;
}
bool inputNum(int integer[],int& len,istream& file)
{string in;
int j,k;
for (len=0;len<35;len++ )
integer[len]=0;
len=35-1;
file>>in;
if(!file)
return false;
for(len=0;in[len]!='\0';len++);
k=0;
for(j=len-1;j>=0;j--)
integer[j]=in[k++]-48;
return true;
}
Check it out I've edited your program now all the output generated from this program will be saved in a file named OutputFile.txt.
There is not much task in performing this task all you need to do is
ofstream myfile; /*your file myfile.open ("example.txt"); here the file will open and after this statement every output in the program will be written in this file. */
myfile.close(); // When the task is done and your program is about to return please close the output file.
Your program starts from here
#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
bool inputNum(int [],int&,istream&);
void multiply(int[],int,int[],int,int[],int&);
void print(int[],int,int,int);
int main()
{
ofstream myfile; myfile.open ("OutputFile.txt");
ifstream input;
int num1[35],num2[35],len1,len2,num3[60],len3=10,i;
input.open("multiplyV2.txt"); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it\n";
system("pause");
return 1;
}
while(inputNum(num1,len1,input))
{inputNum(num2,len2,input);
multiply(num1,len1,num2,len2,num3,len3);
print(num1,len1,len3,1);
print(num2,len2,len3,2);
for(i=0;i<len3;i++)
cout<<"-";
cout<<endl;
print(num3,len3,len3,1);
//cout<<len1<<" "<<len2<<" "<<len3<<endl;
cout<<endl;
}
system("pause");
}
void print(int num[],int len, int maxlen,int n)
{int i,count;
count=maxlen-len;
if(n==2)
count--;
for(i=0;i<count;i++)
cout<<" ";
if(n==2)
cout<<"x";
for(i=len-1;i>=0;i--)
cout<<num[i];
cout<<endl;
}
void multiply(int num1[],int len1,int num2[],int len2,int num3[],int& len3)
{int i,j,tmp ;
for(i=0;i<60; i++)
num3[i]=0;
for(i=0;i<len2;i++)
{ len3=i;
for(j=0;j<len1;j++)
{tmp=num1[j]*num2[i];
num3[len3]=num3[len3]+tmp;
num3[len3+1]=num3[len3+1]+num3[len3]/10;
num3[len3]%=10;
len3++;
}
}
if(num3[len3]!=0)
len3++;
}
bool inputNum(int integer[],int& len,istream& file)
{string in;
int j,k;
for (len=0;len<35;len++ )
integer[len]=0;
len=35-1;
file>>in;
if(!file)
return false;
for(len=0;in[len]!='\0';len++);
k=0;
for(j=len-1;j>=0;j--)
integer[j]=in[k++]-48;
myfile.close();
return true;
}
Get Answers For Free
Most questions answered within 1 hours.