I've been trying to make a program that can read some information from a file and store it in memory to compare with user input. It's sort of a quiz with organic synthesis. A synthesis is dived into steps so I chose to make a synthesis a vector of vector strings. That way every vector inside the main vector is a step and everything inside the step is the reactant and the solvent required for the reaction. As you will see, each synthesis is stored in a text file that has a number for a name. it has a number for a name because that way it is easier to get the level of difficulty of the synthesis.The problem I'm facing is that when I try to read the file that contains the synthesis I get weird formatting errors. Here's the code snippet

int disponible=100; // las sintesis disponibles del 0 al 99 en ese level
    ifstream tempsynth;
    int SynthNumber;

         do {
         SynthNumber = rand() % (disponible)+ Level*100;
         string filename=itoa(SynthNumber)+".txt";
         tempsynth.open(filename.c_str(),ios::binary);

         if (!tempsynth)
            disponible=disponible-5;   //como no van a haber 100 sintesis se baja la busqueda hasta que hallan
                                    }

         while (!tempsynth);

            getline(tempsynth,Pasosstr);
            stringstream(Pasosstr)>>Pasos;


            string tempstr;
            Paso temp;
            temp.clear();

              while(tempsynth ){
             for (int x=0;x<6;x++){
                getline(tempsynth,tempstr);

                if (tempstr!="#" && tempstr!="TM\0" && tempstr!="\0" && tempstr!="\n"){
                temp.push_back(tempstr);

                }


                                    }

               Key.push_back(temp);

for (int x=0;x< (int) temp.size();x++)
Log<<"Temp tiene---->"<<temp[x]<<" en la posicion "<<x<<"\n";
               temp.clear();
                     }

The output from the last for loop is this:

Temp tiene---->NAOH
 en la posicion 0
Temp tiene---->1EQUI
 en la posicion 1
Temp tiene---->*H2O
 en la posicion 2
Temp tiene---->#
 en la posicion 3
Temp tiene---->KH
 en la posicion 4
Temp tiene---->|
 en la posicion 5
Temp tiene---->NAOH
 en la posicion 0
Temp tiene---->*H2O
 en la posicion 1
Temp tiene---->TM
 en la posicion 2
Temp tiene---->
 en la posicion 3
Temp tiene---->3
 en la posicion 4
Temp tiene---->4
 en la posicion 5
Temp tiene---->5
 en la posicion 0

The file it's reading from has this written on it:

2

NAOH

1EQUI

*H2O

#

KH

|

NAOH

*H2O

TM



3

4

5

#

Anyone know what wrong with this? Why is the line getting eaten up like that?

Recommended Answers

All 6 Replies

you got it all wrong I mean realy

for (int x=0;

can't you declare it outside the loop??
and second what exactly does Paso::clear() do?

Paso is typedef I created that is the same as vector<string>. I cleared the temporary vector because I was doing some error checking and I needed the size of the vector. If I didn't clear I'd get some weird results.
Yes, I can declare x outside the loop, but it's the same thing. I already figured out a way to work around this, but I'd like to know what's wrong with the code anyway.

is the operator [] of the class using a reference to int x?? 'cause if it's, the problem might be there.

It's just accessing a the string in the position x of the vector.

whell there's something messing with your int right there.

What's messing with loop or with the output? I'm sorry I don't understand what you're tying to say.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.