C++
how can I fix these errors
this is my code
main.cpp
#include "SpecialArray.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int measureElementsPerLine(ifstream& inFile) {
// Add your code here.
string line;
getline(inFile, line);
int sp = 0;
for (int i = 0; i < line.size(); i++)
{
if (line[i] == ' ')
sp++;
}
sp++;
return sp;
}
int measureLines(ifstream& inFile) {
// Add your code here.
string line;
int n = 0;
while (!inFile.eof())
{
getline(inFile, line);
n++;
}
return n;
}
int main()
{
int numOfLines, numOfElements;
string fileName, dataType;
cin >> fileName >> dataType;
ifstream inFile(fileName);
// Add try statement below
try
{
if (inFile.fail())
throw "File does not exist.";
numOfElements =
measureElementsPerLine(inFile);
inFile.close();
inFile.open(fileName);
numOfLines =
measureLines(inFile);
if (numOfLines == 0)
throw "File is
empty";
inFile.close();
if (dataType == "int") {
SpecialArray<int> specialArray(numOfLines,
numOfElements);
inFile.open(fileName);
specialArray.readFile(inFile);
inFile.close();
specialArray.print();
specialArray.sort();
cout <<
"\nSorted outputs: \n";
specialArray.print();
}
else if (dataType == "string")
{
SpecialArray<string> specialArray(numOfLines,
numOfElements);
inFile.open(fileName);
specialArray.readFile(inFile);
inFile.close();
specialArray.print();
specialArray.sort();
cout <<
"\nSorted outputs: \n";
specialArray.print();
}
}
// Add catch statement below
catch (const char *msg)
{
cout << msg << endl;
return -1;
}
return 0;
}
SpecialArray.h
#ifndef SpecialArray_H
#define SpecialArray_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Add your function declarations and definitions here. With template classes, it is recommended to put them in the same file.
template <class T>
class SpecialArray
{
private:
T **arr;
int rows;
int cols;
public:
SpecialArray()
{
rows = 0;
cols = 0;
arr = NULL;
}
SpecialArray(int r, int c)
{
rows = r;
cols = c;
arr = new T*[r];
for (int i = 0; i < r; i++)
arr[i] = new T[c];
}
~SpecialArray()
{
for (int i = 0; i < rows; i++)
delete[] arr[i];
delete arr;
rows = 0;
cols = 0;
}
void readFile(ifstream &in)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
in >> arr[i][j];
}
}
}
T max()
{
T temp = arr[0][0];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] > temp)
temp = arr[i][j];
}
}
return temp;
}
T min()
{
T temp = arr[0][0];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] < temp)
temp = arr[i][j];
}
}
return temp;
}
void sort()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
for (int k = j + 1; k < cols; k++)
{
if (arr[i][j] > arr[i][k])
{
T temp = arr[i][j];
arr[i][j] = arr[i][k];
arr[i][k] = temp;
}
}
}
}
}
void print()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j];
if (j != cols - 1)
cout << " ";
}
cout << endl;
}
}
void saveToFile()
{
ofstream out;
out.open("output.txt");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
out << arr[i];
if (j != cols - 1)
out << " ";
}
out << endl;
}
out.close();
}
};
#endif
Solution:
Note : The error is due to the no.of lines read in the input file. In the above code it reads an extra one empty line. Following contains the modified code.
Code:
main.cpp:
#include "SpecialArray.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int measureElementsPerLine(ifstream& inFile) {
// Add your code here.
string line;
getline(inFile, line);
int sp = 0;
for (int i = 0; i < line.size(); i++)
{
if (line[i] == ' ')
sp++;
}
sp++;
return sp;
}
int measureLines(ifstream& inFile) {
// Add your code here.
string line;
int n = 0;
while (!inFile.eof())
{
getline(inFile, line);
n++;
}
return (n-1);
//Modification 7
}
int main()
{
int numOfLines, numOfElements;
string fileName, dataType;
cout<<"\nEnter filename : ";
//Modification 1
cin >> fileName;
cout<<"\nEnter datatype : ";
//Modification 2
cin >> dataType;
ifstream inFile(fileName.c_str());
//Modification 3
// Add try statement below
try
{
if (inFile.fail())
throw "File does
not exist.";
numOfElements =
measureElementsPerLine(inFile);
inFile.close();
inFile.open(fileName.c_str()); //Modification 4
numOfLines =
measureLines(inFile);
if (numOfLines == 0)
throw "File is
empty";
inFile.close();
if (dataType == "int") {
SpecialArray<int> specialArray(numOfLines,
numOfElements);
inFile.open(fileName.c_str()); //Modification 5
specialArray.readFile(inFile);
inFile.close();
specialArray.print();
specialArray.sort();
cout <<
"\nSorted outputs: \n";
specialArray.print();
}
else if (dataType == "string")
{
SpecialArray<string> specialArray(numOfLines,
numOfElements);
inFile.open(fileName.c_str()); //Modification 6
specialArray.readFile(inFile);
inFile.close();
specialArray.print();
specialArray.sort();
cout <<
"\nSorted outputs: \n";
specialArray.print();
}
}
// Add catch statement below
catch (const char *msg)
{
cout << msg <<
endl;
return -1;
}
return 0;
}
---------------------------------------
SpecialArray.h:
#ifndef SpecialArray_H
#define SpecialArray_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Add your function declarations and definitions here. With
template classes, it is recommended to put them in the same
file.
template <class T>
class SpecialArray
{
private:
T **arr;
int rows;
int cols;
public:
SpecialArray()
{
rows = 0;
cols = 0;
arr =
NULL;
}
SpecialArray(int r, int c)
{
rows = r;
cols = c;
arr = new
T*[r];
for (int i = 0;
i < r; i++)
arr[i] = new T[c];
}
~SpecialArray()
{
for (int i = 0;
i < rows; i++)
delete[] arr[i];
delete
arr;
rows = 0;
cols = 0;
}
void readFile(ifstream
&in)
{
for (int i = 0;
i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
in >> arr[i][j];
}
}
}
T max()
{
T temp =
arr[0][0];
for (int i = 0;
i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] >
temp)
temp =
arr[i][j];
}
}
return
temp;
}
T min()
{
T temp =
arr[0][0];
for (int i = 0;
i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] <
temp)
temp =
arr[i][j];
}
}
return
temp;
}
void sort()
{
for (int i = 0;
i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
for (int k = j + 1; k <
cols; k++)
{
if
(arr[i][j] > arr[i][k])
{
T temp = arr[i][j];
arr[i][j] = arr[i][k];
arr[i][k] = temp;
}
}
}
}
}
void print()
{
for (int i = 0;
i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout <<
arr[i][j];
if (j != cols - 1)
cout
<< " ";
}
cout << endl;
}
}
void saveToFile()
{
ofstream
out;
out.open("output.txt");
for (int i = 0;
i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
out << arr[i];
if (j != cols - 1)
out
<< " ";
}
out << endl;
}
out.close();
}
};
#endif
----------------------------------------------------------------
Output:
------------------------------------------------------------------------------------------
Please comment if you have any queries on this solution..
Kindly up-vote if you are satisfied with this solution.
C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <...
Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){ int rows = 5; int cols = 5; int x; int** arr = new int[rows][cols] cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x]; return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){ int rows; int cols; int x;...
Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() { const int n = 50; int *arr,...
I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...
How can I fix that ? Can you please explain clearly ? main.cpp 2 3 using namespace 4. 5 int main() std; unsigned int x = 0b01101100; 8 9 10 12 13 14 15 16 17 18 19 20 21 unsigned int z = 0x12345678; unsigned int e 0x7; unsigned int w = 0x87654321; unsigned int =0x123; int 1; int 92; int Q3; int Q4; Q1=x&y; cout << bitset<8> (Q1) << endl; Q2=-(x| y); cout << bitset<8>(Q2) << endl; Q3=(z...
Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...
I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...
Can some one fix my code so that the output result same as below? BAR PLOT OF CYLINDER PRESSURE -VS- TIME pressure is on horizontal axis - units are psi time is on vertical axis - units are msec 0.0 20.0 40.0 60.0 80.0 100.0 120.0 +---------+---------+---------+---------+---------+---------+ 0.0|************************* 1.5|********************************* 3.0|***************************************** 4.4|************************************************** 05.9|********************************************* 7.4|******************************** 8.9|*********************** 10.4|***************** 11.9|************** 13.3|************* 14.8|************ 16.3|********* 17.8|******* 19.3|****** 20.7|****** 22.2|******* 23.7|******* .......... ===================== her is my code //#include"stdafx.h" #include #include #include #include #include #include using...
Would somebody be able to assist me with this C++ problem ? E. What would the code segment in Listing 4 output when it is executed? Listing 4: Code Segment 1 const int ROWS = 2; 2 const int COLS = 3; 3 int x[ROWS] [COLS] = {{2,3,5}, {7,11 , 13}}; 4 int i, j; 5 for (j=0; j<COLS; j++) 6 { cout <<"Column"<<j +1 <<": "; for (i=0; i<ROWS; i++) 11 cout <<x[i][j]<<" "; } cout <<endl; 13 }
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...
How do I compile c++ code in terminal. Below i have some code to apply Gaussian Filter on an image using OpenCV. I have tried compling using the code g++ Project2.cpp -o Project2 `pkg-config --cflags --libs opencv` && ./Project2 and it gives me a whole list of errors saying directory not found. --------------------------------------------------------------------------------------------------------- #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <opencv2/core/core.hpp> #include <iostream> using namespace cv; using namespace std; int main() { Mat image = imread("//u//oowens//First.jpg"); int rows=image.rows; int cols=image.cols; if (image.empty())...