Hey all, first off, I'm NOT looking for answers to my homework by any means. I am rather looking for guidance. I want to understand the material rather than just have the solution handed to me. Therefore, here's my problem: I have a project where I have to read info from a data file, print it out in two separate parallel (1D) arrays, and that's pretty much it for now. Here's my code (remember, I am a beginner with a capital B) so please any advice and/or guidance would help tremendously. Thanks for your help.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

const int NUM_WORDS = 154;

typedef string PigWords[NUM_WORDS];
typedef string EngWords[NUM_WORDS];

void PrintDictionary (int, const PigWords, const EngWords);

int main()
{
    ifstream inFile;

    PigWords pigs;
    EngWords engs;

    inFile.open("pigData.txt");

    if ( !inFile )
    {
        cout << "Error: Data file could not be opened" << endl;
        return (EXIT_FAILURE);
    }



    PrintDictionary (NUM_WORDS, pigs, engs);

    inFile.clear();
    inFile.close();

    return (EXIT_SUCCESS);
}

void PrintDictionary (int NUM_WORDS, PigWords pigs, EngWords engs)
{
    return;
}

Dani AI

Generated

Summary: the file is opened but nothing is ever read into your arrays. Two key steps will fix that: read records into the arrays with an index and a counter, and implement PrintDictionary to print only the items actually read (not the full fixed size). correctly pointed out you need a read loop; 's reminder about code formatting is helpful for future posts.

A minimal, robust read loop (expects an English token then its Pig Latin token per record):

int count = 0;
while (count < NUM_WORDS && (inFile >> engs[count] >> pigs[count])) {
    ++count;
}

This stops when the file ends or when the array is full. If your records are more complex (phrases or punctuation), read lines and parse them with std::istringstream instead.

Implement PrintDictionary so it prints only count entries:

void PrintDictionary(int count, const PigWords pigs, const EngWords engs)
{
    for (int i = 0; i < count; ++i)
        cout << setw(20) << left << engs[i] << pigs[i] << '\n';
}

Call PrintDictionary(count, pigs, engs) from main after the read loop. Using the count avoids printing uninitialized array elements.

Troubleshooting notes: confirm the exact file format (tokens per line), handle blank lines and trailing whitespace, and check the read operation’s return value rather than relying on eof(). If the number of entries is unknown or can exceed NUM_WORDS, prefer std::vector<string> to avoid overflow. Finally, include proper headers (<iomanip>, <sstream> if parsing lines) and close the file after reading.

Recommended Answers

All 3 Replies

Your code doesn't seem to be doing anything with the file once you read it in.

Say I had..

ifstream inData;
inData.open("packets.txt");
string line;

while (getline(inData, line)) 
{
       cout << line << endl;
}

The condition in the while loop remains true as long as the end of the file has not been reached. So in this example it will print out each line of the file until the end of file has been reached.

For example if in packets.txt we had..

My name is bob.
Hello bob!

After this code has printed the second line the condition in the while loop would become false and cause us to fall out of it.

Hopefully this helped you out. You should be able to apply these ideals to your problem.

I'll try it out and get back to you, but anyway, thank you so much for your help. I appreciate it a ton.

Don't forget code tags. It makes it easier for us to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.