Not obtaining data from source file correctly

program done
Last edited on
 
#define MAX_UNI 1000 
Hi,

Line 15 should be :

#define MAX_UNI 1000

But an even better idea is to not use a #define for this purpose. Make a const variable instead, but not a global one, put it in main:

const unsigned int MAX_UNI = 1000;

Notice I have as an unsigned type, this is a good idea for anything involving size, or indeed anything that shouldn't be negative. One could use std::size_t also.

Btw, I found your error by using cpp.sh (the gear icon top right of the code) with all 3 warning levels on. It detected errors before whatever options you were using. So, set your warnings levels to a high level - warnings are your friend :+)

Have you learnt about struct yet? Instead of having lots of parallel arrays, you can have an array of struct.

Good Luck !! :+D

Thank you! Wow i feel, dumb.

And no, I haven't learned struct yet. But will plan to.

I corrected it, and I took your suggestion to make it a const variable, and it worked.

The program compiled and work, but apparently it is not extracting the data correctly.

I edited my original post, to reflect the recent changes and also the current issue.

Trying to get the program to read 'Princeton University' and put it into variable universityname.
Then skip a line and read 'NJ', put it into variable state,
then read 'Princeton', put it into variable city,
then skip a line and read the 4 data points.

I am guessing it has something to do with how I am doing the ignore() functionalities.

The program outputted:
P

Purpose: Universities Analysis
Press any key to continue . . .

The program outputted:
P

Purpose: Universities Analysis
Press any key to continue . . .


That's because those were the only things you sent to std::cout. You haven't sent any data apart from the Purpose to the file ....
I edited my original post, to reflect the recent changes and also the current issue.

Please don't do this, it makes following the thread almost impossible, instead add the modified code into a new post.

In your read loop I would move that first .ignore() to the end of the loop, because the first time through the loop it really isn't required. I also doubt that the second .ignore() is even required.
Ah I am sorry.

So I am still having trouble how to read a text file line by line and extracting certain text into certain variables.

I tried googling, but there is no clear perfect example to do it, and people seems to be struggling also.

For example:

Princeton University
NJ Princeton
41820 8014 0.0740 0.98 0.97
Harvard University
MA Cambridge
43838 19882 0.0580 0.97 0.97

I know the basic structure, which is probably,
inFile >> universityname[count] >> state[count] >> city[count] >> etc, etc...
but how do I 'skip' a line, and then repeat the whole process again?

closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <string>

int main () {
    std::string university;
    std::string state;
    std::string city;
    
    int ID = 0;
    int d1 = 0;
    double d2, d3, d4;
    
    std::ifstream myfile ("read_file.txt");
    if (myfile.is_open())
    {
        while ( getline(myfile, university) )
        {
            myfile >> state;
            myfile.ignore(100, ' '); // <--
            
            getline(myfile, city);
            myfile >> ID >> d1 >> d2 >> d3 >>d4;
            
            std::cout
            << "University: " << university << '\n'
            << "     State: " << state << '\n'
            << "      City: " << city << '\n'
            << "        ID: " << ID << '\n'
            << "        D1: " << d1<< '\n'
            << "        D2: " << d2 << '\n'
            << "        D3: " << d3 << '\n'
            << "        D4: " << d4 << '\n'
            << "-------------------------------\n";
            
            myfile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            //myfile.clear();
        }
        
        myfile.close();
    }
    
    else std::cout << "Unable to open file";
    
    return 0;
}

University: Princeton University
     State: NJ
      City: Princeton
        ID: 41820
        D1: 8014
        D2: 0.074
        D3: 0.98
        D4: 0.97
-------------------------------
University: Harvard University
     State: MA
      City: Cambridge
        ID: 43838
        D1: 19882
        D2: 0.058
        D3: 0.97
        D4: 0.97
-------------------------------
University: Yale University
     State: CT
      City: New Haven
        ID: 45800
        D1: 12109
        D2: 0.069
        D3: 0.99
        D4: 0.98
-------------------------------

This might be a worthwhile pattern to follow. Your breakup into data pieces is probably not the same but that's a matter of adapting it, as for indexing and putting into an array(s).
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/195383/
Topic archived. No new replies allowed.