Reading values from File into a class variable? - c++

I'm trying to write a code that will get values from a file and store them into a class variable. I'm not saving them according to my code. I would really appreciate the help. I don't know where my error is. The first value from the file is the number of class objects there is in total.
class customer {
private:
string *names;
char *plans;
int *hours;
int customerTotal;
public:
string name;
char plan;
int hour;
customer() {
name = "";
plan = ' ';
hour = 0;
customerTotal = 0;
}
void setName(string x) {
names[customerTotal] = x;
}
void setPlan(char x) {
plans[customerTotal] = x;
}
void setHours(int x){
hours[customerTotal] = x;
customerTotal++;
}
void printArray(){
for (int i = 0; i < customerTotal;i++){
cout << names[i] << plans[i] << hours[i] << endl;
}
}
};
int main() {
int numCustomers;
ifstream inFile;
string n;
char p;
int h;
inFile.open("customers.txt");
if (!inFile)
{
cout << "File not found!" << endl << endl;
system("pause");
return 1;
}
customer x;
inFile >> numCustomers;
while (!(inFile.eof())) {
inFile >> n >> p >> h;
x.setName(n);
x.setPlan(p);
x.setHours(h);
}
x.printArray();
inFile.close();
}

Related

Passing array of pointers to object c++

I have to create an array of pointer to class objects.
Then I have with function AddStudent I have to change/create a new student (add his data to pointer address).
In last step function ShowAllStudents has to show all students. But I shows only the last added student.
Every new object is beeing created in array index of 0. I don't know why.
There is my code:
index.html:
#include <iostream>
#include "student.h"
void AddStudent(Student* pointer_array[], int n)
{
int IndexNr{}, id{};
std::string name{}, surname{};
std::cout << "Write IndexNr:";
std::cin >> IndexNr;
for(int i = 0; i < n; i++){
if (IndexNr != pointer_array[i]->Student::IndexNr && pointer_array[i]->Student::name == "none")
{
i = id;
pointer_array[i]->Student::IndexNr = IndexNr;
break;
}
else if (IndexNr == pointer_array[i]->Student::IndexNr) {
i = id;
break;
}
}
std::cout << "Student's name: ";
std::cin >> name;
pointer_array[id]->Student::name = name;
std::cout << "Student's surname ";
std::cin >> surname;
pointer_array[id]->Student::surname = surname;
}
void ShowAllStudents(Student* pointer_array[], int n)
{
for (int i = 0; i < n; i++) {
if (pointer_array[i]->Student::name != "none")
{
std::cout << "<id>: " << pointer_array[i]->Student::IndexNr << " <Name>: " << pointer_array[i]->Student::name << " <Surname>: " << pointer_array[i]->Student::surname << std::endl;
}
else if (pointer_array[i]->Student::name != "none" && i == (n - 1))
std::cout << "<id>: no data" << std::endl;
}
}
int main()
{
int n{10};
Student* pointer_array[10];
for (int i = 0; i < n; i++)
pointer_array[i] = new Student;
AddStudent(pointer_array, n);
AddStudent(pointer_array, n);
AddStudent(pointer_array, n);
ShowAllStudents(pointer_array, n);
return 0;
}
student.h
#include <iostream>
#ifndef STUDENT_H_
#define STUDENT_H_
class Student {
public:
int IndexNr;
std::string name;
std::string surname;
Student() {
IndexNr = 0;
name = "none";
surname = "none";
}
};
#endif
It shows only the last edited student.

exception unhandled violation access

I was using a dynamic array to expand it in the function assignbook and I have no idea why when I delete[] the array it gives me unhandled access violation.
this is the main
int main() {
Translator t;
Book b;
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
t.AssignBook(b);
cout << t.getNumBooks();
}
and this is the implementation of translator class :
void Translator::AssignBook(Book& x) {
if (numBooks < size) {
translatebooks[numBooks] = x;
numBooks++;
}
else {
size += 10;
Book* t = new Book[size];
for (int i = 0; i < numBooks; i++) {
t[i] = translatebooks[i];
}
delete[] translatebooks;
translatebooks = t;
translatebooks[numBooks] = x;
cout << "The array is too small so it expanded from size " << numBooks << " to " << size << endl;
numBooks++;
}
}
and this is the error:
I'm so done with this error, please help. I do delete the array again in the destructor, though.
When I remove the delete[] statement it works, but still it gives me the weird accesss thing. :/
there's the whole code :
#include<iostream>
#include<string>
#define size1 2
using namespace std;
class Date {
private:
int Day;
int Month;
int Year;
public:
Date();
Date(int, int, int);
int getDay() const;
int getMonth() const;
int getYear() const;
void setDate(int, int, int);
void setMonth(int);
void setYear(int);
void setDay(int);
void print() const;
};
class Person {
private :
int PID;
string Pname;
public :
Person();
Person(int, string);
int getPID() const;
string getPname() const;
void setPID(int);
void setPerson(int, string);
void setPname(string);
void print() const;
};
class Book {
private:
int ISBN;
string BookTitle;
int NumPages;
string Language;
Date PublishingDate;
int NumAuthors;
Person* Authors;
public:
Book();
Book(int, string, int, string, Date&, int);
~Book();
void setISBN(int);
void setBookTitle(string);
void setLanguage(string);
void setNumPages(int);
void setPublishDate(Date&);
void setNumberAuthors(int);
void setAuthor(Person&);
int getISBN() const;
string getBookTitle() const;
string getLanguage() const;
int getNumPages() const;
Date getPublishDate() const;
int getNumberAuthors() const;
void print() const;
};
class Employee {
private:
Person Ename;
double salary;
protected:
int Rank;
public:
Employee();
Employee(Person&, double ,int);
void setEname(Person&);
void setSalary(double);
void setRank(int);
Person getEname() const;
int getRank() const;
virtual bool isManager() ;
virtual double getNetSalary();
double getSalary() const;
void print() const;
virtual void AssignBook(Book&);
};
class Translator:public Employee{
private:
int Experience;
int numBooks;
Book *translatebooks;
int size;
public :
void setExperience(int);
void AssignBook(Book&);
void unAssignBook(int ISBN);
double getNetSalary() const;
int getExperience() const;
int getNumBooks() const;
Translator();
Translator(Person&, double, int,int);
~Translator();
bool isManager();
void print() const;
};
class Manager : public Employee {
private:
bool certified;
public :
void setCertified(bool x);
bool getCertified();
Manager();
Manager(Person&, double, int,bool);
~Manager();
bool isManager();
};
Employee* ReadOneEmployee() {
int x;
cout << "Please Enter 1 for Translator \n 2 for Manager\n";
cin >> x;
if (x == 1) {
cout << "Name :" << endl;
string name;
cin >> name;
cout << "Rank: \n";
int Rank;
cin >> Rank;
cout << "Experience\n";
int Experience;
cin >> Experience;
cout << "Salary \n";
double salary;
cin >> salary;
cout << "ID =\n";
int ID;
cin >> ID;
Person P(ID, name);
return new Translator(P, salary, Rank, Experience);
}
else if (x == 2) {
cout << "Name :" << endl;
string name;
cin >> name;
cout << "Rank: \n";
int Rank;
cin >> Rank;
cout << "Certified:\n";
bool Experience;
cin >> Experience;
cout << "Salary \n";
double salary;
cin >> salary;
cout << "ID =\n";
int ID;
cin >> ID;
Person P(ID, name);
return new Manager(P, salary, Rank, Experience);
}
}
double Tax = 0.2;
int main() {
Book books[200];
Person z(2,"Meh");
Person x(2, "xd");
books[0].setAuthor(z);
books[0].setAuthor(x);
books[0].setNumPages(5);
Employee *list[size1];
for (int i = 0; i < size1; i++) {
list[i] = ReadOneEmployee();
}
int booksnum = 123;
int i = 0;
while (booksnum >= 0) {
if (!list[i]->isManager() && i<size1) {
list[i]->AssignBook(books[booksnum]);
i++;
}
else if (i >= size1) {
i = 0;
}
booksnum--;
}
for (int i = 0; i < size1; i++) {
cout << "Name :" << list[i]->getEname().getPname() << endl;
cout << "Salary =" << list[i]->getSalary() << endl;
cout << "Net Salary =" << list[i]->getNetSalary() << endl;
}
double totalN = 0,totalS=0;
for (int i = 0; i < size1; i++) {
totalN += list[i]->getNetSalary();
totalS += list[i]->getSalary();
}
cout << "Total Salary is :" << totalS << endl;
cout << "Total Net Salary is :" << totalN << endl;
int totalP = 0;
for (int i = 0; i < 200; i++) {
totalP += books[i].getNumPages();
}
cout << "Total Pages is :" << totalP << endl;
int max = 0;
for (int i = 0; i < 200; i++) {
if (books[i].getNumberAuthors() > max) {
max = books[i].getNumberAuthors();
}
}
for (int i = 0; i < 200; i++) {
if (books[i].getNumberAuthors() == max) {
books[i].print();
}
}
}
//i used regions to make it easier to read!
#pragma region Date_Implementation
Date::Date() {
Day = Month = 1;
Year = 1900;
}
Date::Date(int x,int y,int z) {
setDate(x, y, z);
}
int Date::getDay() const {
return Day;
}
int Date::getMonth() const {
return Month;
}
int Date::getYear() const {
return Year;
}
void Date::setDate(int x, int y, int z) {
setDay(x);
setMonth(y);
setYear(z);
}
void Date::setMonth(int x) {
if (x > 0 && x <= 12) {
Month = x;
}
else {
cout << "Invalid Value for the Months and it's been set to 1\n";
Month = 1;
}
}
void Date::setYear(int x){
if (x >= 1900) {
Year = x;
}
else {
cout << "Invalid Value for the Years and it's been set to 1900\n";
Year = 1900;
}
}
void Date::setDay(int x) {
if (x > 0 && x <= 31) {
Day = x;
}
else {
cout << "Invalid Value for the Days and it's been set to 1\n";
Day = 1;
}
}
void Date::print() const {
cout << "The Date is: " << Day << "/" << Month << "/" << Year << endl;
}
#pragma endregion
#pragma region Person_Implemntation
Person::Person() {
Pname = "";
PID = 1;
}
Person::Person(int x, string y) {
setPerson(x, y);
}
void Person ::setPerson(int x, string y) {
setPname(y);
setPID(x);
}
int Person::getPID() const {
return PID;
}
string Person::getPname() const {
return Pname;
}
void Person::setPID(int x) {
if (x > 0) {
PID = x;
}
else {
cout << "Invalid Value for PID it's been set to 1\n";
PID = 1;
}
}
void Person::setPname(string x) {
if (x != "") {
Pname = x;
}
else {
cout << "Invalid Value for Pname it's been set to \"\"\n";
Pname="";
}
}
void Person::print() const {
cout << "Person's name = " << Pname << endl;
cout << "Person's ID = " << PID << endl;
}
#pragma endregion
#pragma region Book_Implementation
Book::Book() {
ISBN = NumPages = 1;
Authors = new Person[3];
BookTitle = "";
Language = "English";
NumAuthors = 0;
PublishingDate.setDate(1, 1, 1900);
}
Book::Book(int x, string y, int z, string c, Date& v, int b) {
setISBN(x);
setBookTitle(y);
setNumPages(z);
setLanguage(c);
setPublishDate(v);
NumAuthors = 0;
}
Book::~Book() {
// cout << "Book " << BookTitle << " Destructed\n";
delete[] Authors;
}
void Book::setISBN(int x) {
if (x > 0) {
ISBN = x;
}
else {
cout << "Invalid Value for ISBN it's been set to 1\n";
ISBN = 1;
}
}
void Book::setBookTitle(string x) {
if (x != "") {
BookTitle = x;
}
else {
cout << "Invalid value for Book title it's been set to \"\"\n";
BookTitle = "";
}
}
void Book::setLanguage(string x) {
if (x == "English" ||x=="Arabic") {
BookTitle = x;
}
else {
cout << "Invalid value for Book's Language it's been set to English\n";
BookTitle = "English";
}
}
void Book::setNumPages(int x) {
if (x > 0) {
NumPages = x;
}
else {
cout << "Invalid Value for Number of Pages it's been set to 1\n";
NumPages = 1;
}
}
void Book::setPublishDate(Date& x) {
PublishingDate.setDate(x.getDay(), x.getMonth(), x.getYear());
}
void Book::setNumberAuthors(int x) {
if (x > 0 && x<=3) {
NumAuthors = x;
}
else {
cout << "Invalid Value for Number of authors it's been set to 1\n";
NumAuthors = 1;
}
}
void Book::setAuthor(Person& x) {
if (NumAuthors < 3) {
Authors[NumAuthors].setPerson(x.getPID(), x.getPname());
NumAuthors++;
}
else cout << "Can't set an author cause the array is full!!\n";
}
int Book::getISBN() const {
return ISBN;
}
string Book::getBookTitle() const {
return BookTitle;
}
int Book::getNumPages() const {
return NumPages;
}
Date Book::getPublishDate() const {
return PublishingDate;
}
int Book::getNumberAuthors() const {
return NumAuthors;
}
string Book::getLanguage() const {
return Language;
}
void Book::print() const {
cout << "ISBN = " << ISBN << endl;
cout << "BookTitle = " << BookTitle << endl;
cout << "Number of Pages = " << NumPages << endl;
cout << "Language = " << Language << endl;
cout << "The Authors that were set :\n";
for (int i = 0; i < NumAuthors; i++) {
Authors[i].print();
}
}
#pragma endregion
#pragma region Employee_Implementation
Employee::Employee() {
Rank = salary = 0;
Ename.setPerson(1, "Default");
}
Employee::Employee(Person& x, double y, int z) {
setEname(x);
setRank(z);
setSalary(y);
}
void Employee::setEname(Person& x) {
Ename.setPerson(x.getPID(), x.getPname());
}
void Employee::setSalary(double x) {
if (x > 0) {
salary = x;
}
else {
cout << "Invalid Value for Salary it's been set to 0\n";
salary = 0;
}
}
void Employee::setRank(int x) {
if (x >= 0) {
Rank = x;
}
else {
cout << "Invalid Value for Rank it's been set to 0\n";
Rank = 0;
}
}
Person Employee::getEname() const {
return Ename;
}
int Employee::getRank() const {
return Rank;
}
double Employee::getSalary() const {
return salary;
}
double Employee::getNetSalary() {
double totalS = salary + (Rank * 320);
double Net_Salary = totalS - totalS * Tax;
return Net_Salary;
}
void Employee::print() const {
Ename.print();
cout << "Rank = " << Rank<<endl;
cout << "salary = " << salary << endl;
}
bool Employee :: isManager() {
return false;
}
void Employee::AssignBook(Book& x) {
cout << "Can't cause it's just an Employee!";
}
#pragma endregion
#pragma region Translator_Implementation
void Translator::setExperience(int x) {
if (x >= 0) {
Experience = x;
}
else
{
cout << "Invalid Value for The Experience it's been set to 0\n";
Experience = 0;
}
}
void Translator::AssignBook(Book& x) {
if (numBooks < size) {
translatebooks[numBooks] = x;
numBooks++;
}
else {
size += 10;
Book* t = new Book[size];
for (int i = 0; i < numBooks; i++) {
t[i] = translatebooks[i];
}
delete[] translatebooks;
translatebooks = t;
translatebooks[numBooks] = x;
cout << "The array is too small so it expanded from size " << numBooks << " to " << size << endl;
numBooks++;
}
}
void Translator::unAssignBook(int ISBN) {
for (int i = 0; i < numBooks; i++) {
if (translatebooks[i].getISBN() == ISBN) {
for (int j = i; j < numBooks-1; j++) {
translatebooks[j] = translatebooks[j + 1];
}
numBooks--;
break;
}
}
}
double Translator::getNetSalary() const {
int total = 1;
for (int i = 0; i < numBooks; i++) {
total += translatebooks[i].getNumPages();
}
return getNetSalary() + 5 * total;
}
int Translator::getExperience() const {
return Experience;
}
int Translator::getNumBooks() const {
return numBooks;
}
Translator::Translator():Employee() {
Experience = 0;
numBooks = 0;
size = 10;
translatebooks = new Book[size];
}
Translator::Translator(Person& x, double y, int z, int c) :Employee(x, y, z) {
setExperience(c);
numBooks = 0;
size = 10;
translatebooks = new Book[size];
}
Translator::~Translator() {
delete[] translatebooks;
translatebooks = NULL;
}
bool Translator:: isManager() {
return false;
}
void Translator::print() const{
Employee::print();
cout << "Experience = " << Experience << endl;
cout << "Number of books = " << numBooks << endl;
cout << "And they are :\n";
if (numBooks == 0) cout << "None\n";
else
for (int i = 0; i < numBooks; i++) {
translatebooks[i].print();
}
}
#pragma endregion
#pragma region Manger_Implementation
void Manager::setCertified(bool x) {
certified = x;
}
bool Manager::getCertified() {
return certified;
}
Manager::Manager():Employee() {
certified = false;
}
Manager::Manager(Person& x, double y , int z, bool c):Employee(x,y,z) {
certified = c;
}
Manager::~Manager() {
cout << "Manager Destructed\n";
}
bool Manager::isManager() {
return true;
}
#pragma endregion
You have a rule of 0/3/5 violation. You have no copy constructor for Book. So when your code does:
for (int i = 0; i < numBooks; i++) {
t[i] = translatebooks[i];
}
This invokes the default copy constructor for Book. This is a disaster because the destructor looks like this:
Book::~Book() {
// cout << "Book " << BookTitle << " Destructed\n";
delete[] Authors;
}
So the default copy constructor gives both Book objects the same value for Authors. Whichever one is destroyed first deletes it. That leaves the other one with a pointer to nowhere.
Then this code runs when you expand the array:
delete[] translatebooks;
This destroys the books you copied, destroying the Authors arrays in the copies.
Moral: Don't use raw pointers. Don't use raw arrays. We have lots of classes that make all this stuff easy, why make it hard?

c++ How to Reading file in to dynamic array and accessing array elements

I'm learning how to use fstream with dynamic array, the program supposed to print out all the data in txt file and can do something like sorting, delete gender or calculate average grade. But turned out code is all messy and arrays are hard to access.
This is the text file use to practice.
Code:
struct studentData{
string branch;
string name;
char gender;
double grade;
int number;
int total;
};
int main() {
int line_count = 0;
ifstream file_in;
int couter = 0;
file_in.open("student.txt");
if(!file_in.good())
{
cout << "Eror, could not open the file." << endl;
file_in.clear();
return -1;
}
line_count = openFileTest(file_in);
file_in.clear();
file_in.seekg(ios::beg);
studentData* p_studentData = new studentData[line_count];
loadStudentData(file_in, p_studentData,couter);
displayStudentData(p_studentData, line_count,couter);
delete [] p_studentData;
file_in.close();
return 0;
}
int openFileTest(ifstream& file_in)
{
string temp;
int linecount = 0;
while(getline(file_in, temp))
{
linecount ++;
}
return linecount;
}
void loadStudentData(ifstream& file_in,studentData* p_studentData,int couter)
{
int temp ;
file_in >> p_studentData -> total ;
temp = p_studentData->total;
for(int i=0;i<temp;i++)
{
file_in >> p_studentData->branch >> p_studentData->number ;
for(int k=0;k<p_studentData[i].number;k++)
{
file_in >> p_studentData[k].name >> p_studentData[k].gender >> p_studentData[k].grade ;
}
p_studentData++;
}
return;
}
void displayStudentData(studentData* p_studentData, int count_line,int couter)
{
cout << p_studentData->total << endl;
int temp = p_studentData->total ;
for(int i=0;i<temp;i++)
{
cout << p_studentData[i].branch << " " ;
cout << p_studentData[i].number << endl;
for(int j=0;j<p_studentData[i].number;j++)
{
cout << p_studentData[j].name << " " << p_studentData[j].gender << " " << p_studentData[j].grade << endl;
}
}
return;
}
The output I get is:
It appears you are not being careful to track where you are in the file. I don't know why arrays[] are so popular for these kinds of assignments when a vector would do, but notice that you are missing a level of abstraction:
your code: array of student
assignment: array of subject; each subject is an array of student
It is often very helpful to use types to keep track of this:
struct student
{
// as above
};
struct subject
{
string name;
int number_of_students;
student* students;
};
struct all_subjects
{
int number_of_subjects;
subject* subjects;
};
Using this, we can work on deciphering the file:
all_subjects load_all_subjects()
{
ifstream f( ... );
...
all_subjects all;
f >> all.number_of_subjects;
all.subjects = new subject[ all.number_of_subjects ];
for (int n = 0; n < all.number_of_subjects; n++)
load_one_subject( f, all.subjects[ n ] );
return all_students;
}
void load_one_subject( std::ifstream& f, subject& one_subject )
{
f >> one_subject.name;
f >> one_subject.number_of_students;
one_subject.students = new student[ one_subject.number_of_students ];
for (int n = 0; n < one_subject.number_of_students; n++)
load_one_student( f, one_subject.students[ n ] );
}
And so on. Good luck!

C++ Console is blank when program is run

This might be a stupid question I'm still very new to coding. For my CS class I was given code for the basics of a boardgame. When I try to run the code it just comes up blank in my console, I tried to print "check" at the very beginning of main but still nothing prints to the console. No errors come up
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
class square {
private:
int move;
string message;
char symbol;
public:
square();
void print();
int action();
void set(int, char, string);
};
void print_board(square[], int, int);
void read_board(square[]);
void check_position(int &);
const int board_length = 20;
int main() {
cout << "check";
int current_player = 1, roll;
int player1_position = 0, player2_position = 0;
square the_board[board_length];
srand(time(NULL));
read_board(the_board);
print_board(the_board, player1_position, 1);
print_board(the_board, player2_position, 2);
do {
cout << "\n\n\nPlayer " << current_player << " type enter to roll.\n";
cin.ignore();
roll = 1 + (rand() % 5);
cout << "Player " << current_player << " rolled a " << roll << ".\n";
if (current_player == 1) {
player1_position += roll;
check_position(player1_position);
player1_position += the_board[player1_position].action();
check_position(player1_position);
} else {
player2_position += roll;
check_position(player2_position);
player2_position += the_board[player2_position].action();
check_position(player2_position);
}
print_board(the_board, player1_position, 1);
print_board(the_board, player2_position, 2);
current_player = (current_player % 2) + 1;
} while ((player1_position < board_length-1) && (player2_position < board_length - 1));
current_player = (current_player % 2) + 1;
cout << "\nPlayer " << current_player << " Wins!!!\n";
cin.ignore();
return 0;
}
void read_board(square b[]) {
ifstream infile;
infile.open("game.txt");
int square_number, square_move;
string square_message;
char square_symbol;
while (!infile.eof()) {
infile >> square_number >> square_move >> square_symbol;
getline(infile, square_message);
if (square_number < board_length) {
b[square_number].set(square_move, square_symbol, square_message);
}
}
}
void print_board(square b[], int player_position, int player_number) {
for (int i=0; i < board_length; i++) {
if (i != player_position) {
b[i].print();
} else {
cout << player_number;
}
}
cout << "Goal\n";
for (int i=0; i < board_length; i++) {
cout << "-";
}
cout << "\n";
}
void check_position(int &p) {
if (p < 0) {
p = 0;
}
if (p >= board_length) {
p = board_length - 1;
}
}
square::square() {
symbol = ' ';
move = 0;
message = "";
}
int square::action() {
cout << message << endl;
return move;
}
void square::print() {
cout << symbol;
}
void square::set (int m, char s, string a_message) {
move = m;
symbol = s;
message = a_message;
}
Modify you read_board() to
void read_board(square b[]) {
ifstream infile;
infile.open("game.txt");
int square_number, square_move;
string square_message;
char square_symbol;
while (infile >> square_number >> square_move >> square_symbol) {
getline(infile, square_message);
if (square_number < board_length) {
b[square_number].set(square_move, square_symbol, quare_message);
}
}
}
Change cout<<"check"; to cout<<"check"<<endl;
Without the new line added, the out buffer is not flushed before your code gets hung in your read_board function

c++ why programs reads only first value

program should read numbers from data.txt file and then print them, but prints only first value good, all other values are the same -9.25596e+061
const char CDfv[] = "Data.txt";
const int cmax = 1000;
//---------------------------------------------------------
void read_print_data (double A[], int& x);
//--------------------------------------------------------
int main()
{
ofstream fr;
double A[cmax];
int x;
ifstream fv ("Data.txt");
read_print_data(A,x);
system("Pause");
return 0;
}
void read_print_data (double A[], int& x)
{
ifstream fd(CDfv);
fd >> x;
for (int i = 0; i < x; i++)
{
fd >> A[i];
cout << i + 1 << " " << A[i] << endl;
fd.close();
}
}
You are closing the stream prematurely, You need to move
fd.close();
to be outside the loop.
So the code should look like
for (int i = 0; i < x; i++)
{
if (fd >> A[i]) {
cout << i + 1 << " " << A[i] << endl;
} else {
// Error has occurred
}
}
fd.close();