The file numbers InFile_yourLastName.txt stores one string and 5 decimal numbers
ID: 3855794 • Letter: T
Question
The file numbers InFile_yourLastName.txt stores one string and 5 decimal numbers in one line. For example: James Smith 76.23 62.55. 90.12 74.23 78.25
The following C++ program is not correct that am first opens the file numbersInFile_yourLastName.txt. Read the name and five numbers.
The file numbersinFile_yourLastName.txt stores one string and 5 decimal numbers in one line. For example: James Smith 76.23 62.55. 90.12 74.23 78.25 The following C++ program is not correct that am first opens the file numbersinFile_yourLastName.txt. Read the name and five numbers Calculates the average of above numbers. Next, open an output file named data_yourLastName.txt, writes to this output the name and the average in the following format with numbers in 2 decimal digits Name = James Smith Average = 76.28 Finally, closes file numberslnFile_yourLastName.txt and file data_yourLastName.txt #include #include using namespace std int main() double score1, score2, score3, score4, score5; String name; ifstream infile;Explanation / Answer
Sample code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
string firstName[30], lastName[30], mark1[30], mark2[30], mark3[30], mark4[30], mark5[30];
ifstream myfile ("numbersInFile_yourLastName.txt");
ofstream out("data_yourName.txt");
int id = 0;
if(!myfile)
{
cout<<"Error opening output file"<<endl;
return 0;
}
int count = 0;
while(!myfile.eof())
{
getline(myfile,firstName[id], ' ');
getline(myfile,lastName[id],' ');
getline(myfile,mark1[id],' ');
getline(myfile,mark2[id],' ');
getline(myfile,mark3[id],' ');
getline(myfile,mark4[id],' ');
getline(myfile,mark5[id],' ');
id++;count++;
}
int inc; float m1, m2, m3, m4, m5, avg;
if (count>0){
for(inc=0; inc<count; inc++)
{
stringstream geek1(mark1[inc]);
geek1 >> m1;
stringstream geek2(mark2[inc]);
geek2 >> m2;
stringstream geek3(mark3[inc]);
geek3 >> m3;
stringstream geek4(mark4[inc]);
geek4 >> m4;
stringstream geek5(mark5[inc]);
geek5 >> m5;
avg = (m1+m2+m3+m4+m5)/5;
out <<" Name= "<<firstName[inc]<<" "<<lastName[inc] <<endl;
out <<" Average= "<<setprecision(4)<<avg<< endl;
}
}
myfile.close();
out.close();
return 0;
}
sample numbersInFile_yourLastName.txt file:
James Smith 76.23 62.55 90.12 74.23 78.25
Sample output file
Name= James Smith
Average= 76.28
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.