//I need to convert this program to read from input file.

I know how to create the data file but I am strugling with how to code in fstream

#include <iostream>

using namespace std;

// Function Prototypes

void Linear_search (int[], int);

// main function

int main ()

{

//Variable declaration

int NUM_ELS = 18;

const int ARRAY_SIZE = 18;

int array [ARRAY_SIZE] = { 5658845, 8080152, 1005231,

450125, 4562555, 6545231, 7895122, 5522012, 3852085,

8777541, 5050552, 7576651, 8451277, 7825877, 7881200,

1302850, 1250255, 4581002};

Linear_search (array, NUM_ELS);

}

// Linear Search Funtions

void Linear_search (int array[], int NUM_ELS)

{

int charge_num; // variable to hold Charge Number

bool found = false;

// Ask user for number

cout << " Input account charge number: " << endl;

cin >> charge_num;

for (int i = 0; i < NUM_ELS; i++)

{

if (array[i] == charge_num)

{

found = true;

break;

}

}

if (found== +1)

{

cout << " Number Valid " << endl;

}

else

{

cout << " Number Invalid" << endl;

} system("pause");

}

// Definition of Function selectionsort

//Performs an ascending order selection sort order on Array. Size is the number of elements

void selectionSort(int array [], int size)

{

int startScan, minIndex, minValue;

for(startScan = 0; startScan<(size-1); startScan++)

{

minIndex = startScan;

minValue = array[startScan];

for (int index = startScan +1; index < size; index++)

{

if (array [index] < minValue)

{

minValue = array[index];

minIndex = index;

}

}

array[minIndex] = array[startScan];

array[startScan] = minValue;

system("pause");

}

}

// Definition of function showArray.

// This function displays output

void showArray (const int array[], int size)

{

for (int count = 0; count < size; count++)

cout <<array[count] <<" ";

cout<<endl;

system("pause");

}

I know how to create the data file but I am strugling with how to code in fstream

#include <iostream>

using namespace std;

// Function Prototypes

void Linear_search (int[], int);

// main function

int main ()

{

//Variable declaration

int NUM_ELS = 18;

const int ARRAY_SIZE = 18;

int array [ARRAY_SIZE] = { 5658845, 8080152, 1005231,

450125, 4562555, 6545231, 7895122, 5522012, 3852085,

8777541, 5050552, 7576651, 8451277, 7825877, 7881200,

1302850, 1250255, 4581002};

Linear_search (array, NUM_ELS);

}

// Linear Search Funtions

void Linear_search (int array[], int NUM_ELS)

{

int charge_num; // variable to hold Charge Number

bool found = false;

// Ask user for number

cout << " Input account charge number: " << endl;

cin >> charge_num;

for (int i = 0; i < NUM_ELS; i++)

{

if (array[i] == charge_num)

{

found = true;

break;

}

}

if (found== +1)

{

cout << " Number Valid " << endl;

}

else

{

cout << " Number Invalid" << endl;

} system("pause");

}

// Definition of Function selectionsort

//Performs an ascending order selection sort order on Array. Size is the number of elements

void selectionSort(int array [], int size)

{

int startScan, minIndex, minValue;

for(startScan = 0; startScan<(size-1); startScan++)

{

minIndex = startScan;

minValue = array[startScan];

for (int index = startScan +1; index < size; index++)

{

if (array [index] < minValue)

{

minValue = array[index];

minIndex = index;

}

}

array[minIndex] = array[startScan];

array[startScan] = minValue;

system("pause");

}

}

// Definition of function showArray.

// This function displays output

void showArray (const int array[], int size)

{

for (int count = 0; count < size; count++)

cout <<array[count] <<" ";

cout<<endl;

system("pause");

}

Last edited on

Add code tags and explain a bit about what the program is supposed to do. Simply put [ code ] and [ / code ] around your code and remove the spaces.

Last edited on

I need to convert this program to read from input file.

I know how to create the data file but I am strugling with how to code in fstream

The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4 × 4 field on the left contains two mines, each represented by a “*” character. If we represent the same field by the hint numbers described above, we end up with the field on the right:

*... *100 .... 2210 .*.. 1*10 .... 1110

The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m (0 < n, m ≤ 100) which stand for the number of lines and columns of the field, respectively. Each of the next n lines contains exactly m characters, representing the field.

Can someone help !!! Pls!!!

I know how to create the data file but I am strugling with how to code in fstream

The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4 × 4 field on the left contains two mines, each represented by a “*” character. If we represent the same field by the hint numbers described above, we end up with the field on the right:

*... *100 .... 2210 .*.. 1*10 .... 1110

The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m (0 < n, m ≤ 100) which stand for the number of lines and columns of the field, respectively. Each of the next n lines contains exactly m characters, representing the field.

Can someone help !!! Pls!!!

Last edited on

Topic archived. No new replies allowed.