how do i make an array read from a file. i tried google it. but i cant get it to work.

// theisonewscpp.cpp : Defines the entry point for the console application.
//

#include<iostream>
#include <vector>
#include <algorithm>


using namespace std;

bool myfunction (int i,int j) { return (i<j); }

int main()

{
	// Arrays that will be copyed into vectors below.
	int one[]={409 , 508 , 509, 607, 608 ,707 , 139 , 148 , 149 , 157 , 158 ,166 , 167 ,229 , 238 , 239 ,247 , 248 ,256 ,257 ,266 ,337 ,338 ,346 ,347 , 355 , 356 , 445 , 446 ,455};
	int two[]={309 , 408 , 507 , 606 , 609 ,708 ,129 ,138 , 147 , 156 , 159 ,168 ,177 , 228 , 237 , 246 , 249 , 255 , 258 , 267 , 336 , 339 , 345 , 348 , 357 , 366 , 444 , 447 , 456 , 555};
	int three[]={209,308,407,506,709,808,119,128,137,146,155,169,178,227,236,245,259,268,277,335,344,349,358,367,448,457,466,556};
	int four[]={208,307,406,505,809,118,127,136,145,179,188,226,235,244,269,278,334,359,368,377,449,458,467,557,566};
	int five[]={900,108,207,306,405,909,117,126,135,144,189,225,234,279,288,333,369,378,459,468,477,558,567,666};
	int six[]={800,107,206,305,404,116,125,134,199,224,233,289,379,388,469,478,559,568,577,667};
	int seven[]={700,106,205,304,115,124,133,223,299,389,479,488,569,578,668,677};
	int eight[]={600,105,204,303,114,123,222,399,489,579,588,669,678,777};
	int nine[]={500,104,203,113,122,499,589,679,688,778};
	int ten[]={400,103,202,112,599,689,779,788};
	int eleven[]={300,102,111,699,789,888};
	int twelve[]={200,101,799,889};
	int thirteen[]={100,899,999};

	// array that will be compared from
	int previous_pick_3_numbers[20]={346,794,861,529,347,925,196,969,243,837,740,025,822,809,454,843,942,510,573,346};
	char* tier[13] = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen"};

	vector<int> pick3(previous_pick_3_numbers,previous_pick_3_numbers+20);
	
	vector<int> firsts_tier(one,one+30);
	vector<int> seconds_tier(two,two+30);
	vector<int> third_tier(three,three+28);
	vector<int> fourths_tier(four,four+26);
	vector<int> fifths_tier(five,five+24);
	vector<int> sixths_tier(six,six+20);
	vector<int> sevenths_tier(seven,seven+16);
	vector<int> eighths_tier(eight,eight+14);
	vector<int> ninths_tier(nine,nine+10);
	vector<int> tenths_tier(ten,ten+8);
	vector<int> elevenths_tier(eleven,eleven+6);
	vector<int> twelves_tier(twelve,twelve+4);
	vector<int> thirteenths_tier(thirteen,thirteen+4);

	vector<vector<int> > all_vector;
	all_vector.push_back(firsts_tier);
	all_vector.push_back(seconds_tier);
	all_vector.push_back(third_tier);
	all_vector.push_back(fourths_tier);
	all_vector.push_back(fifths_tier);
	all_vector.push_back(sixths_tier);
	all_vector.push_back(sevenths_tier);
	all_vector.push_back(eighths_tier);
	all_vector.push_back(ninths_tier);
	all_vector.push_back(tenths_tier);
	all_vector.push_back(elevenths_tier);
	all_vector.push_back(twelves_tier);
	all_vector.push_back(thirteenths_tier);

	vector<char*> tiers_str(tier, tier+13);

	int vnb = 13;

	char curr_pick[4];


	// using default comparison:

	for(int i =0; i<all_vector.size(); ++i)
	{
		vector<int> curr = all_vector[i];
		sort (curr.begin(), curr.end());
		cout << "\n";
		cout << "Looking in Tier " <<  tiers_str.at(i) << "\n";
		for (int j =0; j<sizeof(pick3); ++j)
		{
			sprintf(curr_pick,"%d",pick3[j]);
			if (binary_search(curr.begin(), curr.end(),pick3[j]))
				cout << curr_pick << " found in Tier " << tiers_str.at(i) << "!\n"; 
			else 
				cout << "not found.\n";
		}
	}
	system("pause");
	return 0;

}

I want int previous_pick_3_numbers[20]={to read from file}

Recommended Answers

All 16 Replies

Use an ifstream object and the extraction operator >> to get the values. As you read them in, loop over the members of your array and write them in. Take another shot at it and post back if you are still having trouble.

Here is what I do:

Create the text file and save the data in it. Say the file is named "MatrixData.txt".

In your C++ program, open the file and test to ensure it is opened successfully:

ifstream in("MatrixData.txt", ios::in);

if (!in) {
    cout << "Cannot open the input file.\n";
    return 0;
}

Now loop through and input the matrix data. Note that you are inputting from a file, NOT THE CONSOLE, so use in rather than cin:

for (i = 0; i < mDim; i++){ //Input the A Matrix from the file, size mDim x mDim
    for (j = 0; j < mDim; j++){
        in >> A[i][j];
    }//End for j
}//End for i

Once you are finished inputting data from the file, don't forget to close it:

in.close(); //Close the input file

Done.

If you ever want to output data to a text file, the process is similar.

DavidB. that was helpfull. that narrow down many compiler errors. i got 3 errors left. also just incase it matters the txt file is a series of 3 1 digit numbers

i get error 52 C:\Documents and Settings\fpadilla\My Documents\theisonewscpp.cpp invalid types `int[int]' for array subscript .

follow the link for the entire code http://codepad.org/yC92Ehvy


date in text file

1 5 1,
6 2 1,
9 2 3,
5 2 0,
5 2 2,
2 9 6,
1 8 0,
0 9 7,
2 3 3,
6 8 3,
1 4 8,
9 0 3,
6 0 3,
7 3 5,
9 9 5,

>> I want int previous_pick_3_numbers[20]={to read from file}

I'm seeing way more than 20 numbers in this file. You want to read the whole file or just the first 20 numbers? Do you know for a fact that the array will be exactly 20 integers in length at compile time?

If so, declare it like this...

int previous_pick_3_numbers[20];

No brackets, no equals sign. You don't use those here when you're reading from a file.

>> I want int previous_pick_3_numbers[20]={to read from file}

I'm seeing way more than 20 numbers in this file. You want to read the whole file or just the first 20 numbers? Do you know for a fact that the array will be exactly 20 integers in length at compile time?

If so, declare it like this...

int previous_pick_3_numbers[20];

No brackets, no equals sign. You don't use those here when you're reading from a file.

the [20] was from an old version of the code that i already edit.

i did the changes you said.

i get the same error

52 C:\Documents and Settings\fpadilla\My Documents\theisonewscpp.cpp invalid types `int[int]' for array subscript

i think its because the array is declared one dimensional and the forloop is 2 dimentionsal am i right?

the [20] was from an old version of the code that i already edit.

...

i think its because the array is declared one dimensional and the forloop is 2 dimentionsal am i right?

I have no idea. I don't know what's old and what's new. I can only go by your last link made a half hour ago and from your earlier posts in this thread. Looks like a one-dimensional array to me in your first post and I see nothing in the thread that says that's changed. Perhaps a re-statement of the current problem and the current code is in order.

I have no idea. I don't know what's old and what's new. I can only go by your last link made a half hour ago and from your earlier posts in this thread. Looks like a one-dimensional array to me in your first post and I see nothing in the thread that says that's changed. Perhaps a re-statement of the current problem and the current code is in order.

i get one error it is 52 C:\Documents and Settings\fpadilla\My Documents\theisonewscpp.cpp invalid types `int[int]' for array subscript

// theisonewscpp.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <string>

using namespace std;

bool myfunction (int i,int j) { return (i<j); }

int main()

{
    ifstream myfile;
    int i,j;
  
	// Arrays that will be copyed into vectors below.
	int one[]={409 , 508 , 509, 607, 608 ,707 , 139 , 148 , 149 , 157 , 158 ,166 , 167 ,229 , 238 , 239 ,247 , 248 ,256 ,257 ,266 ,337 ,338 ,346 ,347 , 355 , 356 , 445 , 446 ,455};
	int two[]={309 , 408 , 507 , 606 , 609 ,708 ,129 ,138 , 147 , 156 , 159 ,168 ,177 , 228 , 237 , 246 , 249 , 255 , 258 , 267 , 336 , 339 , 345 , 348 , 357 , 366 , 444 , 447 , 456 , 555};
	int three[]={209,308,407,506,709,808,119,128,137,146,155,169,178,227,236,245,259,268,277,335,344,349,358,367,448,457,466,556};
	int four[]={208,307,406,505,809,118,127,136,145,179,188,226,235,244,269,278,334,359,368,377,449,458,467,557,566};
	int five[]={900,108,207,306,405,909,117,126,135,144,189,225,234,279,288,333,369,378,459,468,477,558,567,666};
	int six[]={800,107,206,305,404,116,125,134,199,224,233,289,379,388,469,478,559,568,577,667};
	int seven[]={700,106,205,304,115,124,133,223,299,389,479,488,569,578,668,677};
	int eight[]={600,105,204,303,114,123,222,399,489,579,588,669,678,777};
	int nine[]={500,104,203,113,122,499,589,679,688,778};
	int ten[]={400,103,202,112,599,689,779,788};
	int eleven[]={300,102,111,699,789,888};
	int twelve[]={200,101,799,889};
	int thirteen[]={100,899,999};
//check to see if file opens
ifstream in("pik3numbers.txt", ios::in);
 
if (!in) {
    cout << "Cannot open the input file.\n";
    return 0;
}





	// array that will be compared from
	int previous_pick_3_numbers[2627];
    //{346,794,861,529,347,925,196,969,243,837,740,025,822,809,454,843,942,510,573,346};
	
	for (i = 0; i < 2627; i++){ //Input the data from the file, 
    for (j = 0; j < 2627; j++){
        in >> previous_pick_3_numbers[i][j];
    }//End for j
}//End for i
in.close(); //Close the input file
    
    
    
    
    
    
    
    char* tier[13] = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen"};

	vector<int> pick3(previous_pick_3_numbers,previous_pick_3_numbers+20);
	
	vector<int> firsts_tier(one,one+30);
	vector<int> seconds_tier(two,two+30);
	vector<int> third_tier(three,three+28);
	vector<int> fourths_tier(four,four+26);
	vector<int> fifths_tier(five,five+24);
	vector<int> sixths_tier(six,six+20);
	vector<int> sevenths_tier(seven,seven+16);
	vector<int> eighths_tier(eight,eight+14);
	vector<int> ninths_tier(nine,nine+10);
	vector<int> tenths_tier(ten,ten+8);
	vector<int> elevenths_tier(eleven,eleven+6);
	vector<int> twelves_tier(twelve,twelve+4);
	vector<int> thirteenths_tier(thirteen,thirteen+4);

	vector<vector<int> > all_vector;
	all_vector.push_back(firsts_tier);
	all_vector.push_back(seconds_tier);
	all_vector.push_back(third_tier);
	all_vector.push_back(fourths_tier);
	all_vector.push_back(fifths_tier);
	all_vector.push_back(sixths_tier);
	all_vector.push_back(sevenths_tier);
	all_vector.push_back(eighths_tier);
	all_vector.push_back(ninths_tier);
	all_vector.push_back(tenths_tier);
	all_vector.push_back(elevenths_tier);
	all_vector.push_back(twelves_tier);
	all_vector.push_back(thirteenths_tier);

	vector<char*> tiers_str(tier, tier+13);

	int vnb = 13;

	char curr_pick[4];


	// using default comparison:

	for(int i =0; i<all_vector.size(); ++i)
	{
		vector<int> curr = all_vector[i];
		sort (curr.begin(), curr.end());
		cout << "\n";
		cout << "Looking in Tier " <<  tiers_str.at(i) << "\n";
		for (int j =0; j<sizeof(pick3); ++j)
		{
			sprintf(curr_pick,"%d",pick3[j]);
			if (binary_search(curr.begin(), curr.end(),pick3[j]))
				cout << curr_pick << " found in Tier " << tiers_str.at(i) << "!\n"; 
			else 
				cout << "not found.\n";
		}
	}
	system("pause");
	return 0;
}

ok i made the for loop one dimensional and it compiles and runs now. but i dont think its reading from the file

Lines 47 and 52. So this is a 2-D array or a 1-D array? You have 2627 times 2627 numbers in the file? I think you need to explain what the assignment is from start to finish. Where' the 2627 come from? I see rows of 3 numbers in the file.

commented: I had to jet so thanks for jumping in on this one :) +14

Lines 47 and 52. So this is a 2-D array or a 1-D array? You have 2627 times 2627 numbers in the file? I think you need to explain what the assignment is from start to finish. Where' the 2627 come from? I see rows of 3 numbers in the file.

its alotter program. based on this strategie http://www.chessandpoker.com/pick_3.html


the data in the txt file is the pick 3 numbers for the past 5 years in my state. which is 2627 in total of pick 3 drawings.

the program will compare from which Tier most of the numbers in the past 3 drawings hit.

I imagine it would be a 2627 x 3 array then?

// array that will be compared from
int previous_pick_3_numbers[2627][3];
 
for (i = 0; i < 2627; i++){ //Input the data from the file, 
    for (j = 0; j < 3; j++){
        in >> previous_pick_3_numbers[i][j];
    }//End for j
}

Now it's at least consistent.

But there's a slight problem. You have a comma at the end of each line. We'll have to throw it out.

// array that will be compared from
int previous_pick_3_numbers[2627][3];

char throwOut;
 
for (i = 0; i < 2627; i++){ //Input the data from the file, 
    for (j = 0; j < 3; j++){
        in >> previous_pick_3_numbers[i][j];
    }//End for j

    in >> throwOut; // read in comma and ignore it.
}

That gets you that far, but it's now time to figure out what you're trying to do in line 65. You still have that one dimensional array vs. two dimensional problem. And if 20 is obsolete, it needs to be gone. Is pick3 a vector of ints or a vector of vector of ints. You have to get this 1 dimension versus 2 dimension problem consistent.

it will be 1-d array. the reason i added the commas is to seperate the series. every 3 number is a serioes 123,456,333, etc

i were to enter the numbers instead of having it read from the file i would have to include the com mas i.e int array[]={234,567,366,444, etc.

when i use the 2 for loops. it gives out tons of error. one for loop compiles and runs, but i dont think its reading from the file because its not finding any numbers in the tier

. . . but i dont think its reading from the file because its not finding any numbers in the tier

Right after you input the file, echo it to the console, as a check. Then you know for sure.

You also might want to cut your program waaaay down, get a section working, then slowly add sections.

Here is what I do:
Always keep a plaintext editor (like Notepad) running. Copy and paste your entire program to a plaintext document and save it as a back up. Now cut most of your code out of the program. Leave just enough to run and test the file entry section. Once you have got that section working properly, copy and paste additional code from the Notepad file back into your program. Add and test a small section of code at a time. That way you don't have a whole bunch of things to wonder about. You can build and fix one small thing at a time.

commented: Great advice +14
// theisonewscpp.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>


using namespace std;

bool myfunction (int i,int j) { return (i<j); }

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
   stringstream ss(s);
   std::string item;
   while(std::getline(ss, item, delim)) {
      elems.push_back(item);
   }
   return elems;
}


std::vector<std::string> split(const std::string &s, char delim) {
   std::vector<std::string> elems;
   return split(s, delim, elems);
}


int main()

{
   // Arrays that will be copyed into vectors below.
   int one[]={409 , 508 , 509, 607, 608 ,707 , 139 , 148 , 149 , 157 , 158 ,166 , 167 ,229 , 238 , 239 ,247 , 248 ,256 ,257 ,266 ,337 ,338 ,346 ,347 , 355 , 356 , 445 , 446 ,455};
   int two[]={309 , 408 , 507 , 606 , 609 ,708 ,129 ,138 , 147 , 156 , 159 ,168 ,177 , 228 , 237 , 246 , 249 , 255 , 258 , 267 , 336 , 339 , 345 , 348 , 357 , 366 , 444 , 447 , 456 , 555};
   int three[]={209,308,407,506,709,808,119,128,137,146,155,169,178,227,236,245,259,268,277,335,344,349,358,367,448,457,466,556};
   int four[]={208,307,406,505,809,118,127,136,145,179,188,226,235,244,269,278,334,359,368,377,449,458,467,557,566};
   int five[]={900,108,207,306,405,909,117,126,135,144,189,225,234,279,288,333,369,378,459,468,477,558,567,666};
   int six[]={800,107,206,305,404,116,125,134,199,224,233,289,379,388,469,478,559,568,577,667};
   int seven[]={700,106,205,304,115,124,133,223,299,389,479,488,569,578,668,677};
   int eight[]={600,105,204,303,114,123,222,399,489,579,588,669,678,777};
   int nine[]={500,104,203,113,122,499,589,679,688,778};
   int ten[]={400,103,202,112,599,689,779,788};
   int eleven[]={300,102,111,699,789,888};
   int twelve[]={200,101,799,889};
   int thirteen[]={100,899,999};

   // array that will be compared from
   int previous_pick_3_numbers[20]={346,794,861,529,347,925,196,969,243,837,740,025,822,809,454,843,942,510,573,346};
   char* tier[13] = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen"};

   vector<int> pick3(previous_pick_3_numbers,previous_pick_3_numbers+20);

   vector<int> firsts_tier(one,one+30);
   vector<int> seconds_tier(two,two+30);
   vector<int> third_tier(three,three+28);
   vector<int> fourths_tier(four,four+26);
   vector<int> fifths_tier(five,five+24);
   vector<int> sixths_tier(six,six+20);
   vector<int> sevenths_tier(seven,seven+16);
   vector<int> eighths_tier(eight,eight+14);
   vector<int> ninths_tier(nine,nine+10);
   vector<int> tenths_tier(ten,ten+8);
   vector<int> elevenths_tier(eleven,eleven+6);
   vector<int> twelves_tier(twelve,twelve+4);
   vector<int> thirteenths_tier(thirteen,thirteen+4);

   vector<vector<int> > all_vector;
   all_vector.push_back(firsts_tier);
   all_vector.push_back(seconds_tier);
   all_vector.push_back(third_tier);
   all_vector.push_back(fourths_tier);
   all_vector.push_back(fifths_tier);
   all_vector.push_back(sixths_tier);
   all_vector.push_back(sevenths_tier);
   all_vector.push_back(eighths_tier);
   all_vector.push_back(ninths_tier);
   all_vector.push_back(tenths_tier);
   all_vector.push_back(elevenths_tier);
   all_vector.push_back(twelves_tier);
   all_vector.push_back(thirteenths_tier);

   vector<char*> tiers_str(tier, tier+13);

   int vnb = 13;

   char curr_pick[4];
   
   char qwe[4]; //123
   char qew[4]; //132
   char weq[4]; //231
   char wqe[4]; //213
   char eqw[4]; //312
   char ewq[4]; //321

   int iqwe; //123
   int iqew; //132
   int iweq; //231
   int iwqe; //213
   int ieqw; //312
   int iewq; //321

   ifstream myfile ("pik3numbers.txt");
   string line;

   if (myfile.is_open())
   {
      while ( myfile.good() )
      {
         getline (myfile,line);
         vector<string> splitted = split(line, ' ');
         if( splitted.size()==0)
         {
            continue;
         }
         string str = splitted[0];
         sprintf(curr_pick,"%c%c%c",str.c_str()[0],str.c_str()[2],str.c_str()[4]);

         sprintf(qwe,"%c%c%c",str.c_str()[0],str.c_str()[2],str.c_str()[4]);
         sprintf(qew,"%c%c%c",str.c_str()[0],str.c_str()[4],str.c_str()[2]);
         sprintf(weq,"%c%c%c",str.c_str()[2],str.c_str()[4],str.c_str()[0]);
         sprintf(wqe,"%c%c%c",str.c_str()[2],str.c_str()[0],str.c_str()[4]);
         sprintf(eqw,"%c%c%c",str.c_str()[4],str.c_str()[0],str.c_str()[2]);
         sprintf(ewq,"%c%c%c",str.c_str()[4],str.c_str()[2],str.c_str()[0]);

         iqwe = atoi(qwe);
         iqew = atoi(qew);
         iweq = atoi(weq);
         iwqe = atoi(wqe);
         ieqw = atoi(eqw);
         iewq = atoi(ewq);

         for(int i =0; i<all_vector.size(); ++i)
         {
            sort (all_vector.begin(), all_vector.end());
         }
         for(int i =0; i<all_vector.size(); ++i)
         {
            vector<int> curr = all_vector;
            //sort (curr.begin(), curr.end());

            //cout << "\n";
            //cout << "Looking in Tier " <<  tiers_str.at(i) << "\n";

            if ( binary_search(curr.begin(), curr.end(), iqwe) ||
               binary_search(curr.begin(), curr.end(), iqew) ||
               binary_search(curr.begin(), curr.end(), iweq) ||
               binary_search(curr.begin(), curr.end(), iwqe) ||
               binary_search(curr.begin(), curr.end(), ieqw) ||
               binary_search(curr.begin(), curr.end(), iewq) )
               cout << curr_pick << " found in Tier " << tiers_str.at(i) << "!\n";
            //else
               //cout << "not found.\n";

         }
      }
      myfile.close();
   }

   else cout << "Unable to open file";


   system("pause");
   return 0;
}

I get error. 141 C:\Documents and Settings\fpadilla\My Documents\Grelot.cpp conversion from `std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >' to non-scalar type `std::vector<int, std::allocator<int> >' requested

i don't know why

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.