Hi, could anybody please tell me what i am doing wrong in this program.

The goal is to read the student name and grade from a text file and print it on the screen.

This is what i have so far. by the way this is for a school project that is due this coming Monday. The attached file is the input source.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

//void calculateAverage();
//int calculateGrade();
//void updateFrequency();

int main()
{
	const int size=10;
	int debug=size;

	int test1[size], test2[size], test3[size], test4[size], test5[size];
	ifstream infile;
	for(int i=0;i<size;i++)
	{
		test1[i]=0;
		test2[i]=0;
		test3[i]=0;
		test4[i]=0;
		test5[i]=0;
	}

	cout<<"Reading from file ''input.txt''..."<<endl;
	infile.open("input.txt");

	while(size==debug)
	{
		for(int i=0;i<size;i++)
		{
			infile>>test1[i];
			infile>>test2[i];
			infile>>test3[i];
			infile>>test4[i];
			infile>>test5[i];
		}
		debug--;
	}
	
	for(int i=0;i<size;i++)
	{
		cout<<test1[i]<<" "<<test2[i]<<" "<<test3[i]<<" "<<test4[i]<<" "<<test5[i]<<" "<<endl;
	}
	infile.close();
	system("pause");
	return 0;
}

Recommended Answers

All 4 Replies

You should check first to see if the file has been opened.

ifstream inFile;
if (!inFile) {
cerr << "Unable to open file input.txt";
    exit(1);   // call system to stop
}

Once you have done that, read from the stream the same way you do cin.

int sum = 0;
int x;

while (inFile >> x)
sum += x;

inFile.close();

i did that but it still doesn't work. the thing i dont get is that after initializing the array elements to 0

for(int i=0;i<size;i++)
	{
		test1[i]=0;
		test2[i]=0;
		test3[i]=0;
		test4[i]=0;
		test5[i]=0;
	}

and after i read this:

while(size==debug)
	{
		for(int i=0;i<size;i++)
		{
			infile>>test1[i];
			infile>>test2[i];
			infile>>test3[i];
			infile>>test4[i];
			infile>>test5[i];
		}
		debug--;
	}

but when i print it. all that comes out is the 0's

for(int i=0;i<size;i++)
	{
		cout<<test1[i]<<" "<<test2[i]<<" "<<test3[i]<<" "<<test4[i]<<" "<<test5[i]<<" "<<endl;
	}

Attached is the output file

like this?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//void calculateAverage();
//int calculateGrade();
//void updateFrequency();

int main()
{
	//const int size=10;
	//int debug=size;
    string tmp;
	//int test1[size], test2[size], test3[size], test4[size], test5[size];
	
	ifstream infile;
	/*
	for(int i=0;i<size;i++)
	{
		test1[i]=0;
		test2[i]=0;
		test3[i]=0;
		test4[i]=0;
		test5[i]=0;
	}
*/
	cout<<"Reading from file ''input.txt''..."<<endl;
	infile.open("input.txt");
	
	if( infile ){
	
	    while( getline(infile, tmp ) ){
	        cout << tmp << endl;
	    }
	}
	
/*
	while(size==debug)
	{
		for(int i=0;i<size;i++)
		{
			infile>>test1[i];
			infile>>test2[i];
			infile>>test3[i];
			infile>>test4[i];
			infile>>test5[i];
		}
		debug--;
	}
	
	for(int i=0;i<size;i++)
	{
		cout<<test1[i]<<" "<<test2[i]<<" "<<test3[i]<<" "<<test4[i]<<" "<<test5[i]<<" "<<endl;
	}
	*/
	
	infile.close();
	#ifdef __LINUX 
	system("pause");
	#endif
	return 0;
}

Dear Sir, How can I input fraction number (like 2.345) in a row. How can i select a poit ( in row 1, column 3). If you please response !!!

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.