Why is the array not reading from the txt file

AI Thread Summary
The discussion centers on a programming issue where an array is not reading data from a text file as intended. The user aims to create a program that compares a predefined array of numbers against multiple vectors to identify matches, regardless of order. The code compiles but fails to read from the specified file, which is crucial for populating the array to be checked. Suggestions include ensuring the file path is correct and using proper data types for arrays and vectors to avoid type conversion issues. The conversation emphasizes debugging file input and improving code structure for better readability and functionality.
theisonews
Messages
3
Reaction score
0
help me with this vector program

i want to create a program that will have an arraycalled array1[] of 30 sets of 3 one digit numbers i.e 804, 450,430 etc)

i will have 13 vector containersor arrays defined with a series of 3 one digit numbers. i want the program to find any series of numbers that is array1[] in any of the 13 vectors. and it will tell me in which vector the series was found in

also the order of the number shouldn't matter for example vector<int> first_tier (324)

the program should compare the values of one vector to an other vector for reoccuring series and it finds it in first_tier because 3 so which function do i use to do this. this is what i got so far

#include <iostream>
#include <algorithm>


using namespace std;

int * p; // points to array element


int main()

{
int First_tier[]={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 second_tier[]={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 third_tier[]={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 fourth_tier[]={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 fifth_tier[]={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 sixth_tier[]={800,107,206,305,404,116,125,134,199,224,233,289,379,388,469,478,559,568,577,667};
int seventh_tier[]={700,106,205,304,115,124,133,223,299,389,479,488,569,578,668,677};
int eighth_tier[]={600,105,204,303,114,123,222,399,489,579,588,669,678,777};
int ninth_tier[]={500,104,203,113,122,499,589,679,688,778};
int tenth_tier[]={400,103,202,112,599,689,779,788};
int eleventh_tier[]={300,102,111,699,789,888};
int twelve_tier[]={200,101,799,889};
int thirteenth_tier[]={100,899,999};


//numbers to be found in the above arrays.
int previous_pick_3_numbers[]{346,794,861,529,347,925,196,969,243,837,740,025,822,809,454,843942,510,573,346}


return 0;

}
 
Technology news on Phys.org
the code compiles and runs but i don't think its reading from the file. the pick 3 array is suppose to read from the file.

below is the code

Code:
// 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 f,g;
  
	// 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("C:\\Documents and Settings\\fpadilla\\My Documents\\file.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[1125];
	
    
	//throw out commas
    char throwOut;
    
for (g = 0; g < 1125; g++)
    { //Input the data from the file, 
   
        in >> previous_pick_3_numbers[g];
    }//End for g
  in >> throwOut; // read in comma and ignore it.}
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;
}
 
Well, firstly, let's get that code indented and formatted in an acceptable way (you seriously need to work on that):

Code:
// 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 f, g;

    // 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("C:\\Documents and Settings\\fpadilla\\My Documents\\file.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[1125];

    //throw out commas
    char throwOut;
    for (g = 0; g < 1125; g++)
    { //Input the data from the file,
        in >> previous_pick_3_numbers[g];
    }//End for g
    in >> throwOut; // read in comma and ignore it.}
    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;
}

Did you turn all the warnings on when compiling? I suggest you do so. A few complaints g++ made was that you are converting const char* to char*:

char* tier[13] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen"};

That should be:

const char* tier[13] = ... etc ...

You will want to change the vector also to add const:

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

However, that's kind of a nasty way to initialize a vector from an array. Initializing vectors and such from arrays will become much nicer and easier in the next C++ standard (C++0x), but until then, this might save you from counting elements manually:

vector<int> firsts_tier(one, one + (sizeof(one) / sizeof(one[0])));

The compiler also complains about lines like this:

for (int i = 0; i < all_vector.size(); ++i)

The problem is that .size() doesn't return an int. It returns a value of type size_t. The for loop would better be written as:

for (size_t i = 0; i < all_vector.size(); ++i)

This line is also possibly a problem:

for (int j = 0; j < sizeof(pick3); ++j)

pick3 is a vector. Shouldn't you be using .size() here as well? Keep in mind that size() doesn't return an int, so you need to fix it like the previous one.

You shouldn't generally put "magic numbers" in your code. This kind of thing is a bad idea:

int previous_pick_3_numbers[1125];

for (g = 0; g < 1125; g++)

At least define a constant for that (the 1125). It'll save you hassle later, trust me. However, the whole idea of what you're doing right there is probably a bad one. If you go over, it will silently stop reading in numbers. If you're under, then you're wasting memory space at best, or trying to read numbers that aren't there at worst.

Why not read a variable number of ints into a vector until you hit the end of the file, instead? You put them in vector one a bit later, anyways.

I can't really test your program since I don't have the input file. But I'm not sure you're reading it in correctly. Are you supposed to be reading in and throwing out a comma between each number? Right now, it's outside the loop. Perhaps that's your main bug?

I tend to like to use istream::ignore to get past things like commas and such. You might want to look it up and see if it might not do it better.

One piece of extra advice: You might want to think about splitting that monolithic main() into some functions.

Anyways, that's what I see on my first pass through. Hope it helps.
 
theisonews said:
the code compiles and runs but i don't think its reading from the file. the pick 3 array is suppose to read from the file.

I would recommend that you use either a console library, (or the standard IO) to actually print stuff out either to some standard IO whether its a console or some output log file.

If you're using windows you can use ::AllocConsole, but it would probably be smarter to use cout or something of that nature.

If your data ends up being junk, then its most likely going to be a pointer issue or something or that sort (wrong data type/format , bad index and so on).
 
Grep said:
Well, firstly, let's get that code indented and formatted in an acceptable way (you seriously need to work on that):

Code:
// 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 f, g;

    // 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("C:\\Documents and Settings\\fpadilla\\My Documents\\file.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[1125];

    //throw out commas
    char throwOut;
    for (g = 0; g < 1125; g++)
    { //Input the data from the file,
        in >> previous_pick_3_numbers[g];
    }//End for g
    in >> throwOut; // read in comma and ignore it.}
    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;
}

Did you turn all the warnings on when compiling? I suggest you do so. A few complaints g++ made was that you are converting const char* to char*:

char* tier[13] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen"};

That should be:

const char* tier[13] = ... etc ...

You will want to change the vector also to add const:

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

However, that's kind of a nasty way to initialize a vector from an array. Initializing vectors and such from arrays will become much nicer and easier in the next C++ standard (C++0x), but until then, this might save you from counting elements manually:

vector<int> firsts_tier(one, one + (sizeof(one) / sizeof(one[0])));

The compiler also complains about lines like this:

for (int i = 0; i < all_vector.size(); ++i)

The problem is that .size() doesn't return an int. It returns a value of type size_t. The for loop would better be written as:

for (size_t i = 0; i < all_vector.size(); ++i)

This line is also possibly a problem:

for (int j = 0; j < sizeof(pick3); ++j)

pick3 is a vector. Shouldn't you be using .size() here as well? Keep in mind that size() doesn't return an int, so you need to fix it like the previous one.

You shouldn't generally put "magic numbers" in your code. This kind of thing is a bad idea:

int previous_pick_3_numbers[1125];

for (g = 0; g < 1125; g++)

At least define a constant for that (the 1125). It'll save you hassle later, trust me. However, the whole idea of what you're doing right there is probably a bad one. If you go over, it will silently stop reading in numbers. If you're under, then you're wasting memory space at best, or trying to read numbers that aren't there at worst.

Why not read a variable number of ints into a vector until you hit the end of the file, instead? You put them in vector one a bit later, anyways.

I can't really test your program since I don't have the input file. But I'm not sure you're reading it in correctly. Are you supposed to be reading in and throwing out a comma between each number? Right now, it's outside the loop. Perhaps that's your main bug?

I tend to like to use istream::ignore to get past things like commas and such. You might want to look it up and see if it might not do it better.

One piece of extra advice: You might want to think about splitting that monolithic main() into some functions.

Anyways, that's what I see on my first pass through. Hope it helps.

thanks for that. when i get off work. i will give it an other try
 
Code:
for (g = 0; g < 1125; g++)
    {
        in >> previous_pick_3_numbers[g];
    }
  in >> throwOut;
in.close();

Look carefully at your input code. Your data file has a comma after each number, correct?

The statement "in >> previous_pick_3_numbers[g];" is performed 1125 times.

How many times is the statement "in >> throwOut;" (which is supposed to skip over the commas) performed?
 


First I'd suggest making a vector of vectors rather than having 13 separate named variables.

Next, you can use std::includes or std::set_intersection to determine if one vector is a subset of another. So in pseudo-code:

for each vector in the vector list
check to see if the "main" vector is a subset of the current vector using std::includes
if yes, add a pointer to this vector in a separate vector of vector pointersiterate through your new filtered list
do whatever
 
Last edited:

Similar threads

Back
Top