C++ Reading a file and it's individual characters to output to a table in the console

I have a project due tomorrow that I've been working on for a while and I'm just plain stumped. This is exactly what's required of me:

Perform a few activities with a data file. Create an input text file. Open an output file and the input file. On the screen, display your heading (see first program for what it should contain) Echo all characters read from file to both the console and to the output file. (The output file will contain only a copy of the input file.) On console, also display a “table” with headings showing how many digit, alpha, and other characters appear in the input file. The ‘\t’ character is handy for tabbing between columns. To console, display the total number of characters in the file.

This is just a basic programming course so we aren't using complicated stuff.

I'm not asking for an answer to the problem, I just need help figuring out how to clear my errors and if there are any other major issues that are noticeable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
   string x;
    ifstream input ("input.txt");
    ofstream output ("output.txt");
        if(!input) cout<<"error"; //ensures opened file
    while (!input.eof()){
        input>>x;
        output<<x;
    }
    int digit=0;
    int alpha=0;
    int other=0;
    int count=0;

    input>>x;
    while (x!="-a"){

    if(x>='0'&&x<='9'){
        digit++;
        count++;
    }
    if((x>='a'&&x<='z')||(x>='A'&&x<='Z')){
        alpha++;
        count++;
    }
    if(x>='!'&&x<=')'){
        other++;
        count++;
    }

    x.erase(0,0);
    }
    cout<<"Your file has "<<digit<<" digits"<<endl;
    cout<<"Your file has "<<alpha<<" letters"<<endl;
    cout<<"Your file has "<<other<<" other characters"<<endl;

return 0;}
You have some issues. You should be reading the input file character-by-character not as strings. Also if he wants a "table" you should probably look at the setw() function that displays columnar output. You should by all means do your own work, but if you would like a guide the following does what you want and not much else.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cctype> // for isalpha, isdigit
#include <fstream> // for file i/o
#include <iomanip> // for setw
#include <iostream> // for output to console

using namespace std; // we're only using std namespace

int main()
{
  int digit = 0,alpha = 0,other = 0; // placeholders for character counts
  ifstream in("input.txt"); // input stream
  ofstream out("output.txt"); // output stream
  if(in.good() && out.good()) // both streams are open and ready
  {
    cout << endl << ">> Reading input file: " << endl;
    char c; // placeholder for character
    while(in.get(c)) // while reading a character from the stream is successful
    {
      cout.put(c); // display character to console
      out.put(c); // write character to output stream
      if(isalpha(c)) ++alpha; // is char alpha? incremement
      else if(isdigit(c)) ++digit; // is char numeric? incremement
      else ++other; // char is something else, increment
    }
    cout << endl << ">> End of input file." << endl;
  } else {
    if(!in.good()) cout << "Error opening input file." << endl;
    if(!out.good()) cout << "Error creating output file" << endl;
  }
  cout << endl;
  cout << setw(10) << "Letters: " << setw(10) << alpha << endl; // display 2 columns
  cout << setw(10) << "Digits: " << setw(10) << digit << endl; // display 2 columns
  cout << setw(10) << "Other: " << setw(10) << other<< endl; // display 2 columns
}
Topic archived. No new replies allowed.