i finished the program but the input of 0 and one is too much i want to know how to have it read from a file i understand i have to use inFile. open but where do i put it i put my whole program in the thread:
#include <iostream>
//#include <iomanip>
using namespace std;
int const MAX_ROW=10;
int const MAX_COL=10;
int grid [MAX_ROW][MAX_COL];
void load_grid (int binArray[][10])
{
int num =-1;
for ( int i=0;i<10;i++)
{
for ( int j=0;j<10; j++)
{
cout<<" enter a one or zero; "<<endl;
cin>>num;
while (! ((num==1)||(num==0)))
{
cout<<" error !";
cout << " enter a 0 or a 1: "<<endl;
cin>> num;
}
binArray[j]= num;
}
}
}
int count_something(int[][10],int,int);
void main()
{
int a[10][10];
int row,col;
load_grid(a);
for (int row = 0; row < MAX_ROW; row++) //2D array to go through both rows and columns
{
for (int col = 0;col< MAX_COL ; col++)
{
cout<<a[row][col]; //fill in the grid
}
cout << endl;
}
cout<<" pick a row number";
cin>> row;
if( row>10)
{
cout<<"error! input again: ";
cin>>row;
}
cout<<"pick a col ";
cin>> col;
if(col>10)
{
cout<<"error! input again: ";
cin>>col;
}
cout<<"your answer: "<<count_something(a,row,col);
}
int count_something(int grid[][10],int row, int col)
{
if ( row<0||row>MAX_ROW||col<0||col>MAX_COL)
return(0);
else
if (grid [row][col]==0)
return(0);
else
{
grid[row][col]=0;
return( 1+count_something(grid,row+1,col-1)
+count_something(grid, row+1,col)
+count_something(grid,row+1,col+1)
+count_something(grid,row,col+1)
+count_something(grid,row,col-1)
+count_something(grid,row-1,col+1)
+count_something(grid,row-1,col)
+count_something(grid,row-1,col-1));
}
}

Recommended Answers

All 2 Replies

Put it in main and pass the ifstream value to the load_grid function

string Input_File_Name;
	ifstream InFile;
	int number;
	cout << "Enter the name of the input file (ctrl-c to exit): ";
	cin >> Input_File_Name;
	InFile.open(Input_File_Name.c_str());
	while(!InFile)                          // Tests the state of the ifstream
	{
	cout << "Input file with name: " << Input_File_Name;
	cout << " could not be opened! " << endl;
	cout << "Please try again...\n" << endl;
	InFile.close();     // Closes the input file
	InFile.clear();     // Clears the ifstream
	}

        // Read in the integers here

i put my whole program in the thread

I've nothing against the fact you're putting your whole program in this thread, as long as you use code tags !

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.