I/O - How to read data from input file and write it to ouput file and calculate the average number

My input file is named scores.txt with the below information:

Becky Muggah 34 65 76 43 76 34 76 32 54 76
Hanna Riley 32 7 76 54 67 87 65 87 98 78
Henry James 34 65 87 9 56 34 23 65 87 56
Johan Rhys 32 76 64 46 98 64 23 56 78 87
Casper Thomas 32 89 45 78 36 74 26 7 3 63
Gabriel David 39 58 29 76 58 29 1 92 78 85

My output file is named output.txt


My full code to date:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cctype>

using namespace std;

void input (ifstream& input_file, ofstream& output_file);
char name;

int main()
{
ifstream input_file;
ofstream output_file;

input_file.open("scores.txt");
if(input_file.fail())
{
cout << "The input file open has failed";
cin.get();
cin.ignore();
exit(1);
}

output_file.open("output.txt");
if(output_file.fail())
{
cout << "The output file open has failed";
cin.get();
cin.ignore();
exit(2);
}

output_file << "Writing to Output File.\n";

input(input_file, output_file);

output_file << "\nMain is working if you see this text.\n";

output_file.close();
return 0;

}

void input(ifstream& input_file, ofstream& output_file)
{
int spacecounter=0; int in_score, total_score=0, final_score=0;
char in_name, name=0;

output_file << "\nThis is from my function called void input.\n";

while(name)
{
input_file.get(in_name);
output_file.put(in_name);
input_file.get(in_name);

if(isspace(in_name))
spacecounter++;

{
if(spacecounter>=2)
name=false;
}

output_file << name;

for(int i=0; i<=9; i++)
{
input_file >> in_score;
output_file << in_score;
total_score=in_score;
}

}
}


My input and output files both open. My problem right now is since I added name=0, nothing from my input file goes to the output file. It is just taking my information in the program in quotes and writes it out.

This is what my output file looks like:

Writing to Output File.

This is from my function called void input.

Main is working if you see this text.

I need it to look like this:

Becky Muggah 34 65 76 43 76 34 76 32 54 76 56.6
Hanna Riley 32 7 76 54 67 87 65 87 98 78 65.1
Henry James 34 65 87 9 56 34 23 65 87 56 51.6
Johan Rhys 32 76 64 46 98 64 23 56 78 87 62.4
Casper Thomas 32 89 45 78 36 74 26 7 3 63 45.3
Gabriel David 39 58 29 76 58 29 1 92 78 85 54.5

How do I capture each character of the name and each score to then write it to my output file and calculate the average per person?
closed account (SECMoG1T)
First of all while (name) will never execute ad it will always be false
You can use while (input_file) instead.

How do I capture each character of the name and each score to then write it to my output file and calculate the average per person?

Consider using a stringstream object to parse the score s
Thanks, but I have not yet learned about stringstream yet; therefore, cannot use that.

The while(name) came from the teacher. I may be missing additional needed code.

What I am looking for is how do I read the input file to the output file with the get and put with knowing there is always 1 space between the first and last name and a space between each score?
Ok, so I guess I have learned about stringstream.

Here is my code and it works, but I have a question on what I did. I got some help from someone and I don't know why we used a 1 in my while loop.

Can someone help explain why he used a while(1) please?

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cctype>

using namespace std;

void input (ifstream& input_file, ofstream& output_file);
char name;

//Function: Main Program
//Arguments: None
//Returns: 0 - Successful completion

int main()
{
ifstream input_file;
ofstream output_file;

input_file.open("scores.txt");
if(input_file.fail())
{
cout << "The input file open has failed";
cin.get();
cin.ignore();
exit(1);
}

output_file.open("output.txt");
if(output_file.fail())
{
cout << "The output file open has failed";
cin.get();
cin.ignore();
exit(2);
}

output_file << "Writing to Output File.\n\n";

input(input_file, output_file);

output_file.close();
return 0;

}

void input(ifstream& input_file, ofstream& output_file)
{
using namespace std;

while(1)
{
string first, last;
double t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;

input_file >> first >> last >> t1 >> t2 >> t3 >> t4 >>t5 >> t6 >>t7 >> t8 >> t9 >> t10;

if (input_file.eof())
{
break;
}

double ave=((t1+t2+t3+t4+t5+t6+t7+t8+t9+t10)/10);

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

output_file << first << ' '
<< last << ' '
<< t1 << ' '
<< t2 << ' '
<< t3 << ' '
<< t4 << ' '
<< t5 << ' '
<< t6 << ' '
<< t7 << ' '
<< t8 << ' '
<< t9 << ' '
<< t10 << ' '
<< ave
<< endl
<< endl;

}

}
New Code - All works, but I need to switch it around somehow so that IF a person has less than 10 scores, it still processes and still divides by 10.

I cannot figure out how to change my code to get the above to work.

My input file looks like:
Becky Muggah 34 65 76 43 76 34 76 32 54 76
Hanna Riley 32 7 76 54 67 87 65 87 98 78
Henry James 34 65 87 9 56 34 23 65 87 56
Johan Rhys 32 76 64 46 98 64 23 56 78 87
Casper Thomas 32 89 45 78 36 74 26 7 3 63
Gabriel David 39 58 29 76 58 29 1 92 78 85

So, if I remove the last score for Becky of 76, the code no longer works.

Please HELP!



My code now is:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cctype>

using namespace std;

void input (ifstream& input_file, ofstream& output_file);

//Function: Main Program
//Arguments: None
//Returns: 0 - Successful completion

int main()
{
ifstream input_file;
ofstream output_file;

input_file.open("scores.txt");
if(input_file.fail())
{
cout << "The input file open has failed";
cin.get();
cin.ignore();
exit(1);
}

output_file.open("output.txt");
if(output_file.fail())
{
cout << "The output file open has failed";
cin.get();
cin.ignore();
exit(2);
}

output_file << "Writing to Output File.\n";

input(input_file, output_file);

output_file << "\n\nMain is working if you see this text.\n";

output_file.close();
input_file.close();

cin.get();
cin.ignore();

return 0;

}

//Function: Void input
//Arguments: 2
//Returns: info from input file into output file

void input(ifstream& input_file, ofstream& output_file)
{
char next; int spacecounter=0, in_score=0, gradecounter=0; double gradetotal=0;

output_file << "\nThis is from my function called void input.\n\n";

input_file.get(next);

while(! input_file.eof())
{
output_file.put(next);
gradecounter=0;
gradetotal=0;
input_file.get(next);

if(isspace(next))
spacecounter++;

if(spacecounter>=2)
{

output_file.put(next);

for(int i=0; i<=9; i++)
{
input_file >> in_score;
gradetotal+=in_score;
output_file << " " << in_score;
gradecounter++;
}

input_file.get(next);
if(next!='\n') output_file.put(next);
input_file.get(next);

spacecounter=0;
output_file << "\tGrade Total: " << gradetotal/gradecounter << "%" << endl << endl;
}


}

}

closed account (SECMoG1T)
If some people in your file have less than 10 scores, then that means using a loop which iterates for ten times will make your input-file run to the next line which will cause an error you can change from

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for(int i=0; i<=9; i++)
 {
    input_file >> in_score;
    gradetotal+=in_score;
    output_file << " " << in_score;
    gradecounter++;
  }	

//to

  stringstream stringstream_obj;  string str.

/// while (input_file)
  stringstream_obj.clear (); stringstream_obj.str ("");
 getline (input_file, str,'\n')
 stringstream_obj <<str;

  /// get the two names from string stream obj then proceed to next steps
  /// while (stringstream_obj)
  ///      stringstream_obj>> in_score;
   ///      gradetotal+=in_score;
   ///      gradecounter++; 
   /// calc average.  
Last edited on
Topic archived. No new replies allowed.