Nothing happening within the code?

Write your question here.

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
37
38
39
40
41
42
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

void openFile(ifstream&);

int main()
{
  
  ifstream inData;
  
  openFile(inData);
  
    return 0;
}

void openFile(ifstream &inData)
{
  char input;
  int alpha = 0, digits = 0, chr = 0;
  
  inData.open("data.txt");
  
  while (inData)
{
  if (isalpha(input))
    alpha++;
  else if (isdigit(input))
    digits++;
  else if (isspace(input))
    chr++;
}
    cout << "Alpha = " << alpha << "\nDigits = " << digits << "\nCharacters = " << chr << endl;
  
  if (!inData)
  {
    cout << "\nError";
    exit(13);
  }
}


This is the file that is supposed to be read
1
2
3
4
5
6
7
8
9
1 
2 
3 
!
@
#
a
b 
c
Last edited on
You need to actually read something from the file.

Line 26:
 
    while (inData)

should be
 
    while (inData >> input)

After that loop has completed, inData.fail() will be set and so the error message will be output at line 39.
Topic archived. No new replies allowed.