I have trouble answering the following question. please help with codes for my practice sake.
QUESTION:
The table below shows the average rainfall and temperature during a rain season stored in a text file called Weather.txt

Month Temperature Rainfall
Chingola 23 27
Kitwe 24 27
Ndola 23 26
Kabwe 24 26
Lusaka 22 26
Kafue 24 16
Mazabuka 26 15
Monze 26 12
Choma 27 10
Livingstone 26 10
Write a program that reads this information from the text file, stores the values in parallel arrays and prints the information stored in the arrays to the screen.

PLEASE HELP ASAP!

Recommended Answers

All 4 Replies

It's a simple program. Show us what you have tried so far.

ASAP: Short for "I left my homework until the last minute and now I want someone to do it for me."

Im really sorry I had to sound so bad by the use for ASAP a request. Iam actually learning how these things work. Once more iam so sorry.
THIS below is what i have tried. It is running but I'm mot sure it is using any arrays let alone having any. I will really appreciate codes with some explaination if posible. As I indicated Iam a beginner. infact this is just my fifth day of coding.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{string Town="";
    int temperature,rainfall;
    char ans;
    ofstream outFile;
    outFile.open("Weather.txt");
    if (outFile)
        {
            outFile<<"Month\tTemperature\tRainfall\n";
    do
        {             cout<<"Enter town ";
    cin>>Town;
     cout<<"Enter Temperature ";
        cin>>temperature;
        cout<<"Enter Rainfall ";
        cin>>rainfall;
        outFile<<Town<<"\t"<<temperature<<"\t"<<rainfall<<endl;
        cout<<"Do you want to enter another record? Y/N ";
        cin>>ans;
        }
        while (toupper(ans)=='Y');
        cout<<"Records Saved";
          outFile.close();
          }
          else
            {
                 cout<<"File failed to open";
          }

    if (outFile)
    return 0;
}

To read a file, you need to declare an ifstream, open it, read from it using using >> into the appropriate types and check for eof(), bad(), fail() to detect errors and end of file. Note that >> at EOF sets fail (you didn't get what you asked for) as well as eof but not bad.

Putting the data into parallel arrays is pretty easy. Declare an array for each of the 3 items, and load it as you read using the same index. The arrays need to be big enough or you need to dynamically size them, google for examples.

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