### How to implement my variables into my output file?

Creating a program which counts the number of A's B's C's D's E's and F's in one of Plato's works and then create another file (called statistics.txt) which lists the different values of each letter. I believe I've created the code necessary to calculate this, however I'm not exactly sure how to insert the values I've found into the output file. Should be pretty easy to answer. Thanks!

Here's the code for what its worth...

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, const char * argv[])
{
int numA = 0;
int numB = 0;
int numC = 0;
int numD = 0;
int numE = 0;
int numF = 0;
char c = ' ';

ifstream myFile;
myFile.open("plato.txt");

if(!myFile)
{
cout << "Error: File could not be found:" << endl;

}

while (myFile.good())
{
c = myFile.get();

if ((c == 'a' || c == 'A'))
{
++numA;
}
if ((c == 'b' || c == 'B'))
{
++numB;
}
if ((c == 'c' || c == 'C'))
{
++numC;
}
if ((c == 'd' || c == 'D'))
{
++numD;
}
if ((c == 'e' || c == 'E'))
{
++numE;
}
if ((c == 'f' || c == 'F'))
{
++numF;
}

}

{
ofstream newFile;
newFile.open ("statistics.txt");

//This is where I'm getting lost

newFile << "Total Number of A's is" << numA << \n << << "Total Number of B's is" << numB << \n<< "Total Number of C's is" << numC << \n<< "Total Number of D's is" << numD << \n<< "Total Number of E's is" << numE << \n << "Total Number of F's is" << numF << \n;
newFile.close();
}

myFile.close();
cin.get();
return 0;
}
you dont need the "{" above `ofstream newFile;` and "}" below `newFile.close();`

You are missing double quotes around \n and you have extra "<<" in the statement

improvement/alternative:
Consider using switch statement like
 ``12345678`` ``````switch( topper(c) ) { case 'A' : ++numA; break; //Other cases }``````

OR put else like
 ``12345678910`` ``````if ((c == 'a' || c == 'A')) { ++numA; } else if ((c == 'b' || c == 'B')) { ++numB; } //Other cases to follow similarly ``````
Topic archived. No new replies allowed.