I am trying to read integers from a txt file and then read the values looking for similarities. I am initializing an array as all 0 however, after I have read from the file and output all the variables, there are random numbers displayed in the array and I don't know why.


Here is my entire code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
	int j=0;
	int i;
	int check1=0;
	int check2=0;
	int option=9;
	string fileName, temp;
	ifstream in_file;
	ofstream out_file;
	int count;
	int line[100] = {0};

	while (option != 0)
	{
		cout << "COP2271 Data Manager" << endl;
		cout << endl;
		cout << "MAIN MENU" << endl;
		cout << "1. To input graph data-file." << endl;
		cout << "2. To read graph from data-file." << endl;
		cout << "3. To compute the number of outgoing arcs from a certain node." << endl;
		cout << "4. To compute the number of incoming arcs to a certain node." << endl;
		cout << "5. To create a file with a subgraph." << endl;
		cout << "Please choose: ";
		cin >> option;
	
		if (option == 1)
		{
			if (check1 == 0) 
			{
				do 
				{
					cout << "Please give the filename: ";
					cin >> fileName;
					cout << endl;
					in_file.open(fileName.c_str());

					if (in_file.fail())
					{
						cout << "Error opening file. Please try again." << endl;
						j++;
					}
					else 
					{
						check1 = 1;
						break;
					}
				} while (j<3);
			}
		}
		if (option == 2)
		{
			if (check1 == 0)
			{
				cout << "No file has been given yet!" << endl;
				cout << endl;
			}
			else 
			{	

				while (!in_file.eof())
				{
					for (count = 0; count < sizeof(line); count++)
					in_file >> line[count];
				}

				cout << "Graph successfully inputted! The graph has " << line[0] << " arcs." << endl <<endl; 
				check2 = 1;
				for(int x=0; x<sizeof(line); x++)
				{
					cout << line[x];
				}
			}

		}
	
		if (option == 3)
		{
			if (check1 == 0)
			{
				cout << "No file has been given yet!" << endl;
				cout << endl;
			}
			else if (check2 == 0)
			{
				cout << "The file has not been read yet." << endl;
				cout << endl;
			}
			else
			{
				int nodeCount=0;
				int s = sizeof(line);

				cout << "Please give the node that you are looking for: ";
				cin >> i;
				cout << endl;
			
				for (int k=1; k < s; k=k+2)
				{
					if(line[k] == i)
					{
						nodeCount++;
					}
				}
				
				cout << "There are " << nodeCount << " arcs leaving node " << i << endl; 
			}
		}
		if (option == 4)
		{
			if (check1 == 0)
			{
				cout << "No file has been given yet!" << endl;
				cout << endl;
			}
			else if (check2 == 0)
			{
				cout << "The file has not been read yet." << endl;
				cout << endl;
			}
			else
			{
				int nodeCount=0;
				int s = sizeof(line);

				cout << "Please give the node that you are looking for: ";
				cin >> i;
			
				for (int k=2; k < s; k=k+2)
				{
					if(line[k] == i)
					{
						nodeCount++;
					}
				}
				
				cout << "There are " << nodeCount << " arcs going to node " << i << endl; 
			}
		}
		if (option == 5)
		{
			if (check1 == 0)
			{
				cout << "No file has been given yet!" << endl;
				cout << endl;
			}
			else if (check2 == 0)
			{
				cout << "The file has not been read yet." << endl;
				cout << endl;
			}
			else
			{
				int s = sizeof(line);
				int subArcCount = 0;
				int x[10000];
				int p = 1;
				string nodes[500], subNode, tempNodes;
				out_file.open("subgraph.txt");
				int check3 = 0;
		
				cout << "Please give nodes you want to include in the subgraph (press -1 to stop input): ";
				for(int i=0;x[i-1]!=-1;i++) //writing to array x all the nodes you want to examine
				{
					cin >> x[i];
				}
				//nested for loops compare the nodes the user wants to look for to the nodes in the file
				for (int k=1; k < sizeof(line); k=k+3)//compares if the node is the first node
				{
					for (int j = 0; j < 10000; j++) 
					{
						if (line[k]==x[j])						
							nodes[p]=line[k]+line[k+1]+line[k+2]; 
							p++;
							subArcCount++;
					}
				}


				for (int k =3; k< sizeof(line); k=k+3)//compares if the node is the second node
				{
					for (int j = 0; j < 10000; j++) 
					{
						if (line[k]==x[j]&&!nodes[k].empty()&&!nodes[k].empty())						
							nodes[p]=line[k]+line[k+1]+line[k+2]; 
							p++;
					}
				}

				for (int k = 1; k < 500;k++)//this for loop checks for duplicates (if the arc went both ways, without this it would count it twice)
				{
					for(int j = 1; j<500;j++)
					{
						if(k!=j&&nodes[k]==nodes[j]&&!nodes[k].empty())
						{
							nodes[j].clear();
							
						}
					}
				}
				for(int k = 1;k<500;k++)//this should count the total number of nodes in the subgraph
				{
					if(nodes[k].length()==3)
						subArcCount++;
				}

				out_file << subArcCount << endl;//this should output the node count
				for (int k = 1; k<500;k++)
				{
					out_file << nodes[k] << endl;//this should output the actual arc recordings
				}
				cout << "Subgraph written successfully in subgraph.txt" << endl << endl;
			}
		}

		if (option != 1 && option != 2 && option != 3 && option != 4 && option != 5)
		{
			cout << "Wrong choice! Please choose again." << endl << endl;
		}
	}
	cout << "Now quitting.." << endl;

	system ("Pause");
	return (0);
}

And here is the section that is giving me trouble.

while (!in_file.eof())
				{
					for (count = 0; count < sizeof(line); count++)
					in_file >> line[count];
				}

				cout << "Graph successfully inputted! The graph has " << line[0] << " arcs." << endl <<endl; 
				check2 = 1;
				for(int x=0; x<sizeof(line); x++)
				{
					cout << line[x];
				}

I've intialized my array to 0 but when I output the values of line it has a lot of weird numbers added in as well and I don't know why.

Anyone able to help?

Okay, I have discovered it is properly outputting the 100 values of line but then it outputs additional values beyond that.

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.