Answer to Question #215837 in C++ for Khan

Question #215837

Write a C++ program that read student’s subject marks from file named (std_makrs.txt) which contains the marks of 5 students. Each row of the file represents the subject marks of a student. Total subject registered for each student is saved into file named (std_subject.txt)


1
Expert's answer
2021-07-11T14:35:37-0400
/*
std_makrs.txt file:


75 65 85 95 56
45 56 23
89 56 56 23 56 89 78 45
23 56 89 56 23 15
65 95 75 85


*/


#include <iostream>
#include <string>
#include <sstream>
#include <fstream>


using namespace std;


int main () {
	ifstream std_makrsFile("std_makrs.txt");
	if (std_makrsFile.good())
	{
		ofstream std_subjectFile;
		std_subjectFile.open("std_subject.txt");
		string line;
		int n=1;
		while(getline(std_makrsFile, line)) 
		{
			istringstream ss(line);
			int totalSubjectRegistered=0;
			int mark;
			//Read from file in till end of line
			while(ss >> mark)
			{
				totalSubjectRegistered++;
			}
			std_subjectFile<<"Total subject registered for student "<<n<<" is "<<totalSubjectRegistered<<"\n";
			n++;
		}
		std_subjectFile.close();
	}else{
		cout<<"\nThe file does not exist.\n\n";
	}
	//Close the files
	std_makrsFile.close();
	
	system("pause");
	return 0;
}







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS