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
Used the next seudoucode pseudocode and implement in the next code below run in c ++...
Used the next seudoucode pseudocode and implement in the next code below run in c ++ this code What is the output of the following pseudocode, where num1 , num2 , and num3 are integer variables? num1 = 5 num2 = 1 num3 = 4 aQueue.enqueue(num2) aQueue.enqueue(num3) aQueue.dequeue() aQueue.enqueue(num1 - num2) num1 = aQueue.peek() aQueue.dequeue() num2 = aQueue.peek() aQueue.dequeue() cout << num2 << " " << num1 << " " << num3 << endl ____________________________________________________________________________________ a// Created by Frank M....
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...
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
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>...
Hello, I feel like I am super close but I can not figure out why my...
Hello, I feel like I am super close but I can not figure out why my C++ code it not displaying the proper medium. Thank you! Here is an example of the output: Input : a[] = {1, 3, 4, 2, 6, 5, 8, 7} Output : Mean = 4.5 Median = 4.5 Code so far:   #include <iostream> using namespace std; int main() { int a[100]; int n,i,sum=0; float mean, medium; //read array size // read array cout<<"Enter array size:...
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...
Below is the problem that I have and here is the code that I have in...
Below is the problem that I have and here is the code that I have in c++. Can someone help me on what I am doing wrong or the correct code. A teacher has asked all her students to line up according to their first name. For example, in one class Amy will be at the front of the line, and Yolanda will be at the end. Write a program that prompts the user to enter the number of students...