I found this example from here: https://www.tutorialspoint.com/reading-and-writing-binary-file-in-c-cplusplus
I copy that and put into my Visual Studio 2019. The result is fine but I cannot exit the program with Enter. I have to press ESC wait for it before the program exit itself with "exited with code -1073741819"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct Student {
int roll_no;
string name;
};
int main() {
ofstream wf("student.dat", ios::out | ios::binary);
if (!wf) {
cout << "Cannot open file!" << endl;
return 1;
}
Student wstu[3];
wstu[0].roll_no = 1;
wstu[0].name = "Ram";
wstu[1].roll_no = 2;
wstu[1].name = "Shyam";
wstu[2].roll_no = 3;
wstu[2].name = "Madhu";
for (int i = 0; i < 3; i++)
wf.write((char*)&wstu[i], sizeof(Student));
wf.close();
if (!wf.good()) {
cout << "Error occurred at writing time!" << endl;
return 1;
}
ifstream rf("student.dat", ios::out | ios::binary);
if (!rf) {
cout << "Cannot open file!" << endl;
return 1;
}
Student rstu[3];
for (int i = 0; i < 3; i++)
rf.read((char*)&rstu[i], sizeof(Student));
rf.close();
if (!rf.good()) {
cout << "Error occurred at reading time!" << endl;
return 1;
}
cout << "Student's Details:" << endl;
for (int i = 0; i < 3; i++) {
cout << "Roll No: " << wstu[i].roll_no << endl;
cout << "Name: " << wstu[i].name << endl;
cout << endl;
}
return 0;
}
Above comment are really appreciable and must be considered.
One another method would be defining name as character pointer like:
char *name;
I'm trying to output some arrays to a file "output.txt". I run this code with no errors but no output file seems to be created to the working directory
ofstream myfile("output.txt");
if (myfile.is_open()) {
myfile << "t: " << endl;
for (int i = 0; i < range; i++) {
myfile << t[i] << " ";
}
cout << endl;
myfile << "x_1: " << endl;
for (int i = 0; i < range; i++) {
myfile << x_1[i] << " ";
}
cout << endl;
myfile << "y_1: " << endl;
for (int i = 0; i < range; i++) {
myfile << y_1[i] << " ";
}
cout << endl;
myfile << "x_2: " << endl;
for (int i = 0; i < range; i++) {
myfile << x_2[i] << " ";
}
cout << endl;
myfile << "y_2: " << endl;
for (int i = 0; i < range; i++) {
myfile << y_2[i] << " ";
}
cout << endl;
myfile.close();
}
else cout << "Unable to open file";
What's the problem?
I want to display array using a method, if the array has under 200 elements it display all the elements, which works fine for me. The problem is if the array has over 200 elements i want to display the first 100 elements and the last 100 elements of an array. It works if I use an array of 500 elements or even 10000, but I type something like 9999 or 8999 I get long negative integer numbers on the bottom half of my display list but the top half half works. Any advice?
int main()
{
string fileName,text, size;
fstream inText;
int lengthOf = 0;
cout << "Please Enter An Input File Name: ";
getline(cin, fileName);
inText.open(fileName.c_str() , fstream::in);
if(!inText)
{
cout << "Could Not Open " << fileName << " File" << endl;
exit(EXIT_FAILURE);
}
else
{
inText >> lengthOf;
int * myArray = new int[lengthOf];
for(int i = 0; i < lengthOf; i++)
{
inText >> myArray[i];
}
cout << "Data File Array " << endl;
displayArray(myArray,lengthOf);
}
return 0;
}
void displayArray (int a[], int s)
{
if(s <= 200)
{
for (int i = 0; i < s; ++i)
{
if(i%10 == 0)
{
cout << endl;
}
cout << setw(6) << a[i] << " ";
}
cout << endl;
}
else
{
for(int i = 0; i < 100; i++)
{
if(i%10 == 0)
{
cout << endl;
}
cout << setw(6) << a[i] << " ";
}
cout << endl;
for (int i = s-100; i < s; ++i)
{
if (i%10 == 0)
{
cout << endl;
}
cout << setw(6) << a[i] << " ";
}
cout << endl;
}
}
Printing the array is straight forward, example:
int main()
{
int a[551]; //some random number
int s = 551;
for (int i = 0; i < s; ++i) a[i] = i;
for (int i = 0; i < s; ++i)
{
if (i % 10 == 0) cout << "\n";
if (i % 100 == 0) cout << "\n";
cout << std::setw(6) << a[i] << " ";
}
return 0;
}
When reading the file you can use std::vector to store the integers, this way you don't have to know how big the array should be before hand. The example below reads text and then tries to convert to integer, this way you know if there was an error in input file.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
std::string fileName;
cout << "Please Enter An Input File Name: ";
getline(cin, fileName);
std::ifstream inText(fileName);
std::vector<int> vec;
std::string temp;
while (inText >> temp)
{
try {
int i = std::stoi(temp);
vec.push_back(i);
}
catch (...) {
cout << temp << " - error reading integer\n";
}
}
for (size_t i = 0; i < vec.size(); ++i)
{
if (i % 10 == 0) cout << "\n";
if (i % 100 == 0) cout << "\n";
cout << std::setw(6) << vec[i] << " ";
}
return 0;
}
Are You sure the problem is with this method? The code seems to work, I can't reproduce Your problem. Maybe You could paste Your whole code (if its not too big)?
This code is supposed to display a menu with 6 options. create new file, display numbers, total, and average, display a sort, search for a num and tell yow how many occurrences it had, append random numbers, and display largest.
It runs and does MOST of what it is supposed to do but I just CANNOT get the searchNum function to search for the number entered.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <iomanip>
using namespace std;
//Function Prototypes
void menu();
string createFile();
void displayNumTotalAverage(string myFileName);
void displaySortedNums(string myFileName);
void searchNum(string myFileName);
void displayLargestNum(string myFileName);
void appendRandomNum(string myFileName);
int main()
{
int choice = 0;
string myFileName = "";
do
{
cout << "** MENU **" << endl << endl;
cout << "Current Data File: ";
cout << fixed << setprecision(2) << showpoint;
menu();
cin >> choice;
while (choice < 1 || choice > 7)
{
cout << "Menu Choice: ";
cin >> choice;
cout << endl;
}
switch (choice)
{
case 1:
myFileName = createFile();
break;
case 2:
displayNumTotalAverage(myFileName);
break;
case 3:
displaySortedNums(myFileName);
break;
case 4:
searchNum(myFileName);
break;
case 5:
displayLargestNum(myFileName);
break;
case 6:
appendRandomNum(myFileName);
break;
}
} while (choice != 7);
system("PAUSE");
return 0;
}
void menu()
{
cout << "\n\n(1) Select / create data file(.txt file extension will be added automatically)\n"
<< "(2) Display all numbers, total, and average\n(3) Display all numbers sorted\n(4) "
<< "Search for a number and display how many times it occurs\n(5) Display the largest number\n"
<< "(6) Append a random number(s)\n(7) Exit the program\n\nMenu Choice:";
}
string createFile()
{
string myFileName;
ifstream inFile;
cout << "\nName of data file: ";
cin >> myFileName;
inFile.open(myFileName);
if (inFile)
{
cout << myFileName;
}
else
{
cout << "\nFile not found, creating file.\n\n";
ofstream outFile;
outFile.open(myFileName + ".txt");
}
system("PAUSE");
return myFileName;
}
void displayNumTotalAverage(string myFileName)
{
ifstream inFile;
const int SIZE = 50;
int num[SIZE];
int count = 0;
int total = 0;
double average = 0;
inFile.open(myFileName + ".txt");
while (!inFile)
cout << "\nData File is empty" << endl << endl;
while (count < SIZE && inFile >> num[count])
count++;
inFile.close();
for (int index = 0; index < count; index++)
{
cout << num[index] << endl;
total += num[index];
}
average = (float)total / count;
cout << endl << "Total : " << total << endl << endl;
cout << "Average: " << average << endl << endl;
cout << "File Successfully Read" << endl << endl;
system("PAUSE");
return;
}
void displaySortedNums(string myFileName)
{
ifstream inFile;
inFile.open(myFileName + ".txt");
if(inFile.good())
{
const int SIZE = 50;
int num[SIZE]; int counter = 0;
while (counter < SIZE && inFile >> num[counter])
counter++;
inFile.close();
for(int idx1 = 0; idx1 < counter; ++idx1)
for(int idx2 = idx1; idx2 < counter; ++idx2)
if(num[idx1] > num[idx2])
{
int tmp = num[idx1];
num[idx1] = num[idx2];
num[idx2] = tmp;
}
for (int i = 0; i < counter; i++)
{
cout << num[i] << endl;
}
cout << endl;
}
system("PAUSE");
return;
}
void searchNum(string myFileName)
{
ifstream inFile;
inFile.open(myFileName + "txt");
const int SIZE = 50;
int num[SIZE];
bool found = false;
int position = -1;
int index = 0;
int userNum = 0;
int counter = 0;
int numCount = 0;
cout << "Search Number: ";
cin >> userNum;
cout << endl << endl;
while (index < SIZE && !found)
{
if (num[index] == userNum)
{
found = true;
position = index;
numCount++;
}
index++;
}
cout << userNum << " occurs " << numCount << " times ";
cout << "File Successfully Read\n\n";
system("PAUSE");
return;
}
void displayLargestNum(string myFileName)
{
ifstream inFile;
inFile.open(myFileName + ".txt");
const int SIZE = 50;
int nums[SIZE];
int count = 0;
int highest;
while (count < SIZE && inFile >> nums[count])
count++;
highest = nums[0];
for (int i = 0; i < SIZE; i++)
{
if (nums[i] > highest)
highest = nums[count];
}
cout << "\nLargest Number: " << highest << endl << "File Successfully Read" << endl << endl;
}
void appendRandomNum(string myFileName)
{
cout << "i am in the appendRandomNum function - option 6" << endl;
int num = 0;
int count = 0;
ofstream outFile;
outFile.open(myFileName + ".txt", ios::app);
cout << "How many random numbers: ";
cin >> count;
for (int i = 0; i < count; i++)
outFile << rand() % 10 << endl;
outFile.close();
cout << endl << "Number(s) Added" << endl << endl;
system("PAUSE");
return;
}
Can anyone please help with this?
I can see 2 problems in searchNum function:
The file is opened but num array is not populated with the file content
numCount will always return either 0 or 1. Remove 'found' boolean variable altogether and it will then return the correct numCount value.
Here are some important rules they never seem to teach in programming courses: start with something small and simple that works, add complexity a little at a time, and develop new functionality in isolation.
Suppose you have the rest of the code working perfectly, and you can produce a file called nums.txt that looks like this:
7
9
3
8
0
2
4
8
3
9
Now you want to develop and test the searchNum function. So you write a main function like this:
int main()
{
string myFileName = "nums";
searchNum(myFileName);
system("PAUSE");
return 0;
}
Then a searchNum function:
void searchNum(string myFileName)
{
}
You compile and run this, it does nothing, so far so good.
Now have it open the file, read the first number and display it:
void searchNum(string myFileName)
{
ifstream inFile;
inFile.open(myFileName + "txt");
int num;
inFile >> num;
cout << num << endl;
}
So far so good. Now iterate through the whole file:
void searchNum(string myFileName)
{
ifstream inFile;
inFile.open(myFileName + "txt");
int num;
while(inFile >> num)
{
cout << num << endl;
}
}
So far so good. Now count the 8's:
void searchNum(string myFileName)
{
ifstream inFile;
inFile.open(myFileName + "txt");
int num;
int count = 0;
while(inFile >> num)
{
cout << num << endl;
if(num == 8)
++count;
}
}
You get the idea. Using this approach you'll get clean working code, and you'll get it faster than by trying to write it all and then fix all the bugs.
A couple of things I noticed also in searchNum
inFile.open(myFileName + "txt"); should be inFile.open(myFileName + ".txt");, a dot is missing from the suffix.
Read the rest of the answers and get acquainted with the debugger it will save you a lot of time and frustration.
Here is your code sample with the needed corrections. Keep up the good work and invest some time for the use of a debugger.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <iomanip>
using namespace std;
//Function Prototypes
void menu();
string createFile();
void displayNumTotalAverage(string myFileName);
void displaySortedNums(string myFileName);
void searchNum(string myFileName);
void displayLargestNum(string myFileName);
void appendRandomNum(string myFileName);
int main()
{
int choice = 0;
string myFileName = "";
do
{
cout << "** MENU **" << endl << endl;
cout << "Current Data File: " << myFileName << (!myFileName.empty() ? ".txt" : "");
cout << fixed << setprecision(2) << showpoint;
menu();
cin >> choice;
while (choice < 1 || choice > 7)
{
cout << "Menu Choice: ";
cin >> choice;
cout << endl;
}
switch (choice)
{
case 1:
myFileName = createFile();
break;
case 2:
displayNumTotalAverage(myFileName);
break;
case 3:
displaySortedNums(myFileName);
break;
case 4:
searchNum(myFileName);
break;
case 5:
displayLargestNum(myFileName);
break;
case 6:
appendRandomNum(myFileName);
break;
}
} while (choice != 7);
cin.get();
return 0;
}
void menu()
{
cout << "\n\n(1) Select / create data file(.txt file extension will be added automatically)\n"
<< "(2) Display all numbers, total, and average\n(3) Display all numbers sorted\n(4) "
<< "Search for a number and display how many times it occurs\n(5) Display the largest number\n"
<< "(6) Append a random number(s)\n(7) Exit the program\n\nMenu Choice:";
}
string createFile()
{
string myFileName;
ifstream inFile;
cout << "\nName of data file: ";
cin >> myFileName;
inFile.open(myFileName + ".txt", std::ifstream::in);
if (inFile)
{
cout << myFileName;
}
else
{
cout << "\nFile not found, creating file.\n\n";
ofstream outFile;
outFile.open(myFileName + ".txt");
outFile.close();
}
cin.get();
return myFileName;
}
void displayNumTotalAverage(string myFileName)
{
ifstream inFile;
const int SIZE = 50;
int num[SIZE];
int count = 0;
int total = 0;
double average = 0;
inFile.open(myFileName + ".txt");
while (!inFile)
cout << "\nData File is empty" << endl << endl;
while (count < SIZE && inFile >> num[count])
count++;
inFile.close();
for (int index = 0; index < count; index++)
{
cout << num[index] << endl;
total += num[index];
}
average = (float)total / count;
cout << endl << "Total : " << total << endl << endl;
cout << "Average: " << average << endl << endl;
cout << "File Successfully Read" << endl << endl;
cin.get();
return;
}
void displaySortedNums(string myFileName)
{
ifstream inFile;
inFile.open(myFileName + ".txt");
if(inFile.good())
{
const int SIZE = 50;
int num[SIZE]; int counter = 0;
while (counter < SIZE && inFile >> num[counter])
counter++;
inFile.close();
for(int idx1 = 0; idx1 < counter; ++idx1)
for(int idx2 = idx1; idx2 < counter; ++idx2)
if(num[idx1] > num[idx2])
{
int tmp = num[idx1];
num[idx1] = num[idx2];
num[idx2] = tmp;
}
for (int i = 0; i < counter; i++)
{
cout << num[i] << endl;
}
cout << endl;
}
cin.get();
return;
}
void searchNum(string myFileName)
{
ifstream inFile;
inFile.open(myFileName + ".txt");
const int SIZE = 50;
int num[SIZE];
int position = -1;
int index = 0;
int userNum = 0;
int counter = 0;
int numCount = 0;
cout << "Search Number: ";
cin >> userNum;
cout << endl << endl;
// Fill num array with inFile numbers.
while (counter < SIZE && inFile >> num[counter++]);
while (index < SIZE)
{
if (num[index] == userNum)
{
position = index;
numCount++;
}
index++;
}
cout << userNum << " occurs " << numCount << " times ";
cout << "File Successfully Read\n\n";
cin.get();
return;
}
void displayLargestNum(string myFileName)
{
ifstream inFile;
inFile.open(myFileName + ".txt");
const int SIZE = 50;
int nums[SIZE];
int count = 0;
int highest;
while (count < SIZE && inFile >> nums[count])
count++;
highest = nums[0];
for (int i = 0; i < SIZE; i++)
{
if (nums[i] > highest)
highest = nums[count];
}
cout << "\nLargest Number: " << highest << endl << "File Successfully Read" << endl << endl;
}
void appendRandomNum(string myFileName)
{
cout << "i am in the appendRandomNum function - option 6" << endl;
int count = 0;
ofstream outFile;
outFile.open(myFileName + ".txt", ios::app);
cout << "How many random numbers: ";
cin >> count;
for (int i = 0; i < count; i++)
outFile << rand() % 10 << endl;
outFile.close();
cout << endl << "Number(s) Added" << endl << endl;
cin.get();
return;
}
so my problem is that i have to read from a file that looks like this
input_M1
1,2
9,5
4,1
input_M2
3,2,6,1
4,1,7,8
of course I have to omit the "input_1" and "input_1", and i was able to do that and separate each matrix by itself in a string like this :
1,2
9,5
4,1
and this :
3,2,6.1
4,1,7,8
I was trying to make a dynamic array, i got the rows, with this
while(getline(ss,str1)){
row++;
}
and it prints the number of rows. However, when I do that for the columns :
while(getline(ss,str1,',')){
colmn++;
}
and when I print it out nothing appears.
and here is my whole code :
ifstream inFile;
inFile.open("c:\\Games\\crap.txt");
if (inFile.is_open()){
cout << "File successfully opened" << endl;
}
else{
cout << "Error opening file" << endl;
}
string sMain,sCutOut,firstMatrix,secondMatrix;
int counter = 1;
while(getline(inFile,sMain)){
sCutOut+=(sMain+'\n');
}
//cout << sCutOut << endl;
sCutOut = sCutOut.substr( sCutOut.find("1")+1,sCutOut.length() );
//cout << sCutOut << endl;
firstMatrix = sCutOut.substr( 0,sCutOut.find("input_M2") );
//cout << firstMatrix << endl;
secondMatrix = sCutOut.substr( sCutOut.find("_")+3,sCutOut.length() );
//cout << secondMatrix << endl;
istringstream ss (firstMatrix);
istringstream sn (secondMatrix);
string str1,str2,str3;
int row=0,colmn=0;
while(getline(ss,str1,'\n')){
//cout << str1 << '\n';
row++;
//cout << row << " ";
}
while(getline(ss,str2,',')){
cout << str2 << '\n';
colmn++;
cout << colmn << " ";
}
also, i get this when I try to print out the (firstMatrix) with out the new line I get this :
1,29,54,1
I used C++11 Regular expression library and I applied a simple regular expression to strip off the input_M? lines.
// under g++ 4.8.1 compile:
// $ g++ apply_regex.cpp -std=c++11 -lboost_regex -o apply_regex
#include <fstream>
#include <iostream>
// #include <regex> not implemented yet in g++ 4.8.1 (c++11 standard)
#include <boost/regex.hpp> // using boost libraries
using namespace std;
using boost::regex;
using boost::regex_replace;
int main() {
ifstream inFile;
inFile.open("crap.txt");
if (inFile.is_open()) {
cout << "File successfully opened" << endl;
}
else {
cout << "Error opening file" << endl;
}
string sMain, sCutOut;
while(getline(inFile,sMain)) {
sCutOut+=(sMain+'\n');
}
inFile.close();
cout << "Read lines from crap.txt" << endl;
cout << sCutOut;
// regular expression checks from input_M1 or input_M2 etc..
regex txt_regex("input_M[1-9]+");
string result = regex_replace(sCutOut, txt_regex, "");
cout << "After applying regular expression \"input_M[1-9]+\":" << endl;
cout << result;
return 0;
}
The result of the above code is:
$ ./apply_regex
File successfully opened
Read lines from crap.txt
input_M1
1,2
9,5
4,1
input_M2
3,2,6,1
4,1,7,8
After applying regular expression "input_M[1-9]+":
1,2
9,5
4,1
3,2,6,1
4,1,7,8
Update : i made the matrix into one line and then made them delimited with commas and that is it !
#include<iostream>
#include<fstream>
#include<string>
#include<string.h>
#include<sstream>
#include<vector>
#include<string>
#include<stdlib.h> using namespace std;
int main() {
cout << "Hello! today i'm gonna help you to multiply 2 matrices." << endl;
cout << "But first, we need to discuss the format of the file" << endl;
cout << "help me with the format so i can help you to multiply !" << endl;
cout << "this is how i like the format of the file to be : " << endl;
cout << "--------------------------------" << endl;
cout << "input_M1" << endl;
cout << "number,number," << endl;
cout << "number,number" << endl;
cout << "input_M2" << endl;
cout << "number,number," << endl;
cout << "number,number," << endl;
cout << "--------------------------------" << endl;
cout << "make sure that you put the comma at the end of each line to indicate the end of the line of the matrices [not the input_M1 or input_M2], Have fun !" << endl << endl;
string fileDirct = ""; string fileName = "";
cout << "Now, Enter the file directory, with 2 backslashs, i.e : 'C:\\\\program files'" << endl; cin >> fileDirct; cout << "and Enter the file name, followed by the file type, i.e : 'testData.txt'" << endl; cin >> fileName;
string file = fileDirct+"\\"+fileName; fileDirct = fileDirct.c_str(); ifstream infile;
infile.open(file.c_str());
//ifstream infile( "c:\\Games\\crap.txt");
string sLiner,sStoring,firstMatrix,secondMatrix,sTemp1,sTemp2;
while(infile){
getline( infile, sLiner );
sStoring+=(sLiner+'\n');
}
//cout << sStoring;
sStoring = sStoring.substr( sStoring.find("_")+3,sStoring.length() );
//cout << sStoring;
firstMatrix = sStoring.substr(0,sStoring.find("i"));
//cout << firstMatrix;
secondMatrix = sStoring.substr(sStoring.find("_")+3,sStoring.length());
//cout << secondMatrix;
istringstream ssMtrx1(firstMatrix);
istringstream ssMtrx2(secondMatrix);
istringstream ss(firstMatrix);
istringstream ssk(secondMatrix);
ssMtrx1 >> sTemp1;
istringstream ssR_C1(sTemp1);
int colmn=0;
while(getline(ssR_C1,sTemp2,',')){
colmn++;
}
//cout << colmn;
if (colmn > 6 ){
cout << "the matrix has more than 6 columns";
}
sTemp1.clear();
int row=0;
while(getline(ssMtrx1,sTemp1)){
row++;
}
//cout << row;
if (row > 6 ){
cout << "the matrix has more than 6 rows";
}
//--------------------------------------------------------------------------------
sTemp1.clear();
ssMtrx2 >> sTemp1;
istringstream ssR_C2(sTemp1);
int colmn2=0;
while(getline(ssR_C2,sTemp2,',')){
colmn2++;
}
//cout << colmn2;
if (colmn2 > 6 ){
cout << "the matrix has more than 6 columns";
}
sTemp1.clear();
int row2=0;
while(getline(ssMtrx2,sTemp1)){
row2++;
}
row2 = row2-1;
//cout << row2;
if (row2 > 6 ){
cout << "the matrix has more than 6 rows";
}
//=========================================================================
int** Mtrx1Arry = new int*[row];
for(int i = 0; i < row; ++i){
Mtrx1Arry[i] = new int[colmn];
}
int** Mtrx2Arry = new int*[row2];
for(int i = 0; i < row2; ++i){
Mtrx2Arry[i] = new int[colmn2];
}
sTemp1.clear();
sTemp2.clear();
while(getline(ss,sTemp1,'\n')){
sTemp2+=sTemp1;
}
//cout << sTemp2;
istringstream ss1(sTemp2);
sTemp1.clear();
for(int i = 0; i < row;i++){
for(int j = 0; j < colmn;j++){
getline(ss1,sTemp1,',');
Mtrx1Arry[i][j]=atoi(sTemp1.c_str());
}
}
for(int i = 0; i < row;i++){
for(int j = 0; j < colmn;j++){
//cout << Mtrx1Arry[i][j] << " ";
}
}
//-------------------------------------------------------------
sTemp1.clear();
sTemp2.clear();
while(getline(ssk,sTemp1,'\n')){
sTemp2+=sTemp1;
}
//cout << sTemp2;
istringstream ss2(sTemp2);
sTemp1.clear();
for(int i = 0; i < row2;i++){
for(int j = 0; j < colmn2;j++){
getline(ss2,sTemp1,',');
Mtrx2Arry[i][j]=atoi(sTemp1.c_str());
}
}
for(int i = 0; i < row2;i++){
for(int j = 0; j < colmn2;j++){
//cout << Mtrx2Arry[i][j] << " ";;
}
}
//=============================================================================
// mtrx1[x][y] and mtrx2[z][y] : y has to == z // while the values are under 6
if( (colmn <= 6) && (colmn2 <= 6) && (row <= 6) && (row2 <= 6)){
if (colmn==row2){
cout << "the two matrices can be multiplied " << endl;
cout << "the new matrix will be : " << row << " x " <<colmn2 << endl;
int** MtrxRsltArry = new int*[row];
for(int i = 0; i < row; ++i){
MtrxRsltArry[i] = new int[colmn2];
}
for(int i = 0; i < row;i++){
for(int j = 0; j < colmn2;j++){
MtrxRsltArry[i][j]=0;
for(int k=0; k < colmn; k++){
MtrxRsltArry[i][j]=MtrxRsltArry[i][j]+(Mtrx1Arry[i][k]*Mtrx2Arry[k][j]);
}
}
}
for(int i = 0; i < row;i++){
//cout << endl;
for(int j = 0; j < colmn2;j++){
//cout << MtrxRsltArry[i][j] << ",";;
}
}
// cout << endl;
ofstream outFile;
string cat = fileDirct+"\\result.txt";
outFile.open(cat.c_str());
cout << "the result.txt has been created at : " << fileDirct << endl;
outFile << "result_M";
for(int i = 0; i < row;i++){
outFile << endl;
for(int j = 0; j < colmn2;j++){
outFile << MtrxRsltArry[i][j] << ",";
}
}
}
else{
cout << "the two matrices can not be multiplied ";
}
}
for(int i = 0; i < colmn; ++i) {
delete [] Mtrx1Arry[i];
}
delete [] Mtrx1Arry;
for(int i = 0; i < colmn2; ++i) {
delete [] Mtrx2Arry[i];
}
delete [] Mtrx2Arry;
return 0; }