C++ While loop scoping issue

  c++, loops, scope, while-loop

Hey there,

I recently started learning C++, and I’m currently trying build a tool that I recently built in python. My issue is that I can’t figure out how I can make the length of the list of words global for example:

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[]) {
    ....
        fstream file;
        file.open(argv[i], ios::in);
        if (file.is_open()) {
            string tp;
            while (getline(file, tp)) {
                // cout << tp << "n" << endl;
                string words[] = {tp};
                int word_count = sizeof(words) / sizeof(words[0]);
                for (int e = 0; e < word_count; e++) {
                    cout << word_count[e];
                }
            }
            file.close();
            } else {
                cout << "E: File / Directory " << argv[i] << " Does not exist";
                return 0;
            }
    ....
}

Where it says int word_count = sizeof(words) / sizeof(words[0]); and tring words[] = {tp};, I want to be able to use that globaly so that I can then later use then length of the array and the array itself later on so that I can loop through it and use them in another statement.

Can someone tell me how to do so?
And by the way, I’ve only been doing C++ for about 4 days so please dont get annoyed if I don’t understand what you tell me.

Source: Windows Questions C++

LEAVE A COMMENT