1

I could use your help on this problem. I have a file that contains this:

1x+1y+1z=5
2x+3y+5z=8
4x+0y+5z=2

I must store that into a string. Once stored the output should be this:

1x+1y+1z=5
a=1 b=1 c=1 d=5
2x+3y+5z=8
a=2 b=3 c=5 d=8
4x+0y+5z=2
a=4 b=0 c=5 d=2

This is the code I have, however it is not outputting anything. Can anybody help me? It gives me an error on line 19 but I don't know how to fix it. The error states "no matching function for call."

|19|error: no matching function for call to 'std::basic_ifstream::getline(std::string [10], int)'| |22|error: no matching function for call to 'std::basic_istringstream::basic_istringstream(std::string [10])'|

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    ifstream file("matrix.txt");

    if (!file)
    {
        cout << "Could not open input file\n";
        return 1;
    }


    for (string line[10]; file.getline(line, 10); )
    {
        cout << line << '\n';
        istringstream ss(line);

        double a, b, c, d;
        char x, y, z, eq;

        if (ss >> a >> x >> b >> y >> c >> z >> eq >> d)
        {
            if (x == 'x' && y == 'y' && z == 'z' && eq == '=')
            {
                cout << "a = " << a
                     << "  b = " << b
                     << "  c = " << c
                     << "  d = " << d << '\n';
            }
            else
            {
                cout << "parse error 2\n";
            }
        }
        else
        {
            cout << "parse error 1\n";
        }

    }

}
  • 2
    Whenever asking "Why doesn't this code work" please provide all errors you get when compiling/running – Tyler Oct 03 '17 at 15:41
  • [`using namespace std;` is a bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), never use it. – tambre Oct 03 '17 at 15:43
  • `for (string line[10]; file.getline(line, 10); )` This line looks highly suspicious to me. – Borgleader Oct 03 '17 at 15:43
  • Post the **exact** error you receive from the compiler character-for-character in a code block, not a re-phrased or a shortened one. – tambre Oct 03 '17 at 15:44

2 Answers2

1

There are a couple of mistakes.

Mistake no. 1

You cannot and should not declare string line[10]. You also should not use a for loop. Here is a classic implementation of reading string by string from a stream:

string line;
while (file >> line) {
  ... // do stuff
}

Mistake no. 2

Your numbers are ints, not doubles, so you should use:

int a, b, c, d;

Saving values into an array

First of all, let me say, that there is no good reason to use raw arrays. You should always prefer using standard ADTs, such as the std::vector.

In this scenario, you should not read the value right into the vector, since the value may be ill-formed.

Instead follow this sequence:

vector<string> lines;
string line;
while (file >> line) {
  ... // do stuff

  if (successful)
    lines.push_back(line);
}
Daniel Trugman
  • 8,186
  • 20
  • 41
0

Here is code to read a file and insert into an array.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAX_LINES = 100;

int main() {
    std::string strArray[MAX_LINES];
    fstream newFile;
    newFile.open("matrix.txt", ios::in); //open a file to perform read operation
    if (newFile.is_open()) {   //checking whether the file is open
        string tp;
        for (int counter = 0; getline(newFile, tp); counter++) { // get new line
            strArray[counter] = tp; // read into string array
            cout << tp << "\n"; //print the string
        }
        newFile.close(); //close the file object.
    }
    return 0;
}