0

I have this program:

#include <iostream>
#include <vector>
#include <math.h>
#include <cstdlib>
#include <iomanip>  
#include <sstream>

using namespace std;




void ProvaErrore() {

    string line, line1;
    char delim = ' ';
    ifstream myfile("DatiMemoria.txt");
    int i = 0, j = 0;
    vector<vector<double>> vettore(40);
    vector<double> risultato;

    while (getline(myfile, line1))
    {
        stringstream ss(line1);
        while (getline(ss, line, delim))
        {
            if (j == 12)
            {
                i++;
                j = 0;
            }
            if (j == 0)
            {
                if (line == 'p')
                {
                    risultato.push_back(0);
                }
                else
                {
                    risultato.push_back(1);
                }
            }
            if (j == 1)
            {
                if (line == 'x')
                {
                    vettore[i].push_back(0);
                }
                else
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 2)
            {
                if (line == 's')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(0);
                }
                if (line == 'y')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(1);
                }
                if (line == 'f')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(0);
                }
            }
            if (j == 3)
            {
                if (line == 'w')
                {
                    vettore[i].push_back(0);
                }
                if (line == 'n')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 4)
            {
                if (line == 't')
                {
                    vettore[i].push_back(0);
                }
                if (line == 'f')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 5)
            {
                if (line == 'l')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(0);
                }
                if (line == 'p')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(1);
                }
                if (line == 'n')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(0);
                }
                if (line == 'a')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(1);
                }
            }
            if (j == 6)
            {
                if (line == 'b')
                {
                    vettore[i].push_back(0);
                }
                if (line == 'n')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 7)
            {
                if (line == 'k')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(0);
                }
                if (line == 'w')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(1);
                }
                if (line == 'n')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(0);
                }
                if (line == 'p')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(1);
                }
            }
            if (j == 8)
            {
                if (line == 'c')
                {
                    vettore[i].push_back(0);
                }
                if (line == 'e')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 9)
            {
                if (line == 'k')
                {
                    vettore[i].push_back(0);
                }
                if (line == 'n')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 10)
            {
                if (line == 's')
                {
                    vettore[i].push_back(0);
                }
                if (line == 'v')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 11)
            {
                if (line == 'g')
                {
                    vettore[i].push_back(0);
                }
                if (line == 'u')
                {
                    vettore[i].push_back(1);
                }
            }
            j++;

        }
    }
    for (int i = 0; i < 40; i++)
    {
        for (int j = 0; j < 14; j++)
        {
            cout << vettore[i][j]<<" ";
        }
        cout << "\n";
    }

}

With that txt file:

e x y w t a b n c n s g
p x s w t p n k e k s g
p x y n t p n k e n v u
p x y w t p n w e k s g
p x s w t p n n e n s g
p x y w t p n w e n s g
p x y w t p n k e n s g
p x y w t p n k e k s g
p x y n t p n p e k v u
e f f n f n n p e n v u
p f y n t p n k e k v u
p x s w t p n w e n s g
p f y n t p n k c n v u
p x y n t p n n e n v u
p x s w t p n k e n s g
p f s n t p n p e k v u
p x y n t p n p e n v u
p x y w t p n n e k s g
p x y n t p n k e k v u
e x f n f n n n e k v u
e x s w t l b k c k s g
e x f n f n n k e n v u
e x y w t l b w c k s g
p f y n t p n p e n v u
e x s w t l b n c n s g
e x y w t a b w c n s g
e x y w t a b k c n s g
e x y w t l b k c k s g
e x f n f n n p e k v u
p x y n t p n n e k v u
e f f n f n n k e k v u
e x s w t l b w c n s g
e f f n f n n k e n v u
e x f n f n n n e n v u
e x s w t a b k c n s g
e f f n f n n p e k v u
e x f n f n n p e n v u
e x y w t l b n c k s g
e x f n f n n k e k v u
p x y w t p n n e n s g

It is a simple program useful for data analysis. I have a file with 40 lines. Each line is a mushroom and each column is a feature for example the first one is edible or poisonus. I want to read that file and transform the letter in numbers (in bynary numbers) in order to put them in a neural network. Normally I do this with python but I wanted to try in c++

When I first run it everything is fine. But if I try to rerun it I have this error:

error: no matching constructor for initialization of 'std::stringstream' (aka 'basic_stringstream<char, char_traits<char>,
      allocator<char> >')
                stringstream ss(line1);
                             ^  ~~~~~
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.20.27508\include\sstream:531:14: note: candidate constructor not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>,
      allocator<char> >') to 'ios_base::openmode' (aka 'int') for 1st argument
    explicit basic_stringstream(ios_base::openmode _Mode = ios_base::in | ios_base::out)
             ^
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.20.27508\include\sstream:539:5: note: candidate constructor not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>,
      allocator<char> >') to 'std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' for 1st
      argument
    basic_stringstream(basic_stringstream&& _Right) : _Mybase(&_Stringbuffer) { // construct by moving _Right
    ^
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.20.27508\include\sstream:562:5: note: candidate constructor not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>,
      allocator<char> >') to 'const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' for
      1st argument
    basic_stringstream(const basic_stringstream&) = delete;

Why?

MementoMori
  • 283
  • 4
  • 10
  • 2
    When you say "run" and "rerun" I really think you mean *"build"*. – Some programmer dude Jan 27 '20 at 08:57
  • @Someprogrammerdude Yes – MementoMori Jan 27 '20 at 09:00
  • 2
    Please try to create a [mcve] to show us, because the code snippet you show shouldn't give the errors you get. – Some programmer dude Jan 27 '20 at 09:12
  • It would probably be useful to post more of your code. I'm not seeing anything wrong with your example (other than the way you're dealing with file input, but I might just not be seeing the full picture). – Drake Johnson Jan 27 '20 at 09:32
  • Does it make any difference to declare `ss` as an `std::istringstream` or explicitly pass the open mode? It seems that MSVC can't deduce the [right overload of the constructor(3)](https://en.cppreference.com/w/cpp/io/basic_stringstream/basic_stringstream). – Bob__ Jan 27 '20 at 09:38
  • @DrakeJohnson I have edited the question with my program that gives error, not the first time but the second time I run it – MementoMori Jan 27 '20 at 09:42
  • @Someprogrammerdude I have edited the question with my program that gives error, not the first time but the second time I run it – MementoMori Jan 27 '20 at 09:42
  • @MementoMori It would also help a lot of you explained what exactly you're trying to do with your script. Right now all I'm looking at is a bunch of nested if statements and a weird way of reading in files. – Drake Johnson Jan 27 '20 at 09:45
  • @Bob__ I have edited the question with the program. It seems that doesn't make difference – MementoMori Jan 27 '20 at 09:45
  • Note that the posted code doesn't have an `#include `. – Bob__ Jan 27 '20 at 09:48
  • @MementoMori This is not a [repro]. It is neither complete (doesn't have a `main`), nor is it minimal (all the `if`s are irrelevant to the error). Please read the link again carefully. Your code is however at least missing includes for `` and `` and there are other clear errors. For example you cannot compare strings and character literals with `==`. – walnut Jan 27 '20 at 09:48
  • The code you show results in two categories of errors when I try to build it: The first is that `ifstream` is an incomplete type, because you don't include ``. The second category which results in a *lot* of errors is that you compare a string (`line`) with *single characters* (like in e.g. `line == 'p'`). – Some programmer dude Jan 27 '20 at 09:52
  • I have understood that the code is not minimal and I'm trying to fix it. But the code worked perfectly for me the first time that i run it. I'm using root cern with viisual studio 2019 but I think that it is Always c++, this is why I have not main. The file name is ProvaErrore – MementoMori Jan 27 '20 at 09:55
  • @MementoMori It is hard to believe that this code ever worked. While code *may* still work if includes are missing, comparing `std::string`s to `char`s (character literals) was always ill-formed, assuming you didn't define an overload for that manually. It can not have run on any C++ compiler. – walnut Jan 27 '20 at 10:15
  • 1
    Actually, (without knowing much about ROOT, but looking at the documentation): It seems `TString` has constructor overloads for both `std::string` and `char`. Given that the documentation doesn't mention that they are `explicit`, if you include this `TString` class the `==` comparison between `std::string` and `char` might work since it will call the `operator==(const TString&, const TString&)` overload, implicitly converting both arguments. This would not work in standard C++. You should probably add to your question that you are using ROOT and how exactly you are using it. – walnut Jan 27 '20 at 10:19
  • @walnut the code works, so probably is what you are talking about – MementoMori Jan 27 '20 at 10:20
  • 1
    @MementoMori You need to explain exactly how you were using the code when it worked and how you are using it now. ROOT is quite different from just standard C++. I suggest you add the `root-framework` tag and explain how you are using it. – walnut Jan 27 '20 at 10:24
  • @walnut Thank walnut, i don't know that there is that difference, I use and Always used Root to program i don't know also that there is root tag. I will edit the question soon – MementoMori Jan 27 '20 at 10:27

2 Answers2

0

you forgot to

#include <fstream>

the header of std::ifstream


 if (line == 'p')

doesn't work, as line is a std::string. Here you want

 if (line[0] == 'p')

to receive the target character. So replace all line == instances with line[0] ==


As an advice, try to improve the quality of your code. You could achieve that by diving your structure into sections and use functions to handle each task.

also, you shouldn't use namespace std;


full code:

#include <iostream>
#include <vector>
#include <math.h>
#include <cstdlib>
#include <iomanip>  
#include <sstream>
#include <fstream>

using namespace std;


void ProvaErrore() {

    string line, line1;
    char delim = ' ';
    ifstream myfile("DatiMemoria.txt");
    int i = 0, j = 0;
    vector<vector<double>> vettore(40);
    vector<double> risultato;

    while (getline(myfile, line1))
    {
        stringstream ss(line1);
        while (getline(ss, line, delim))
        {
            if (j == 12)
            {
                i++;
                j = 0;
            }
            if (j == 0)
            {
                if (line[0] == 'p')
                {
                    risultato.push_back(0);
                }
                else
                {
                    risultato.push_back(1);
                }
            }
            if (j == 1)
            {
                if (line[0] == 'x')
                {
                    vettore[i].push_back(0);
                }
                else
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 2)
            {
                if (line[0] == 's')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(0);
                }
                if (line[0] == 'y')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(1);
                }
                if (line[0] == 'f')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(0);
                }
            }
            if (j == 3)
            {
                if (line[0] == 'w')
                {
                    vettore[i].push_back(0);
                }
                if (line[0] == 'n')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 4)
            {
                if (line[0] == 't')
                {
                    vettore[i].push_back(0);
                }
                if (line[0] == 'f')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 5)
            {
                if (line[0] == 'l')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(0);
                }
                if (line[0] == 'p')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(1);
                }
                if (line[0] == 'n')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(0);
                }
                if (line[0] == 'a')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(1);
                }
            }
            if (j == 6)
            {
                if (line[0] == 'b')
                {
                    vettore[i].push_back(0);
                }
                if (line[0] == 'n')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 7)
            {
                if (line[0] == 'k')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(0);
                }
                if (line[0] == 'w')
                {
                    vettore[i].push_back(0);
                    vettore[i].push_back(1);
                }
                if (line[0] == 'n')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(0);
                }
                if (line[0] == 'p')
                {
                    vettore[i].push_back(1);
                    vettore[i].push_back(1);
                }
            }
            if (j == 8)
            {
                if (line[0] == 'c')
                {
                    vettore[i].push_back(0);
                }
                if (line[0] == 'e')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 9)
            {
                if (line[0] == 'k')
                {
                    vettore[i].push_back(0);
                }
                if (line[0] == 'n')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 10)
            {
                if (line[0] == 's')
                {
                    vettore[i].push_back(0);
                }
                if (line[0] == 'v')
                {
                    vettore[i].push_back(1);
                }
            }
            if (j == 11)
            {
                if (line[0] == 'g')
                {
                    vettore[i].push_back(0);
                }
                if (line[0] == 'u')
                {
                    vettore[i].push_back(1);
                }
            }
            j++;

        }
    }
    for (int i = 0; i < 40; i++)
    {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 0 0 0 0 1 0 0
1 1 0 1 1 1 0 1 1 1 1 0 1 1
0 1 0 1 1 1 0 1 1 1 1 1 1 1
0 0 1 0 0 0 0 0 1 0 0 0 0 0
0 1 0 1 1 1 0 1 0 0 1 0 1 1
0 0 1 0 0 0 1 1 1 0 1 1 0 0
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • Thank you for the answer but no, I have it. – MementoMori Jan 27 '20 at 09:08
  • @MementoMori if you solved your own problem, add an answer so that people with the same problem will have a solution. – Stack Danny Jan 27 '20 at 09:42
  • I don't know, probably it is a bit different. See my last comment above. The first time that I run it it is fine no errors. In line i have symple letters. I have tried with cout< – MementoMori Jan 27 '20 at 10:03
  • If you try to rerun it a second time, or rebuild as they have said above, is it fine for you? – MementoMori Jan 27 '20 at 10:12
  • @MementoMori yes it works for me, as the output I posted shows. make sure to `#include ` this time. or just copy the code, I posted the whole thing. – Stack Danny Jan 27 '20 at 10:46
0

It looks like you're trying to do file IO by, first, splitting a file into lines and, second, splitting each line by spaces (which, in your example, splits them into 1-character-long strings). Instead, you could just take a range-based loop through every other character (especially since every line has the same number of characters). Opening the file like the following would avoid using std::stringstream altogether:

std::string current_line;
/* vectors here */
std::ifstream myfile("DatiMemoria.txt");

// You had int j(char_num) = 0, i(line_num) = 0;
// I renamed them to make it clear what they're for
// Also, `char_num` is not initialized until later
unsigned line_num = 0;
if (myfile.is_open())
{
    while (std::getline(myfile, current_line))
    {
        for (int char_num = 0; char_num <= 2 * (12 - 1); char_num += 2)
        {
            // use:
            //     current_line.at(char_num)
            // to get the current character. Then do what you need
            // to with that character. Then, the loop will move to 
            // the next character.
        }
        line_num++;
    }
}

However, just so I'm not ignoring your question, it seems your compiler is wanting to use a different constructor for stringstream than the one that takes in the const std::string&. In fact, if you look at your error log, it only shows 3 possible constructors. This is why I tend to try to avoid using stringstream.

If you're dead set on using stringstream, though, you can just create the stream and then push the string into it in two separate steps.

std::string str;
std::stringstream sstrm;
sstrm << str;

Or, if strings are giving you errors,

std::string str;
std::stringstream sstrm;
sstrm << str.c_str();

I can't imagine any sane compiler having a problem with that.

Drake Johnson
  • 640
  • 3
  • 19