1

I'm designing a program code that when given a few select Airport codes it determines the sunrise and sunset times of the week in that given area.

I'd like to point out that because it is for a Advanced C++ Class, I don't want to be handed code. I'd to be shown how to do it, not have someone do it for me.

I have a file in this case cityinfo.txt that contains this information:

APN
 45.07   83.57   E
ATL
 33.65   84.42   E
DCA
 38.85   77.03   E
DEN
 39.75  104.87   M
DFW
 32.90   97.03   C
DTW
 42.23   83.33   E
GRR
 42.88   85.52   E
JFK
 40.65   73.78   E
LAF
 40.42   86.93   E
LAN
 42.77   84.60   E
LAX
 33.93  118.40   P
MBS
 43.53   84.08   E
MIA
 25.82   80.28   E
MQT
 46.53   87.55   E
ORD
 41.98   87.90   C
SSM
 46.47   84.37   E
TVC
 44.73   85.58   E
YYZ
 43.67   79.63   E

The output is like running it does not have the brakes after each time zone code (e,z,c, etc.)

So i have two questions about this:

  • Do I need to format the code when i put it in my string variable, it doesn't need to be outputted just referenced?
  • How can i format it if needed?

Seem to have found the answer for me using the help of you guys are from the public sources of the c++ find function:

    ifstream fileIn;
    string airportCode, lat, longitude, timeZone, line;
    size_t pos;

    cout << "Please Enter an Airport Code: ";
    cin >> airportCode;

    fileIn.open("cityinfo.txt");
    if (fileIn.is_open())
    {
        while (fileIn.good())
        {
            getline(fileIn, line);
            pos = line.find(airportCode);
            if (pos != string::npos)
            {

                fileIn >> lat >> longitude >> timeZone;
                break;
            }

        }

    }
user2311215
  • 39
  • 1
  • 3
  • 10

1 Answers1

1

That's a pretty cleanly formatted file.

You should do fine with something like:

#include <fstream>

ifstream my_ifstream("data_file.txt");  // Create fstream from file.

while ( my_ifstream )
{
    std::string airport;
    double rise;
    double set;
    std::string tz;

    my_ifstream >> airport >> rise >> set >> tz;
    // Store this data somewhere...
}

Have you tried anything like that yet?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Im sorry, with my previous c++ class we barely touched into the fstream library. I'm not sure I understand what that does? Does it just stop at whitespace and the next set of >>'s skips the white space and reads until the whitespace after that? Also not so important to you but the sets of numbers are lat and long. – user2311215 Jan 15 '15 at 04:11
  • @user2311215 if you've used `std::cin`, it uses the same rules to read data from a file. See my edit. – Drew Dormann Jan 15 '15 at 04:15
  • Oh ok I see what it does. same as cin but reads it from the file. what do you mean about storing it somewhere doesn't it get stored in the variables (in your example: airport, rise, set, and tz)? – user2311215 Jan 15 '15 at 04:19
  • 1
    it's in a while loop so you're going to overwrite the values. Need to store them somewhere so you don't lose them. – Rubix Rechvin Jan 15 '15 at 04:21
  • @user2311215 Like any C++ variable, they will be stored until they leave scope (the end of the loop). – Drew Dormann Jan 15 '15 at 04:21
  • is there a function to search through the document first say I ask for a airport code, the user enters JFK. Can I instead search for "JFK" and when found record the rest of the information? – user2311215 Jan 15 '15 at 04:23
  • @user2311215 yes, although this is turning into a forum-style discussion or tutoring session. I'm going to stop responding here. Although if you have a new question you are free to post it and, honestly, get more eyes on it than you would here. Good luck! – Drew Dormann Jan 15 '15 at 04:26
  • @user2311215 You're obviously a _help vampire_! Show efforts of what you've been trying, and post about problems where you're stuck in particular. – πάντα ῥεῖ Jan 15 '15 at 04:27
  • @πάνταῥεῖ please check my original edit and judge that. My judgement is that it's fair, and I'll stick by that choice. Respectfully. On a tangent, I hope you're well... – Drew Dormann Jan 15 '15 at 04:34
  • Made a edit with what seems like to be the solution after i break back into main the values are kept, seem to work well. Does anyone see anything wrong with my retrieval code? – user2311215 Jan 15 '15 at 04:39
  • @DrewDormann I'm pretty well, THX for asking. Not that any of you're answers weren't right. I've just got doubts they're worth it on a such poorly asked question (and follow up questions in comments). Just kick such crap. It doesn't help anyone ... – πάντα ῥεῖ Jan 15 '15 at 04:39
  • @user2311215 _"Does anyone see anything wrong with my retrieval code?"_ 1st spot: [`while (fileIn.good())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – πάντα ῥεῖ Jan 15 '15 at 04:42
  • I'd like to point you to the lil greyed out code before you type a comment "Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”." From the developers of the site, (or the webmaster whoever made the decision to put that there) the comments are for asking for more information. I didn't understand his answer so i asked for some elaboration. I guess I don't see what I did wrong. – user2311215 Jan 15 '15 at 04:51
  • @user2311215 _"I guess I don't see what I did wrong."_ There's a number of things you seriously did wrong in your question (still). I've been voting to close it straight away ... – πάντα ῥεῖ Jan 15 '15 at 04:53
  • @user2311215 we learn by failing. See how you can be a better member by understanding what you read here. Good luck! – Drew Dormann Jan 15 '15 at 04:56
  • Instead of just telling me that i did wrong, tell me what I did wrong. _"Confession is always weakness. The grave soul keeps its own secrets, and takes its own punishment in silence."_ – user2311215 Jan 15 '15 at 04:57
  • @user2311215 multiple high-level folk here were noting that it was *Unclear What You Were Asking*. Which usually means it's less of a programming question and more of a "just tell me what to do". – Drew Dormann Jan 15 '15 at 05:00
  • I even said I didn't want to be told what to do I wanted to be shown how to do it. A C++ teacher is not gonna write all my programs but he/she will show the skills I need to do it myself – user2311215 Jan 15 '15 at 05:04