Read Characters From the User Untill Eof

Input file into array until eof. Assist!

I am having an incredibly difficult fourth dimension wrapping my mind effectually this concept. Any assist is greatly appreciated.

I am request for aid in learning how to read an input file INTO an assortment that is not specified on how many values there are, Then until EOF(stop of file). What I take is this and it doesn't seem to display the file:

                          1
2
3
four
five
6
7
8
9
10
11
12
13
14
xv
16
17
18
19
20
21
22
23
24
25
26
                                                      int                            principal() {                            const                            int                            MAX = 100;     string masterList[MAX];                            int                            count = 0;     ifstream fin;                            //Open up the text file, if fails to open, return.                            fin.open("CWC_Master.txt");                            if(!fin){         cout <<                            "The file failed to open.\north";                            return                            -ane;     }                            //reading                            while(fin.eof() =!simulated){         fin >> masterList[count];         count++;      }      cout <<                            "The information is: ";                            for                            (count = 0; count < MAX; count++)         cout << masterList[count] <<                            " "                            << endl;                                                  

I only have MAX set up to 100 because thats the maximum corporeality of entries for the file, but at any given time, the file could accept anything below 100. And then I need to acquire how to read the input file into an array until eof.

Thanks and then much for reading and whatsoever help you tin can contribute!

Arrays must take a size known at compile time. Don't use an array.

Utilize ane of the the Standard Library containers, like

std::vector

.

                          one
ii
3
4
five
six
seven
eight
9
10
11
12
xiii
14
15
16
                                                      #include <fstream>                            #include <vector>                            int                            main() {     std::vector numbers;                            if(std::ifstream in ("CWC_master.txt"))     {                            int                            ten;                            while(in >> x)                            //loop on the input operation, non eof                            {             numbers.push_back(x);         }     }                            //washed, all your numbers are in the vector                            }                        

http://www.cplusplus.com/reference/vector/
http://en.cppreference.com/w/cpp/container/vector

Last edited on

Thanks for the input.

This is for a projection and unfortunately were told we cannot apply vector :/

Since your professor doesn't want y'all to learn C++, y'all volition have to do it the C way and dynamically allocate the array. I recommend reading the file twice - the first time to fine out how many values at that place are and the 2d fourth dimension to store them into the array that you dynamically allocate.

I call back he wants us to completely understand concepts before moving into other things. Like vector(I take no clue what that is yet)

I changed it to this. And although I am getting the data read from the file, information technology continuously reads repeated data until it hits the 100 value marker I believe.

Still having problem reading until EOF.

                          1
2
iii
4
5
6
seven
8
9
10
11
12
xiii
14
15
16
17
xviii
nineteen
20
21
22
23
24
25
26
                                                      const                            int                            MAX = 100;     string masterList[MAX];                            int                            count = 0;     ifstream fin;     cord chief;                            //Open the text file, if fails to open up, render.                            fin.open("CWC_Master.txt");                            if(!fin){         cout <<                            "The file failed to open.\north";                            return                            -ane;     }                            //reading                            while(count <  MAX && fin >> masterList[count]){             count++;     }      cout <<                            "The information is: ";                            for                            (count = 0; count < MAX; count++)         cout << masterList[count] <<                            " "                            << endl;                            return                            0;                                                  

while(fin >> masterList[count]) count++; would probably exist fine. I wouldn't bother with MAX unless you are reading exactly that number.

Similarly, I think count = 0; while(!masterList[count].empty()) { cout << masterList[count] << " " << endl; count++; } would be better than looping through potentially empty strings.

Last edited on

Thecal wrote:
I think he wants us to completely understand concepts before moving into other things. Like vector(I take no inkling what that is nonetheless)

The C++ customs widely agrees that

std::vector

should be taught earlier arrays. Your professor obviously doesn't agree (or doesn't even know he is in the minority).

Thecal wrote:
I changed it to this. And although I am getting the data read from the file, it continuously reads repeated data until it hits the 100 value mark I believe.

Still having trouble reading until EOF.

Don't think about EOF. Pretend it doesn't exist. Erase it from your listen. It is not important.

                          1
2
3
4
5
6
vii
eight
ix
10
11
12
13
14
15
16
17
18
xix
20
21
22
23
                                                      int                            x; std::size_t count = 0;                            if(std::ifstream in ("CWC_Master.txt")) {                            while(in >> x)     {         ++count;     } }                            //...dynamically classify `arr` to hold `count` elements                            std::szie_t i = 0;                            if(std::ifstream in ("CWC_Master.txt")) {                            while((in >> x) && i < count)                            //just in case the file was changed from last time                            {         arr[i] = x;         ++i;     } }                        

Last edited on

Information technology didnt seem to like that. Compiled, but did not display the input file. Heres what I accept:

                          1
ii
iii
4
5
half-dozen
vii
eight
nine
10
xi
12
13
fourteen
15
xvi
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
                                                      int                            main() {                            const                            int                            MAX = 100;     string masterList[MAX];                            int                            count = 0;     ifstream fin;     string master;     string x;                            //Open the text file, if fails to open, return.                            fin.open up("CWC_Master.txt");                            if(!fin){         cout <<                            "The file failed to open up.\n";                            return                            -1;     }                            else                            {                            while(fin >> ten)         {             count++;         }     }                            int                            i=0;                            while(fin >> ten && i < count)     {         masterList[i] = ten;         ++i;     }        cout << masterList[i];                            //    cout << "The data is: ";                            //    for (count = 0; count < MAX; count++)                            //        cout << masterList[count] << " " << endl;                            return                            0; }                                                  

ignore the comments, I commented out something I was trying.

Thecal wrote:
It didnt seem to like that. Compiled, only did not display the input file.

The code I showed to you lot does not print anything out. I expected you to write that function of the code yourself; it is simple and you have shown the ability to do it in the past.

The file has to exist opened twice, that'south why the if statement was duplicated.

I couted the array on the lawmaking I pasted.

No, on line 33 y'all access an invalid index and print its value. You are unlucky that your program continues instead of crashing.

Printing out the unabridged array involves using a loop.

Ah. Which is what I commented out. I apologize. let me endeavour that.

WIth your to a higher place lawmaking, you only open the file in one case. After line 22, you are at the stop of the file stream and it is in an invalid state. Y'all so endeavor to use the stream in this invalid state on line 25.

Also, y'all need to look upwardly how to dynamically classify memory using new[] and delete[] - you should not take any

MAX

variable.

Last edited on

I took a different arroyo. Tried using the book to walk me through it. Did exactly what was said. The lawmaking is:

                          i
2
3
4
5
6
7
8
9
10
eleven
12
thirteen
14
15
sixteen
17
18
xix
twenty
21
22
23
24
25
26
                                                      int                            main() {                            const                            int                            MAX = 100;                            int                            totalCount[MAX];                            int                            count = 0;      ifstream fin;      fin.open("CWC_Master.txt");                            if(!fin){         cout <<                            "The file failed to open.\due north";                            return                            -1;     }                            while                            (count < MAX && fin >> totalCount[count])         count++;      cout <<                            "The data in the input file is: ";                            for(count = 0; count < MAX; count ++)         cout << totalCount[count] <<                            " ";     cout << endl;      fin.shut();                            return                            0;                                                  

Howerver I am getting some crazy long numbers, where as I should get the data from the file.

Somehow just non grasping this.

Legitimately tried it. Not getting anything.

                          1
2
3
four
5
6
7
8
9
10
11
12
13
14
xv
sixteen
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
                                                      int                            primary() {                            int                            ten;                            int                            count = 0; ifstream fin;                            const                            int                            SIZE = 100;                            int                            Assortment[SIZE];   fin.open("CWC_Master.txt");                            if(fin) {                            while(fin >> 10)     {         ++count;     } }                            //...dynamically allocate `arr` to hold `count` elements                            int                            i = 0;                            if(fin) {                            while((fin >> 10) && i < count)                            //merely in case the file was changed from final fourth dimension                            {         ARRAY[i] = x;         ++i;     }      cout <<                            "The data in the input file is: ";                            int                            i=0;                            for(i = 0; i < SIZE; i ++)         cout << Assortment[i] <<                            " ";     cout << endl;  }                        

Said I needed to define array, so I did that, then said that the assortment had no value, and so I set size to 100. finally compiled. Spitting out naught.

Do non modify the content of the if statements, you are changing the logic of the plan.

Lines 6 and seven should not be.

Last edited on

Topic archived. No new replies immune.

metcalfthervice.blogspot.com

Source: https://www.cplusplus.com/forum/beginner/162125/

Related Posts

0 Response to "Read Characters From the User Untill Eof"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel