[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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;
}
0
crollae
Asked:
crollae
  • 2
3 Solutions
 
pb_indiaCommented:
Why don't you use:

vector<double> signalVector;

This will let you read float and int both
0
 
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 OlivaresCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now