CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    4

    Unhappy Trouble with array of structures

    Hey guys, i'm still trying to get the hang of the this programming thing so bare with me here. i'm trying figure out a way to pull out the integers in file, the file contains integers and chars, and put them in an array of 12 structures i'm able to read in the file and display the file, but i'm honestly not sure what the next logical step is... here is what i have so far.



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

    const int A_YEAR = 12;

    struct months

    {

    int landed;
    int departed;
    int most;
    int least;

    };

    int main()

    {
    months air[12];
    ifstream inFile;
    string filename;
    string data;
    char input[100000];
    int test;




    cout << "Enter the name of the input file ";
    getline(cin, filename);
    inFile.open(filename.c_str());

    int count = 0;
    while (getline(inFile, data))
    {
    cout << data << endl;
    count ++;
    }














    system("pause");
    return 0;
    }



    any help would be great

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Trouble with array of structures

    What else does your program need to do?

  3. #3
    Join Date
    Oct 2010
    Posts
    4

    Re: Trouble with array of structures

    it also needs to take the numbers and find averages, totals and minimums. this also confuses me becuase i'm not sure how to choose what numbers i need to find these things. the average will be made up of a certain set and the totals of a certain set ect.

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Trouble with array of structures

    Copy and paste an example of file contents. Your question is still much too vague for anyone to give you any serious tips.

  5. #5
    Join Date
    Oct 2010
    Posts
    4

    Re: Trouble with array of structures

    Month: January
    landed: 270
    departed: 265
    greatest day: 13
    least day: 1
    Month: February
    landed: 245
    departed:244
    greatest day: 12
    least day: 0
    Month: March
    landed: 280
    departed: 260
    greatest day: 12
    least day: 1
    Month: April
    landed: 305
    departed: 307
    greatest day: 15
    least day: 0
    Month: May
    landed: 300
    departed: 301
    greatest day: 14
    least day: 2
    Month: June
    landed: 325
    departed: 324
    greatest day: 19
    least day: 3
    Month: July
    landed: 378
    departed: 382
    greatest day: 21
    least day: 1
    Month: August
    landed: 350
    departed: 352
    greatest day: 17
    least day: 5
    Month: September
    landed: 290
    departed: 287
    greatest day: 13
    least day: 0
    Month: October
    landed: 292
    departed: 294
    greatest day: 16
    least day: 3
    Month: November
    landed: 250
    departed: 253
    greatest day: 18
    least day: 2
    Month: December
    landed: 120
    departed: 127
    greatest day: 5
    least day: 1

    this is an example of the txt i'm trying to use, i'm trying to add up all the landed portions, all the departed portions and all the greatest days portion. then i want to find the lowest number in the least days column.

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Trouble with array of structures

    Well it looks like your input file is a fairly simple set of key/value pairs separated by a delimiter, ':'. There are many ways to approach this but the first logical step is to parse the file and build the data structure array. You could read in the file one line at a time using getline which you already do. You could also use the member functions of std::string to parse each string and tokenize it into the key/value pair. This could be somewhat tricky because each data structure has exactly 5 pieces of data that you have to keep together. Search for the colon within each string. The data to the left is the key and the data to the right is the value. You can use substr and find member functions to accomplish this. For the value part you have to convert the string to an integer and you can easily write your own template function to do this. I think that you will find the following links helpful.

    http://www.cplusplus.com/reference/string/string/
    http://www.cplusplus.com/forum/articles/9645/
    http://www.codeguru.com/forum/showthread.php?t=231054

    Use an incremental approach. Consider the different parts of the process and try to break the problem down into smaller, logical pieces. This will help you to identify the functions and/or classes that you will need to accomplish this. For instance, you need to be able to tokenize a string into two parts. You need to be able to convert a string into a number. You need a loop that will fill each data structure as well as a loop to fill in each piece of the data structure. The month will have to be converted from a string to a number (that is January - 1, February - 2, March - 3, etc.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured