#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
usingnamespace std;
constint rows=6, columns=4;
int data[rows][columns];
int main()
{
int choice;
ifstream outfile;
bool again=true;
//while(again)
// {
cout<<"\n1 - 5 Display the data""\n2 - 5 Calculate the Highest and Lowest Data""\n3 - 5 Calculate the Average value of the Data""\n4 - 5 Change the Data""\n5 - 5 QUIT";
cout<<"\n\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
outfile.open("FactoryData.txt", ios::out|ios::app);
cout << "Data in \"FactoryData.txt\"" << endl;
for (int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
outfile>>data[i][j];
cout <<data[i][j];
}
}
outfile.close();
break;
}
// }
return 0;
}
output:
1 - 5 Display the data
2 - 5 Calculate the Highest and Lowest Data
3 - 5 Calculate the Average value of the Da
4 - 5 Change the Data
5 - 5 QUIT
Enter your choice: 1
Data in "FactoryData.txt"
000000000000000000000000
Press Enter to return to Quincy...
No. The problem is that you opened the file for writing to it.
It looks like you're trying to read values into data[i][j].
Open file for reading instead. Change line 28 to: outfile.open("FactoryData.txt" );
actually never mind what i said as you know exactly how many numbers there are (i must be getting sleepy lol reading stuff wrong).
strange i just copy/pasted your code and it works fine for me prints out the right values. Are you sure FactoryData.txt is in the right directory etc? . perhaps add a if(is_open) condition there and see what happens?
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
usingnamespace std;
constint rows=6, columns=4;
int data[rows][columns];
void getdata();
void highlow();
ifstream outfile;
int high=0, low=0, highrow, highcol, lowrow, lowcol;
int i, j;
int main()
{
int choice;
bool again=true;
outfile.open("FactoryData.txt", ios::out);
//while(again)
// {
cout<<"\n1 - 5 Display the data""\n2 - 5 Calculate the Highest and Lowest Data""\n3 - 5 Calculate the Average value of the Data""\n4 - 5 Change the Data""\n5 - 5 QUIT";
cout<<"\n\nEnter your choice: ";
cin>>choice;
if (outfile.is_open())
{
switch(choice)
{
case 1:
getdata();
break;
case 2:
highlow();
break;
}
}
else
cout<<"Unable to open the file!";
// }
outfile.close();
return 0;
}
void getdata()
{
cout << "\nData in \"FactoryData.txt\"\n" << endl;
for (int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
outfile>>data[i][j];
cout <<data[i][j]<<" ";
}
cout<<"\n";
}
}
void highlow()
{
int low=data[0][0];
for (int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
outfile>>data[i][j];
if (data[i][j]>high)
{
high=data[i][j];
highrow=i;
highcol=j;
}
if (data[i][j]<low)
{
low=data[i][j];
lowrow=i;
lowcol=j;
}
}
}
cout<<"\nThe Highest Value:"<<"\nROW "<<highrow<<" COLUMN "<<highcol<<"\nVALUE: "<<high << "\n";
cout<<"\n\nThe Lowest Value:"<<"\nROW "<<lowrow<<" COLUMN "<<lowcol<<"\nVALUE: "<<low << "\n";
}
1 - 5 Display the data
2 - 5 Calculate the Highest and Lowest Data
3 - 5 Calculate the Average value of the Data
4 - 5 Change the Data
5 - 5 QUIT
Enter your choice: 2
The Highest Value:
ROW 5 COLUMN 2
VALUE: 920
The Lowest Value:
ROW 0 COLUMN 0
VALUE: 0
Press Enter to return to Quincy...
to answer your second question it is because you've set low to 0 the 2nd if statement in your highlow function will never be true as nothing in your data file is less than 0. if u want to find the lowest, u need to set low to something in your data and then compare with everthing else. Also might be helpful to cout highrow/lowrow + 1 . Computers might start numbering everything from 0 but we humans start from 1 :)
Yeah thats kindof what i mean. Settling low = data[0][0] there won't help as you havent initialized the array data yet (so it will contain some gibberish value) how about calling your getdata function before it and and then set low to data[0][0] and then it just becomes a problem of sorting the array data and you dont need to read from the file again. i.e
only problem is this will print the array too..so perhaps remove cout statements from getdata and put the them in main itself.. its better to do that anyway