hi, well this is the file i have to read into the system... -
<matrix>
-
rows = 2
-
cols = 2
-
-
1 2
-
2 4
-
</matrix>
-
any ideas on how to do it?
below is my code..
and i checked that this doesnt work...
and apparantly someone told me that this is in c programmin style..
could ya help me design a method to read the file in a c++ programming way?
i have done my work.. and this is just exercise from the textbook i am learning c++ from.. unfortunately it doesnt have the solutions.. so i am lost on how to do it.. check my other postings to see that i have actually done my work and this is not homework.. so could someone help me please???? -
void Matrix::readMatrixFile(string FILENAME)
-
{
-
const string filename(FILENAME);
-
ifstream file(filename.c_str());
-
string line;
-
if (getline(file, line) && line == "<matrix>")
-
{
-
if (file >> line /* "rows" */ && file >> line /* "=" */ && file >> row && getline(file,line) /* ; */)
-
{
-
if (file >> line /* "cols" */ && file >> line /* "=" */ && file >> col && getline(file,line) /* ; */)
-
{
-
for (int r = 0; r < row; r++)
-
{
-
for (int c = 0; c < col; c++)
-
{
-
int value;
-
if (file >> value)
-
{
-
m[r][c] = value;
-
}
-
}
-
cout << endl;
-
}
-
}
-
}
-
}
-
}
-
thanks
5 2075 boxfish 469
Recognized Expert Contributor
Hi,
Well, the code works fine for me, what goes wrong when you run it?
gpraghuram 1,275
Recognized Expert Top Contributor
You are reading a ; from the line rows = 2 but the file you have posted dosent have it...
Also i think the logic is complex....
Try reading the vine and tokenize it to get the data.
Raghu
You are reading a ; from the line rows = 2 but the file you have posted dosent have it...
Also i think the logic is complex....
Try reading the vine and tokenize it to get the data.
Raghu
how to tolkenize it to get the data?
could u show me the code that does it?
Hi,
Well, the code works fine for me, what goes wrong when you run it?
well the program doesnt work for me when i run it...
could u take a look and tell me wats wrong?
header file... -
#pragma once
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
#include "Matrix.h"
-
using namespace std;
-
-
class Matrix
-
{
-
public:
-
Matrix(void);
-
~Matrix(void);
-
void readMatrixFile(string FILENAME);
-
void addMatrix(Matrix m1, Matrix m3);
-
void displayMatrix();
-
void transposeMatrix();
-
void multiplyMatrices(Matrix m2, Matrix m3);
-
-
private:
-
int row;
-
int col;
-
/*m = new (double*)[10];
-
for(int i = 0; i < 10; i++)
-
{
-
m[i] = new double[10];
-
}
-
for(int j = 0; j < 10; j++)
-
{
-
delete[] m[i];
-
}
-
delete[] m;*/
-
double m[10][10];
-
};
-
matrix.cpp file
cpp file containing main -
#include <iostream>
-
#include <fstream>
-
#include <string>
-
#include "Matrix.h"
-
using namespace std;
-
-
-
int main(int argc, char* argv[])
-
{
-
Matrix m1, m2 ,m3;
-
char theOption = '0';
-
char whichMatrixToDisplay;
-
string filename1, filename2;
-
-
while(theOption != '5')
-
{
-
cout << "Please choose one of the following options from the menu below:" << endl;
-
cout << "---------------------------------------------------------------" << endl;
-
cout << "(1) Add two matrices" << endl;
-
cout << "(2) Multiply two matrices" << endl;
-
cout << "(3) Take transpose of a matrix" << endl;
-
cout << "(4) Display a matrix" << endl;
-
cout << "(5) Exit" << endl;
-
cout << "---------------------------------------------------------------" << endl;
-
cin >> theOption;
-
-
switch(theOption)
-
{
-
case '1':
-
cout << "You have chosen option 1!: Add two matices!" << endl;
-
cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
-
cout << "Enter name of 1st file" << endl;
-
cin >> filename1;
-
cout << "Enter name of 2nd file" << endl;
-
cin >> filename2;
-
m1.readMatrixFile(filename1);
-
m2.readMatrixFile(filename2);
-
m2.addMatrix(m1, m3);
-
cout << "The two matrices have been added together." << endl;
-
cout << "Select (4): Display a matrix to view it." << endl << endl;
-
break;
-
-
case '2':
-
cout << "You have chosen option 2!: Multiply two matices!" << endl;
-
cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
-
cout << "Enter name of 1st file" << endl;
-
cin >> filename1;
-
cout << "Enter name of 2nd file" << endl;
-
cin >> filename2;
-
m1.readMatrixFile(filename1);
-
m2.readMatrixFile(filename2);
-
m1.multiplyMatrices(m2, m3);
-
cout << "The two matrices have been multiplied with each other." << endl;
-
cout << "Select (4): Display a matrix to view it." << endl << endl;
-
break;
-
-
case '3':
-
cout << "You have chosen option 3!: Transpose a matrix!" << endl;
-
cout << "Please enter the filename that contains the matrix to be transposed!" << endl;
-
cout << "Enter name of file" << endl;
-
cin >> filename1;
-
m1.readMatrixFile(filename1);
-
m1.transposeMatrix();
-
cout << "The matrix has been transposed." << endl;
-
cout << "Select (4): Display a matrix to view it." << endl << endl;
-
break;
-
-
case '4':
-
cout << "You have chosen option 4!: Display a matix!" << endl;
-
cout << "Please enter the option 1, 2 or 3 to display the respective matrices!" << endl;
-
cin >> whichMatrixToDisplay;
-
if(whichMatrixToDisplay == '1')
-
m1.displayMatrix();
-
else if(whichMatrixToDisplay == '2')
-
m2.displayMatrix();
-
else if(whichMatrixToDisplay == '3')
-
m3.displayMatrix();
-
else
-
cout << endl;
-
break;
-
-
case '5':
-
cout << "You have chosen the option to Quit. Goodbye!" << endl;
-
break;
-
-
default:
-
cout << " Sorry the option chosen is invalid. Please enter a valid option!" << endl << endl;
-
break;
-
}
-
}
-
-
return 0;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Steven Burn |
last post by:
The application;
Service on my webserver that allows a user to upload their HOSTS file for functions to verify the contents are still valid.
Uses;
1. XMLHTTP (MSXML2)
2. FileSystemObject...
|
by: Josh |
last post by:
Howdy
i was recently given a program to do. I have to create a 2d matrix
with pointers i have the whole idea down with pointers but there is a
problem with one of them i have the code written down...
|
by: Pushpendra |
last post by:
I am storing the encrypted password in xml file which I have saved in unicode format. While I try to read the xml file and create a dataset from it, it shows me the following error
...
|
by: Babak |
last post by:
Hello everyone,
I'm working on some Finite Elements(FE) codes in C and now I
encountered some problems in assembly stage. The main idea is that a
large number of 3 by 3 elemental stiffness...
|
by: pariub |
last post by:
So here is my problem once... i have a file called
network.txt ...its a 13X13 matrix...but the network.txt can go up to
100X 100.
I should read the text file and store it in array .
...
| |
by: Babak |
last post by:
Hi Everyone,
I've written a standard C code for a simple finite element analysis
in MSVC++ . When I save the file as a cpp file, it compiles and runs
perfectly, but when I save it as a c file,...
|
by: robert maas, see http://tinyurl.com/uh3t |
last post by:
I'm working on examples of programming in several languages, all
(except PHP) running under CGI so that I can show both the source
files and the actually running of the examples online. The first...
|
by: mohammaditraders |
last post by:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
class Matrix
{
private :
int numRows, numCols ;
int elements ;
|
by: dwaterpolo |
last post by:
Hi Everyone,
I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt
and compare them, the first text file has a bunch of values listed like:
y
y
y
y
y
y
y
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |