i/o txt files:

Hello,


New to this site and less than 3 months of c++ experience (just something I piddle with as a hobby).
Below is the code I am struggling with. As of right now it is a workable code. It takes a .txt file with data in the form of:
(#,x,y,z,description)
It searches the last column (description) and filters the data by a user input criteria. This code only handles up to 10 lines of data from a file. I have created another similar program that will handle up to 1000 lines of data from a file, but it is ridiculously long (code wise). It is not super crazy to manually code all the different "int" and "char" variables and "if" statements (I actually use separate programs to write those things then just copy and paste the output to this code to expand it); However it seems clunky and inefficient. Is there any way I don't have to code by hand every possible variable, or every if statement?

An idea I was thinking of is to is create a do-while loop with (i=1;i<1000;i++) with the if statement looking like-->
"if('E'i.find(s2) !=string::npos){cout<<'A'i<<","<<'B'i<<","<<'C'i<<","<<'D'i<<","<<'E'i<<endl;}"

But of course this does not work.... Is there anything like this I could use, or am I just reduced to simply calling all the anticipated variables from the get go manually then manually coding every possible if statement?


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
 #include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;



int main (){
    string inputfile;




    cout<<"Make sure file is contained within folder that this programs is contained in...\n\n";
    cout<<'\n'<<'\n';
    cout<<"Enter name of file to be convert from from comma delimited\n to space delimited (add .txt ext):\n\n\n";
    cin>>inputfile;
    cout<<'\n'<<'\n';




    ////////////////////////////////////////////
    ///////////////////////////////////////////


    ofstream outputfile ("intfileconv.txt");
        if(!outputfile.is_open()) cout << "\nERROR:File Write"<<'\n';

    ifstream firstfile(inputfile.c_str());

    if(!firstfile.is_open()) cout<<"\nERROR:File Open"<<'\n';

    string p,e,n,z,d;

    while(firstfile.good()){

        getline(firstfile,p,',');
        getline(firstfile,e,',');
        getline(firstfile,n,',');
        getline(firstfile,z,',');
        getline(firstfile,d,'\n');




        cout<<p<<" "<<e<<" "<<n<<" "<<z<<" "<<d<<'\n';
        outputfile<<p<<" "<<e<<" "<<n<<" "<<z<<" "<<d<<'\n';
        p<=p;


    }

firstfile.close();
firstfile.clear();
outputfile.close();
outputfile.clear();

    /////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////




string E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11;

string
A1,B1,C1,D1,
A2,B2,C2,D2,
A3,B3,C3,D3,
A4,B4,C4,D4,
A5,B5,C5,D5,
A6,B6,C6,D6,
A7,B7,C7,D7,
A8,B8,C8,D8,
A9,B9,C9,D9,
A10,B10,C10,D10,
A11,B11,C11,D11;


//___________________________________________________________________
//___________________________________________________________________


//Open file

int i,j;
string s2;
string s3 ="";


cout <<"\n\n\n________________________________________________\n\n\n";

cout << "Input search filter: ";
cin >>s2;
cout <<'\n';

    ifstream infile ("intfileconv.txt");
    if (infile.is_open()){

//Test descriptions for control






           infile >>A1>>B1>>C1>>D1>>E1>>
                    A2>>B2>>C2>>D2>>E2>>
                    A3>>B3>>C3>>D3>>E3>>
                    A4>>B4>>C4>>D4>>E4>>
                    A5>>B5>>C5>>D5>>E5>>
                    A6>>B6>>C6>>D6>>E6>>
                    A7>>B7>>C7>>D7>>E7>>
                    A8>>B8>>C8>>D8>>E8>>
                    A9>>B9>>C9>>D9>>E9>>
                    A10>>B10>>C10>>D10>>E10;



if(E1.find(s2) !=string::npos){cout<<A1<<","<<B1<<","<<C1<<","<<D1<<","<<E1<<endl;}
if(E2.find(s2) !=string::npos){cout<<A2<<","<<B2<<","<<C2<<","<<D2<<","<<E2<<endl;}
if(E3.find(s2) !=string::npos){cout<<A3<<","<<B3<<","<<C3<<","<<D3<<","<<E3<<endl;}
if(E4.find(s2) !=string::npos){cout<<A4<<","<<B4<<","<<C4<<","<<D4<<","<<E4<<endl;}
if(E5.find(s2) !=string::npos){cout<<A5<<","<<B5<<","<<C5<<","<<D5<<","<<E5<<endl;}
if(E6.find(s2) !=string::npos){cout<<A6<<","<<B6<<","<<C6<<","<<D6<<","<<E6<<endl;}
if(E7.find(s2) !=string::npos){cout<<A7<<","<<B7<<","<<C7<<","<<D7<<","<<E7<<endl;}
if(E8.find(s2) !=string::npos){cout<<A8<<","<<B8<<","<<C8<<","<<D8<<","<<E8<<endl;}
if(E9.find(s2) !=string::npos){cout<<A9<<","<<B9<<","<<C9<<","<<D9<<","<<E9<<endl;}
if(E10.find(s2) !=string::npos){cout<<A10<<","<<B10<<","<<C10<<","<<D10<<","<<E10<<endl;}





        //infile.close();
        }


    else cout <<"Unable to open file!";



cout<< "\n\nThank you for using this program! :-)\n\n";
cin>>i;


    }
Is there any way I don't have to code by hand every possible variable, or every if statement?
The concept you're looking for is called array. See:

http://www.cplusplus.com/doc/tutorial/arrays/

1
2
3
4
5
6
7
8
9
10
11
const int MaxElements = 10;
string E[MaxElements];

string
A[MaxElements],B[MaxElements],C[MaxElements],D[MaxElements];

for(int i = 0; i < MaxElements; ++i) //Note: an array starts with 0!
{
           infile >>A[i]>>B[i]>>C[i]>>D[i]>>E[i];
}
...
Awesome coder777!
That is much smarter than what I was doing, thanks for leading me in the right direction!
Topic archived. No new replies allowed.