Click here to Skip to main content
15,891,784 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My assignment is to create a program that would read a number N from a textfile and create multiplication table up to N. Each multiplication table is to be written into separate a separate file. The program would essentially create N number for textfiles.
For example Table1.txt would contain
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
Table2.txt would include
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
and so on

So far my code looks like this and will only print all the multiplication tables into one textfile.
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	//initializations
	int input, product;
	ifstream inputstream;
	ofstream outputstream;
	inputstream.open("input.txt");
	outputstream.open("output.txt");
	
	//Inputting the number from the text file.
	inputstream >> input;
	
	//Creating the multiplication tables
	for (int counter = 1; counter <= input; counter++)
	{	
		for (int counter2 = 1; counter2 <= input; counter2++)
		{
			product = counter * counter2;
			outputstream << counter << " x " << counter2 << " = " << product << endl; 
		}
		outputstream << "==========================================" << endl << "==========================================" << endl;
	}
	return 0;
}


How should I change my code to get what the assignment is asking for? My intial guess was to put the output.open("output.txt") into the loop but when I did that I was only able to get one text file which had the 1's times table even though the number in my input file was 10.
for (int counter = 1; counter <= input; counter++)
{
    outputstream.open("output.txt");
    for (int counter2 = 1; counter2 <= input; counter2++)
    {
        product = counter * counter2;
        outputstream << counter << " x " << counter2 << " = " << product << endl;
    }
    outputstream << "==========================================" << endl << "==========================================" << endl;
}
Posted
Updated 20-Feb-15 10:55am
v5
Comments
Afzaal Ahmad Zeeshan 20-Feb-15 14:37pm    
Do you want to open the streams to these files, or do you want to write these files into values or so on?

Also, always add the code of your program into the question post. Do not share the image link to your code, experts don't like it. I am not an expert that is why I gave it a try.
Edward Lamb 20-Feb-15 15:36pm    
I just want to create and write onto these files.
enhzflep 20-Feb-15 14:44pm    
First of all, hello and welcome to CP.
Next of all, please don't post links to images of text. Just post the text in the question. You can edit your question to include it.
I have an answer, but wont reward the question as it stands by submitting it.
Edward Lamb 20-Feb-15 15:36pm    
Sorry about the link, I changed it.
enhzflep 20-Feb-15 15:45pm    
No problem. Links can go bad over time, which renders the question forever useless.
Text in images (a) takes up _much_ more space and (b) can't be copy/pasted.

1 solution

C++
#include <iostream>
#include <sstream>

using namespace std;
int main(int argc, char *argv[])
{
    ostringstream filename;
    for (int i=0; i<10; i++)
    {
        filename.str("");
        filename << "Table" << i << ".txt";
        cout << filename.str() << endl;
    }

    return 0;
}


Output
Table0.txt
Table1.txt
Table2.txt
Table3.txt
Table4.txt
Table5.txt
Table6.txt
Table7.txt
Table8.txt
Table9.txt
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900