Number of words

Hi, thanks for viewing my question.
I am just starting with programming in C++ and I have an assignment that I am not quite sure how to do. I was able to look up and try out myself bits and pieces like how to open a txt file, how to create a new one with output and such. But other than that I am clueless. The assignment sounds something like this:
Create a program to which you pass a text file with any number of words separated by a colon. The program will create a new file where the letters of the alphabet will be written (from A to Z), each on a new line, followed by a number of words from the input file that begins with the letter of the alphabet.

It is rather a huge jump from a simple calculator I would say, or I am just plain stupid. In either case, I would be grateful if someone pointed me in right direction and/or provided me with some bits of code so that I can finaly finish this headache of mine.
I hope I didn't make too many mistakes in the text above, I am not a native english speaker after all.
Anyway, thanks in advance to anyone who takes his/her time to help me and my friend with this task.
It's not a very big jump. It's just something you haven't done yet.

The format of the input file is not entirely clear. Usually "text" files are divided into lines where each line ends in a newline character ('\n'). But it's not clear that that's the case with your input file. It says that it contains a bunch of words separated by colons, but makes no mention of lines. It's also not clear whether or not the last word has a colon after it.

Normally for a text file I'd say read it line-by-line into a std::string, but you are probably best reading your file character-by-character, stopping when you hit a colon for each word, possibly ignoring whitespace.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>  // isspace(), toupper()
using namespace std;

int main() {
    ifstream fin("yourfile");
    char ch;
    string word;

    while (fin.get(ch))
    {
        if (isspace(ch))  // ignore whitespace (if you want)
        {
            continue;
        }
        else if (ch == ':') // found the end of a word
        {
            char first_letter = toupper(word[0]);
            cout << first_letter << '\n';
            word.clear(); // set to empty string for next word
        }
        else  // character to add to the current word
        {
            word += ch;
        }
    }

    // If the last word didn't have a colon after it,
    // it will be handled here
    if (word.size() > 0)
    {
        char first_letter = toupper(word[0]);
        cout << first_letter << '\n';
    }
}    

Since the block of code that processes a word is duplicated you should make it a function.

Functions from <cctype> can be used to detect whitespace characters (isspace()) and to convert the first character to uppercase (toupper()).

You'll need an array of 26 ints to keep the counts for the letters (alternatively you could use a std::map).

The array can be indexed with uppercase letters like this:
1
2
     // 'A' becomes 0, 'B' becomes 1, ..., 'Z' becomes 25
     ++counts[first_letter - 'A'];

Hello Boreal,

Welcome to the forum.

First read these links on posting code:

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

If you have written any code post it. It makes it easier to figure out what you have done and what needs to be done.

Post the input file or a fair sample if it is large, so everyone is using the same information.

If you have something that shows what the output looks like post it too. This way everyone knows what you need to achieve.

The program is not that hard if you work in small pieces and keep building on what you have started with.

I would likely start with opening the input file, checking that it is open and then reading the file. Then build on from that maybe reading the entire file and just count the words. Counting the words may no be needed, but it will help with the start of the program. Even if you do not need a count it could be useful fo awhile.

Hope that helps,

Andy

Edit:
Last edited on
Hey do you guys have articles on how to code Arduinos and Raspberry Pi?
Whoa, I didn't anticipate that someone would actually find some spare time to help me. Thanks guys! Now to answer some things.
Tpb - I also spent some time thinking about that, but I think that since it is not mentioned in the assignment, that there should be no \n it the text file. The only thing mentioned is that the words are separated by a colon. Nothing else. The last word should probably not have a colon after it.
That is exactly what I need! I had no clue how to read the file char by char. As I noted I my question, I might be plain stupid. Anyway, thanks for the nudge. I will try to somehow Stitch the bits and pieces that I know about now together when I get home.
I am not that sure about the array thought. Or, to put in bluntly, I am not sure if I'll be able to make it work. I'll look up some functions and such once I get home.

Andy - That's true. I forgot about that it might be a good idea to show what I have done. Now to be honest it is not much, most of my codes are pretty... Crappy to say the least. We only had one prior assignment and that was the previously mentioned calculator. I have written some code in my spare time. Not much, but I figure that there is no other way to learn than to use trial and error. Especially with our amount of lessons, which is 2 lessons once per 3 weeks. I am sorry about that. Once again, I can post those codes once I get home.
The input/output should look something like this:
Dragoon:Apple:Queen:King:Prince:Soldier:Building:Animation
(The input should be able to process as many words as possible)
A 2
B 1
C 0
D 1
E 0
F 0
G 0
H 0
I 0
J 0
K 1
M 0
N 0
O 0
P 1
Q 1
R 0
S 1
T 0
U 0
V 0
W 0
X 0
Y 0
Z 0
Hope this helps.

Let me take a moment to thank you both once more. It is much appreciated!

using tpb's strategy, just with a flag to capture next. Save from bad characters with isalpha()
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() 
{
    ifstream fin("words.txt");
    char c;
    int freq[26]{0};
    bool capture_next = true;

    while (fin.get(c))
    {
        if (capture_next && isalpha(c))
        {
            freq[toupper(c)-'A'] += 1;
            capture_next = false;
        }
        else if (c == ':')
        {
            capture_next = true;
        }
    }

    for (int i=0; i<26; ++i)
        cout << (char)(i+'A') << " " << freq[i] << endl;
    
    return 0;
}

That's quite a nice solution right there icy1. Even though I have already succesfully finished this assignment, I've learned something new from you. Thank you very much. It will make my future work a bit more efficient.
Topic archived. No new replies allowed.