Im writing a program that reads the first value from the file and use it to cont
ID: 3624788 • Letter: I
Question
Im writing a program that reads the first value from the file and use it to control a FOR LOOP that reads the rest of the values from the file.
#include
#include
#include
using namespace std;
int main()
{
ifstream infile; // Create an input file stream.
int lineSize[5];
int counter;
int x;
int str;
int sum;
int average;
infile.open("F://data.txt"); // Use it to read from a file named data.txt.
infile >> x; // Read the first item from the file into an integer variable x.
cout << "The Program will use "<< x << endl;
infile >> str; // Read the next item from the file into a string variable str
for (counter = 0; counter < 5; counter++)
{
infile >> lineSize[counter];
}
cout << endl;
cout << "The numbers are: ";
for(counter = 0; counter < x;counter++)
{
cout << lineSize[counter] << " ";
}
cout << endl;
for (counter = 0; counter < 5; counter++)
{
infile >> lineSize[counter];
}
cout << endl;
cout << "The numbers are: ";
for(counter = 0; counter < 5;counter++)
{
cout << lineSize[counter] << " ";
}
cout << endl;
for (counter = 0; counter < 5; counter++)
{
infile >> lineSize[counter];
}
cout << endl;
cout << "The numbers are: ";
for(counter = 0; counter < 5;counter++)
{
cout << lineSize[counter] << " ";
}
cout << endl;
for (counter = 0; counter < 5; counter++)
{
infile >> lineSize[counter];
}
cout << endl;
cout << "The numbers are: ";
sum=0;
for(counter = 0; counter < 5;counter++)
{
cout << lineSize[counter] << " ";
}
cout << endl;
sum=sum+lineSize[counter];
average=sum / counter;
cout << "The numbers sum is: ";
cout << sum << endl;
cout << "The average is: ";
cout << average << endl;
system ("Pause");
return 0;
}
This works but I went ahead in the chapters and got it to work using arrays. My teacher said we could not use arrays yet. So im stuck again. I went back through the chapters in the malik book, i can;t seem to find what im stuck on. The program has to beable to loop through the entire file it could be only 40 numbers or it could be 200. It has to work for any of the numbers.
So now Im staring a new code...
the code dosn;t work so well it complies but i get garbage. any help would be greatly appericated thanks!!!!!
#include
#include
#include
using namespace std;
int main()
{
ifstream infile;
int i,j;
int sum,num;
infile.open("F://data.txt");
infile >> i;
cout << "The Program will use "<< i << endl;
infile >> j;
for (i=1; 1<=4; i++)
{
sum=0;
for (j=1; j<=5; j++)
{
infile >> j;
cout << i << " ";
j= j+ i;
}
cout << "sum = " << j << endl;
}
system ("Pause");
return 0;
}
Explanation / Answer
// assuming file contain only numbers and we dont know how many are there. // in file each line contain one number. #include <fstream>#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
int main() {
const char* filename = "test.txt";
std::ifstream inFile(filename);
// Make sure the file stream is good
if(!inFile) {
cout << endl << "Failed to open file " << filename;
return 1;
}
long sum = 0;
long n = 0;
for(; !inFile.eof(); ;) {
inFile >> n;
cout << std::setw(10) << n; sum = sum + n;
su } cout << std::setw(10) << sum;
cout << endl;
return 0;
} // assuming file contain only numbers and we dont know how many are there. // in file each line contain one number. #include <fstream>
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
int main() {
const char* filename = "test.txt";
std::ifstream inFile(filename);
// Make sure the file stream is good
if(!inFile) {
cout << endl << "Failed to open file " << filename;
return 1;
}
long sum = 0;
long n = 0;
for(; !inFile.eof(); ;) {
inFile >> n;
cout << std::setw(10) << n; sum = sum + n;
su } cout << std::setw(10) << sum;
cout << endl;
return 0;
}