Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading input from command line, separated by whitespace, into array of objects

I am experimenting with reading input from command line and successfully store them in objects attributes.

Example of input (./(nameOfExecutable) < (sourceText) in the command line)

20 5
1 5
28 5
2 5
20 5
4 5
22 5
88 3
27 5
34 5

I want to read and store them into object attributes.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

experimentClass.h

#ifndef EXPERIMENTCLASS_H
#define EXPERIMENTCLASS_H

#pragma once

class experimentClass
{
public:
    experimentClass(int x, int y);
    ~experimentClass();

private:
    int age;
    int favoriteNumber;
};

#endif

experimentClass.cpp

#include "experimentClass.h"

experimentClass::experimentClass(int x, int y)
{   
    age = x;
    favoriteNumber = y;    
}

experimentClass::~experimentClass()
{

}

main.cpp

#include <iostream>
#include "experimentClass.h"
using namespace std;

int main(){
    
    int age;
    int favoriteNumber;

    std::cin >> age;
    std::cin >> favoriteNumber;
    experimentClass a(age, favoriteNumber);   
}

In this case, I am able to store 20 into a‘s age, 5 into a‘s favoriteNumber.

However, I want to do this process until it hits the end of input.

So, in this case, I want to create 10 objects with given input, using iteration, and store these object into an array or something.

How can I read them properly so that I can achieve this?

>Solution :

Simply take the logic you already have and put it inside a loop, eg:

#include <iostream>
#include <vector>
#include "experimentClass.h"
using namespace std;

int main() {
    
    int age;
    int favoriteNumber;
    vector<experimentClass> vec;

    while (cin >> age >> favoriteNumber) {
        experimentClass a(age, favoriteNumber);
        vec.push_back(a);
    }
   
    // use vec as needed...
}

Then you can take this a step further by implementing an operator>> for your class, eg:

#ifndef EXPERIMENTCLASS_H
#define EXPERIMENTCLASS_H

#include <istream>

#pragma once

class experimentClass
{
public:
    experimentClass(int x = 0, int y = 0);

    friend std::istream& operator>>(std::istream &is, experimentClass &cls);

private:
    int age;
    int favoriteNumber;
};

std::istream& operator>>(std::istream &is, experimentClass &cls);

#endif
#include "experimentClass.h"

experimentClass::experimentClass(int x, int y)
{   
    age = x;
    favoriteNumber = y;
}

std::istream& operator>>(std::istream &is, experimentClass &cls)
{
    return is >> cls.age >> cls.favoriteNumber;
}
#include <iostream>
#include <vector>
#include "experimentClass.h"
using namespace std;

int main() {
    
    experimentClass a;
    vector<experimentClass> vec;

    while (cin >> a) {
        vec.push_back(a);
    }
   
    // use vec as needed...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading