I am currently readin in integers from a text file into a vector. How do I read in a mixture of floats and integers?

How can I alter this code to read in not only integers but a mixture of float values too?  Do I need to create a struct or class for a user defined type?  I have tried both of these but can't get my code to compile.

// File: get_data.h

#ifndef GET_DATA_H
#define GET_DATA_H

using namespace std;

class anneTest
{
public:
    const vector<int> get_data(string);   // prototype

protected:
private:

};

#endif


// File: get_data.cpp

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

#include "get_data.h"
#include "main.h"


      const vector<int> anneTest::get_data(string filename) {

                      vector<int> signalVector;

            int signal_values;

            // InputFileStream - inputs data from text file
            ifstream data_in;

            // Opens data file
            data_in.open(filename.c_str());  

      
            if (!data_in) {

                  cout << "error, could not open file" << endl;
            }
            else {

                  cout << "file opened successfully" << endl;
            }

            while (data_in >> signal_values) {

                  signalVector.push_back(signal_values);
            }

            data_in.close();

            return signalVector;
      }


// File: main.cpp

#include <iostream>
#include <vector>

using namespace std;

#include "get_data.h"

int main() {

   string data_in = "Book1.txt";

   anneTest event;

   vector<int> vector_returned = event.get_data(data_in);

   cout << "Size of vector = " << vector_returned.size() << endl;

      return 0;
}
crollaeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pb_indiaCommented:
Why don't you use:

vector<double> signalVector;

This will let you read float and int both
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stefan73Commented:
Hi crollae,
double is a good compromise; a float can be stored there and mantissa resolution is sufficient to store an int.

Another alternative is combination of a type info and an anonymous union:

struct intfloat{
    enum{is_float,is_int} type;
    union{
        float val_f;
        int   val_i;
    }
}

...but you won't save any space by this. On almost all systems, padding will create a size of 64 bits per element.

So use a double. Less fuss...

Cheers!

Stefan
0
pb_indiaCommented:
//Here are all the places you can replace with double. Use below and it should work for your proble,

// File: get_data.h

#ifndef GET_DATA_H
#define GET_DATA_H

using namespace std;

class anneTest
{
public:
    const vector<double> get_data(string);   // prototype

protected:
private:

};

#endif


// File: get_data.cpp

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

#include "get_data.h"
#include "main.h"


     const vector<double> anneTest::get_data(string filename) {

                     vector<double> signalVector;

          int signal_values;

          // InputFileStream - inputs data from text file
          ifstream data_in;

          // Opens data file
          data_in.open(filename.c_str());  

     
          if (!data_in) {

               cout << "error, could not open file" << endl;
          }
          else {

               cout << "file opened successfully" << endl;
          }

          while (data_in >> signal_values) {

               signalVector.push_back(signal_values);
          }

          data_in.close();

          return signalVector;
     }


// File: main.cpp

#include <iostream>
#include <vector>

using namespace std;

#include "get_data.h"

int main() {

   string data_in = "Book1.txt";

   anneTest event;

   vector<double> vector_returned = event.get_data(data_in);

   cout << "Size of vector = " << vector_returned.size() << endl;

     return 0;
}
0
Jaime OlivaresSoftware ArchitectCommented:
As an alternative you can use a union to create a "variant" type:

typedef union {
    int intv;
    float floatv;
    double doublev;
} TVariant;

Then you can use as:
vector <TVariant> myVector;

or manipulate a single element as,
cout << myVector[5].intv << endl;

I think could be a bit more legible.

Good luck,
Jaime.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.