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;
}

Dani AI

Generated

Common pitfalls here are (1) extracting integers when the record begins with a name (the first extraction fails and leaves your arrays unchanged at 0), and (2) not checking the stream state after each read. A simple, robust pattern is to read each line, then parse it into a name plus grades. Use double if you expect fractional grades.

#include <fstream>
#include <sstream>
#include <vector>
#include <array>
#include <string>
#include <iostream>
using namespace std;

struct Student { string name; array<double,5> scores; };

int main() {
    ifstream in("input.txt");
    if (!in) { cerr << "Unable to open input.txt\n"; return 1; }

    vector<Student> students;
    string line;
    while (getline(in, line)) {
        if (line.empty()) continue;
        istringstream ss(line);
        Student s;
        bool ok = bool(ss >> s.name);
        for (double &g : s.scores) if (!(ss >> g)) { ok = false; break; }
        if (ok) students.push_back(s);
    }

    for (const auto& s : students) {
        cout << s.name << ": ";
        double sum = 0;
        for (double g : s.scores) { cout << g << ' '; sum += g; }
        cout << "avg=" << sum / s.scores.size() << '\n';
    }
}

Troubleshooting tips grounded in your thread:

  • If your file starts with names, extracting into int arrays will fail immediately; parse the name first.
  • Do not loop on a fixed counter like while(size==debug); loop until input fails.
  • If names can contain spaces, use a delimiter (e.g., CSV) and split accordingly.
  • To access a specific row/column of numeric data later, load into vector<vector<double>> and use grid[row][col].

See std::ifstream, operator>>, and std::getline for details.

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.