Question

can someone edit my c++ code where it will output to a file. I am currently...

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;

}

Homework Answers

Answer #1

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;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8);     //constructor     //Postcondition: noOfSolutions = 0; noOfQueens = queens;     // queensInRow is a pointer to the array     // that store the n-tuple.     // If no value is specified for the parameter queens,     // the default value, which is 8, is assigned to it. bool...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
I am trying to solve the following C++ problem over structures. I am having trouble with...
I am trying to solve the following C++ problem over structures. I am having trouble with the very last structure and I made a comment next to where I am confused. I included the assignment instructions and my current code. I would appreciate if anyone can help me explain what I need to do for the last structure. Thank you. Assignment Instructions Create a program that will prompt me for each of the following items: 1. Date of Birth 2....