-1
// C++ program to count the uppercase
#include<iostream> 
#include<fstream>
#include <string>
using namespace std;

// Function to count uppercase
int Count ( string str )
{
    int upper = 0;
    for (int i = 0; i < str.length(); i++) {
      if (str[i] >= 'A' && str[i] <= 'Z')
        upper++;
    }
    return upper;
}

// Driver function 
int main()
{
    //Open the file
    ifstream openedFile;

    //This is how you turn the potential file into an actualized file inside the defualt directory.
    openedFile.open ( "random words.txt" );

    string str[10001]; int i = 0;
    int uppercase = 0;
    
    while (!openedFile.eof())
    {
        getline ( openedFile, str[i], '\n');
        uppercase = Count(str[i]);
        if (Count(str[i]) == 1) uppercase++;
        if (Count(str[i]) == 3) uppercase++;
        if (Count(str[i]) == 2) uppercase++;
        cout << Count(str[i]) << "\n";
    }

    cout << "Uppercase letters: " << uppercase << endl;

    //Close the file
    openedFile.close ();
}

It shows that there are occurences of capital letters. sometimes even 3 in a line. it doesn't add to the uppercase variable though.

Bill Lynch
  • 72,481
  • 14
  • 116
  • 162
TripHazeX
  • 5
  • 1
  • 1
    `while (!openedFile.eof())` can cause you to read 1 extra line. [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Oct 10 '20 at 22:45
  • 1
    What is the purpose of `uppercase = Count(str[i]); if (Count(str[i]) == 1) uppercase++; if (Count(str[i]) == 3) uppercase++; if (Count(str[i]) == 2) uppercase++;`? – cigien Oct 10 '20 at 22:45
  • `string str[10001]` do you really need an array for this? – drescherjm Oct 10 '20 at 22:46
  • What's the array for if `i` never changes and there's no reason to keep the data around? – Retired Ninja Oct 10 '20 at 22:46
  • You'll probably never see a case where it fails, but `if (str[i] >= 'A' && str[i] <= 'Z')` counts on a character set with A-Z in a contiguous block with A first and Z last, somthiing that is not guaranteed. – user4581301 Oct 10 '20 at 22:52
  • Why's my question downvoted? I don't know how to answer most of these questions. I just try a bunch of stuff cause they seem to be my best effort, I can't find anything in the books or other places online that helps. – TripHazeX Oct 11 '20 at 01:08
  • use `int Count ( const string &str )` to avoid copy the string unnecessarily – phuclv Oct 11 '20 at 01:17
  • @TripHazeX *I can't find anything in the books or other places online that helps* -- C++ is one of the most complex computer languages out there. When you say "in the books", exactly what books are you referring to? Also, a lot of online C++ websites are absolute garbage -- the only ones worth going to are ones that have been peer-reviewed by experts, or run by expert C++ programmers. Last, if you take a look at my answer to your question, you can easily gain a lot of insight on the various algorithm functions such as `count_if`. – PaulMcKenzie Oct 11 '20 at 02:03

2 Answers2

2

In case others may be searching for the title of this thread, here is the solution using the algorithm functions and streams.

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cctype>
#include <fstream>

int main()
{
    std::ifstream openedFile("random words.txt");
    
    std::cout << "Uppercase letters: " 
        <<  std::count_if(std::istream_iterator<char>(openedFile), 
                          std::istream_iterator<char>(), 
                          [](char ch) 
                            {return std::isupper(static_cast<unsigned char>(ch));}
                         ) 
        << "\n";
}

Using the std::count_if algorithm, the file can be read in, using the stream iterators and each character read is sent to the lambda function.

In the lambda function, it is checked to see if the character is a upper case. The std::count_if will then return the count.

PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38
1

You are overwriting the value of uppercase variable on the next iteration: uppercase = Count(str[i]);. Just use += instead and remove those if (Count(str[i]) == X) uppercase++;

Additionally, that array of 1001 strings, of which you only use the first entry, isn't needed at all. You can just declare string str and replace str[i] with str inside main.

Ángel
  • 820
  • 7
  • 12