no matching function for a call to naiveGaussianElimination - c++

I am trying to debugg this, but can't figure out the issue here. Why is it saying no matching function for a call to naiveGaussianElimination evnthough i have passed the correct parameters?
void naiveGaussianElimination(int count,float doubleCoefficient[][count+1]) {
}
int main() {
/*
Read from file and assign values to vector
*/
//File stream object
ifstream inputFile;
// store file name
string fileName;
// ask user for the file name and store it
cout << "Enter the file name:>> ";
cin >> fileName;
//Open the txt file
inputFile.open(fileName.c_str());
//search for the text file
if(!inputFile.is_open())
{
cerr << "Error opening file \n";
exit(EXIT_FAILURE);
}
else
{
cout << "File found and successfully opened. \n";
}
/*
find the number of variables in the equation
*/
int count =0;
string line;
while (getline(inputFile, line)){
count++;
}
cout << "Number of variables: " << count << endl; // show total variables in text file
// clear the eof flag and set it to top
inputFile.clear();
inputFile.seekg (0, ios::beg);
// 2D array to store augmented matrix
float doubleCoefficient [count][count+1];
naiveGaussianElimination(count,doubleCoefficient);
inputFile.close();
}

**Use dynamic array for
//2D dynamic array to store augmented matrix**
float **doubleCoefficient = new float*[count];
for (int i=0; i<(count+1); i++) {
doubleCoefficient[i] = new float[count+1];
}
}

Related

Can't get fstream to read allocate an array size

I'm trying to use a void function to read from a text file and allocate the size of a vector struct.
This is my void function.
void allocateFlight( ifstream &inFile, string &item,vector<Flight>&flight, int &i){
inFile.open("reservations.txt");
//check for error
if (inFile.fail())
{
cerr << "Error Opening File" << endl;
exit(1);
}
while (inFile >>item)
{
i = 0;
flight.resize(i);
if (item.size() == 3){
i++;
flight.resize(i);
}
}
}
This is kept in a separate cpp file and in my main file I declared my variables and tried calling it but I'm only getting zero when I try to compile.
string item;
int i;
fstream inFile;
//this is the flgiht array to partition the amount of flights based on the first line of text indexing
std::vector<Flight> flight;
int main(){
//pass by reference to allocateFlights
allocateFlight(inFile, item ,flight, i);
//int numFlights;
cout << flight.size();
I'm stumped trying to figure out what is wrong.

Am I passing parameters correctly?

I'm trying to get the function getFilename to prompt the user for which file to read and then pass that to the function readFile which calculates the numbers in the file and then (using displayAverage) display the average of the numbers in the file.
I'm new to coding and can't figure out why it doesn't seem to be using the readFile function... the program prompts the user for the file but after that it just inputs a blank line. Am I calling the functions & passing the parameters correctly?
void getFilename(char fileName[])
{
cout << "Please enter the filename: ";
cin >> fileName;
return;
}
float readFile(char fileName[])
{
cout.setf(ios::fixed);
cout.precision(0);
ifstream fin(fileName);
int sum = 0;
int numValue = 0;
float grades = 0;
float average= 0;
if (fin.fail())
{
cout << "Error opening file \"" << fileName << "\"";
return false;
}
while (!fin.eof())
{
fin >> grades;
sum += grades;
numValue++;
if (numValue != 10)
cout << "Error reading file \"" << fileName << "\"";
}
fin.close();
average = sum / 10;
return average;
}
void displayAverage(int average)
{
cout << average;
return;
}
int main()
{
char* fileName;
int average;
getFilename(fileName);
readFile(fileName);
displayAverage(average);
return 0;
}
Your program has undefined behaviour since fileName does not point to anything valid that can hold data.
Unless you are required to use an array of chars to hold the filename, use std::string for fileName.
std::string fileName;
If you are required to use an array of chars to hold the filename, use
char fileName[FILENAME_LENGTH];
Make sure FILENAME_LENGTH is large enough for your needs.

C++ Read file into vectors

I'm having trouble figuring out how to read a file line by line into different data type vectors. Is there a way to do this with inFile >> ? My code is below. Thanks in advance!
void fileOpen()
{
fstream inFile;
inFile.open(userFile);
// Check if file open successful -- if so, process
if (!inFile.is_open()) {cout << "File could not be opened.";}
else
{
cout << "File is open.\n";
string firstLine;
string line;
vector<char> printMethod;
vector<string> shirtColor;
vector<string> orderID;
vector<string> region;
vector<int> charCount;
vector<int> numMedium;
vector<int> numLarge;
vector<int> numXL;
getline(inFile, firstLine); // get column headings out of the way
cout << firstLine << endl << endl;
while(inFile.good()) // while we are not at the end of the file, process
{
while(getline(inFile, line)) // get each line of the file separately
{
for (int i = 1; i < 50; i++)
{
inFile >> date >> printMethod.at(i);
cout << date << printMethod.at(i) << endl;
}
}
}
}
}
Before you use vector.at(i) in your case you should be sure that your vector is long enough cause at will generate out of range exception. As I can see from your code your vector printMethod contains no more than 50 elements so you can resize vector printMethod before use e.g.
vector<char> printMethod(50);
or
vector<char> printMethod;
printMethod.resize(50);
If you're planning to use a variable number of elements more than 50 you should use push_back method like #Phil1970 recommended e.g.
char other_data;
inFile >> date >> other_data;
printMethod.push_back(other_data);

The number of lines is always 0

Why is the number of lines always 0? It should be 10, but the output is always 0.Is anything wrong with the method?
int main() {
vector<double> doubleCoefficient; // vector to store unknown number of equations (double)
/* Read from file and assign values to vector */
//File stream object
ifstream inputFile;
//Open the txt file
inputFile.open("test.txt");
//search for the text file
if(!inputFile.is_open())
{
cerr << "Error opening file \n";
exit(EXIT_FAILURE);
}
else
{
cout << "File found and successfully opened. \n";
}
double x;
while(!inputFile.eof()){
inputFile >> x;
doubleCoefficient.push_back(x);
}
int count =0;
string line;
while (getline(inputFile, line)){
count++;
}
cout << "Number of lines in text file:" << count << endl;
inputFile.close();
}
With while(!inputFile.eof()) You go to the end of the file, so after that, You cann't read lines.
You need to go back to the start using fseek()
try
fseek ( inputFile , 0 , SEEK_SET );
before counting the lines.

c++ initializing array storage size

void start ( string fname )
{
string FirstElement;
int count = 0 ;
fstream Infile;
Infile.open( fname.c_str(), ios::in ); // Open the input file
while(!Infile.eof()) // using while to look for the total lines
{
count++;
}
//read to the array
string data_array[]; //initializing an array
for(int i=0; !Infile.eof(); i++){
Infile >> data_array[i]; // storing the value read from file to array
}
//Display the array
// for(int i=1; i<11; i++){
// cout << data_array[i] << endl;
//}
cout << data_array[0] << endl;
cout << count << endl;
return;
}
I have a text files contain values lines by lines
My plan was to use the while loop to do a total count of the lines
and place it in the "string data_array[]" but somehow it doesnt work that way.
anyone can advise me on how can I make it in a way that It can have a flexible storage size going according to the numbers of values in the text files? thanks
For flexible storage as you call it, you may use STL's container, such as std::vector<T> or std::list<T>. Other issues are highlighted in inline comments.
// pass by reference
void start(const std::string& fname)
{
// use std::ifstream, instead of std::fstream(..., std::ios::in);
std::ifstream Infile(fname.c_str());
// prefer std::vector to raw array
std::vector<std::string> data_array;
std::string line;
// read line by line
while (std::getline(Infile, line))
{
data_array.push_back(line); // store each line
}
// print out size
std::cout << data_array.size() << std::endl;
// display the array, note: indexing starts from 0 not 1 !
for(int i = 0; i < data_array.size(); ++i)
{
std::cout << data_array[i] << std::endl;
}
}