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.
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?