Question

So my test.h file has: #include <iostream> #include <fstream> using namespace std; class test { private:...

So my test.h file has:

#include <iostream>
#include <fstream>

using namespace std;

class test
{
private:
   int data[];

public:
   test(string filename);
};

My test.cpp file has:

#include "sort.h"

test::test(string filename)
{
   ifstream inFile;

   inFile.open(filename);

   int iter = 0;

   while(!inFile.eof())
   {
       cin >> data[iter];
       iter++;
   }

   int numberOfNumbers = iter;

   for (iter = 0; iter < numberOfNumbers; iter++)
       cout << data[iter] << " ";

   inFile.close();
}

my main.cpp has:

#include "sort.h"

int main()
{
   sort numbers("test.txt");
}

the numbers in my test.txt file are

10

20

30

40

50

My question is how do I populate the array in my class with the numbers in the text file and print it using the constructor. When I run my code right now, nothing shows up on the screen.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Firstly, You are including "sort.h" which you haven't defined. Check if that is not causing any problem.

Now I can see this code can't populate your array.
Because you are using cin which is used to take inputs from command line or input buffer.

You can use "filename >> integer" which stores next integer from file into specified integer.

For example, in your code write like this:
int iter=0;
int input_int;
while(inFile >> input_int) {
   data[iter]=input_int;
   iter++;
}

This will work for populating your array.
Here's how I debugged your code using data you spoecified.

//So my test.h file has:

#include <iostream>
#include <fstream>

using namespace std;

class test
{
private:
int data[100];

public:
test(string filename);
};

//My test.cpp file has:

//#include "sort.h"

test::test(string filename)
{
ifstream inFile;

inFile.open(filename);

int iter = 0;
   int input_int;
while(inFile >> input_int)
{
data[iter]=input_int;
iter++;
}

int numberOfNumbers = iter;

for (iter = 0; iter < numberOfNumbers; iter++)
cout << data[iter] << " ";

inFile.close();
}

//my main.cpp has:

//#include "sort.h"

int main()
{
// sort numbers("test.txt");
test("test.txt");
}

Another thing, Don't forget to initialize the size of array as that might cause error.

Hope you got it... :)

Add a comment
Know the answer?
Add Answer to:
So my test.h file has: #include <iostream> #include <fstream> using namespace std; class test { private:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • How do I complete my code using an external file? External file: data.txt //the number 6...

    How do I complete my code using an external file? External file: data.txt //the number 6 is in the external file Incomplete code: #include <iostream> #include <fstream> using namespace std; class example {    int data[1]; public:    example();    void read(ifstream &); }; example::example() {    ifstream infile;    infile.open("data.txt");    } void example::read(ifstream &infile) {    infile >> data[1];    cout << data[1] << endl;    } int main() {    example example, read; //system("pause");    return 0;...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name;...

    employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name; std::string address; std::string phoneNum; double hrWage, hrWorked; public: Employee(int en, std::string n, std::string a, std::string pn, double hw, double hwo); std::string getName(); void setName(std::string n); int getENum(); std::string getAdd(); void setAdd(std::string a); std::string getPhone(); void setPhone(std::string p); double getWage(); void setWage(double w); double getHours(); void setHours(double h); double calcPay(double a, double b); static Employee read(std::ifstream& in); void write(std::ofstream& out); }; employee.cpp ---------- //employee.cpp #include...

  • This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType...

    This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *next;    double value; }; void createList(nodeType*& first, nodeType*& last, ifstream& inf); void printList(nodeType* first); int main() {    nodeType *first, *last;    int num;    ifstream infile;    infile.open("InputIntegers.txt");    createList(first, last, infile);    printList(first);    infile.close();    system("pause");    return 0; } void createList(nodeType*& first, nodeType*& last, ifstream& infile) {    int number;...

  • C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <...

    C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) {    // Add your code here.    string line;    getline(inFile, line);    int sp = 0;    for (int i = 0; i < line.size(); i++)    {        if (line[i] == ' ')            sp++;    }    sp++;    return sp; } int measureLines(ifstream& inFile) {    // Add your code here.    string line;    int n = 0;    while (!inFile.eof())    {        getline(inFile,...

  • Thanks in advance: // File: main.cpp #include <iostream> #include <fstream> #include <iomanip> using namespace std; int...

    Thanks in advance: // File: main.cpp #include <iostream> #include <fstream> #include <iomanip> using namespace std; int recursiveCount = 0; void triangle(const char drawChar, const int maxHeight, const int currentHeight); int main() { char drawChar = '*'; recursiveCount = 0;    cout.fill(drawChar); triangle(drawChar, 5, 1); cout.fill(' '); return 0; }// end main() void triangle(const char drawChar, const int maxHeight, const int currentHeight) { /* DO NOT edit code */ cout.fill(drawChar); recursiveCount += 1; /* END of do not edit */ /*...

  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

  • 4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std;...

    4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std; int main() { string playerName; cout << "Enter name"; cin >> playerName; cout << endl « playerName; return 0; } a. Tom - Sawyer b. Tom Sawyer c. Tom d. Sawyer 5) Which XXX generates "Adam is 30 years old." as the output? #include <iostream> using namespace std; int main() { string name = "Adam"; int age = 30; XXX return 0; } a....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT