Using files..on Macintosh?..

Alright soooooo we're on to using files in our programs..which is kinda hard to understand as they were not explained well to us AT ALL! Anyways, part two of the assignment is to:

Write a program that opens the input file and count the number of 1)upper-case characters, 2)digits, 3)exclamation marks, contained in the file. Display these three counts with appropriate labels.

This is what I have so far, obviously it's wrong as it isn't working and making me feel extremely stupid and aggravated. Little help here guys? What am I doing wrong and how do I begin to fix it? Any and all help is much appreciated!

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
  #include <fstream>
#include <iostream>
using namespace std;



int main()
{
    int upper_case=0, digits=0, exclamation_points=0;
    char next;
    
    ifstream in_stream;
    in_stream.open("/Users/IanHeinze/Downloads/Lab10/test.txt");
    if (in_stream.fail())

    {
        cout << "Input file opening failed.\n";
        return 0;
    }

    in_stream.get(next);
    while (! in_stream.eof())
    {
        in_stream.get(next);

        if (next >= 'A' && next <= 'Z')
            upper_case++;

        else if (next >= '1' && next <= '9')
            digits++;

            else if (next == !)
                exclamation_points++;
    }
      cout << "There are " << upper_case << " upper-case characters." << endl;
      cout << "There are " << digits << " digits." << endl;
      cout << "There are " << exclamation_points << " exclamation points. " << endl;

    return 0;
}
Here is my suggestion; why don't you c++ regex class. It seems like it would help you greatly on doing this assignment.

There are four webpages that i suggest you take a look at (keep in mind that the first to maybe a little complex but they are the best resource as they pertain to exactly what i am talking about):
1. http://www.cplusplus.com/reference/regex/regex_constants/
2. http://www.cplusplus.com/reference/regex/basic_regex/
3. http://www.informit.com/articles/article.aspx?p=2079020
4. https://solarianprogrammer.com/2011/10/12/cpp-11-regex-tutorial/

I hope this helps

- Hirokachi
Try change the big loop (from line 21) to:

1
2
3
4
5
6
7
8
9
while ((in_stream >> next))
{
    if (next >= 'A' && next <= 'Z')
        upper_case++;
    else if (next >= '0' && next <= '9')
        digits++;
    else if (next == '!')
        exclamation_points++;
}


I think the major problem of your code is that exclamation mark. You need to compare with the '!' character, not the ! symbol.
Topic archived. No new replies allowed.