-1

I'm trying to take the profile info(username, email, etc.) from one directory and put it in another. I've been debugging the code for this program, and while there are no errors, the program won't run, saying that the program "has stopped working". I have already looked on this website and others for any possible answers, and found none.

#include <string>
#include <cstring>
#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <iomanip>
#include <filesystem>
using namespace std;

class path{
public:
string parent_directory;
string root_directory;
};

class Data{
public:
string userName;
string nickName;
string fName;
string arena_FName;
string lName;
string arena_LName;
string email;
string arenaEmail;

friend std::istream& operator>>(std::istream& input, Data& d);
};
std::istream& operator>>(std::istream& input, Data& d){
std::getline(input, d.userName);
std::getline(input, d.nickName);
//...
std::getline(input, d.arenaEmail);
return input;
}

int main(){

ifstream myfile("myfunk.txt", ios::in);

ofstream arena("arena.txt");

myfile.open("myfunk.txt", ios::in);
if(myfile){
    cout << "Input file open." << endl;
}

arena.open("arena.txt", ios::out | ios::app);
if(arena){
    cout << "Output file open." << endl;
}

cout << "file opening test: success" << endl;

int x = 0;
int y = 4101;       //Total number of users in the directory.
int z = 0;          //For inputting the required lines of info for each profile.
int profile = 0;
bool valid = false;
string role;
//string arenaRole;
bool post = false;
string line;
string p = "This PC/..."; //Path to the folder of the individual pictures.
//myVar.save("...");
string p = "...";
path path1;
path root_directory;
path parent_directory;
//bool is_directory(const std::filesystem::path& p, std::error_code& ec) noexcept; //Checks if current location is a directory.
//bool postPic;
const unsigned int MAXIMUM_DATA = 4100u;
Data database[MAXIMUM_DATA];

cout << "All variables but the filesystem have been accepted!  Please install this program on the network." << endl;

while(x < y){
    cout << "Primary loop functioning" << endl;

    if(post = true){
        getline(myfile, line); //Grab and read next line.
        myfile >> line;
        line = userName[x];
        arena << "Username: " << userName[x] << "\n";
        z++;

        getline(myfile, line);
        myfile >> line;
        line = role[x];
        arena << "Role: " << role[x] << "\n";
        z++;

        getline(myfile, line);
        line = nickName[x];
        myfile >> nickName[x];
        arena << "nickname: " << nickName[x] << "\n";
        z++;

        getline(myfile, line);
        line = fName[x];
        myfile >> fName;
        arena << "First Name: " << fName[x] << "\n";
        z++;

        getline(myfile, line);
        line = lName[x];
        myfile >> lName;
        arena << "Last Name: " << lName[x] << "\n";
        z++;

        getline(myfile, line);
        myfile >> line;
        line = email[x];
        arena << "Email: " << email[x] << "\n";
        getline(myfile, line);
        z = 0; //Next profile...
    }

    int data;
    while(myfile >> data){
        if(nickName[x] = NULL){
            myfile >> "<Error> Some required information is missing! Contact user! </Error> /n";
            valid = false;
            post = false;
            x++;
            }

            if(email[x] != NULL){
                std::string str("@");
                std::string str2(".com");
                std::string str3(".net");
                std::string str4(".edu");
                if(std::size_t found = email[x].find(str) & (std::size_t found = email[x].find(str2) || std::size_t found = email[x].find(str3) || std::size_t found = email[x].find(str4)){
                    valid = true;
                    if(valid = true){
                        post = true;
                    }
                }

                else{
                    valid = false;
                    post = false;
                    x++;
                }
            }
        }
        }
    }

}
x++;
}

    //x++;

myfile.close(); //Closes the file in the directory.
arena.close();  //Closes the file in Arena.

return 0;
}
  • Welcome to Stack Overflow, @Sora-Knight. You may want to review: https://stackoverflow.com/help/how-to-ask With this information and other help links, you may be able to edit this question to that others are able to provide you with a good answer. In particular, you should reduce this to one question, feel free to enter other questions. You should post code that succinctly displays the one question problem you are encountering: https://stackoverflow.com/help/mcve – Degan Sep 12 '17 at 18:32
  • Unrelated: You have it commented out, but `while(! myfile.eof())` will lead you to grief. Explanation here: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Sep 12 '17 at 18:33
  • I'm not up on my C++17, but `using namespace std::experimental::filesystem::v1;` may be biting you. `filesystem` has a `path` class that you have pulled into the global namespace and into conflict with your `path` class. – user4581301 Sep 12 '17 at 18:35
  • Thank you for the response, @Degan! I have just edited the question with my latest version of the code, with only the relevant parts (no commented lines, debug tests, etc.)As for the .eof, I have edited it to what was suggested, but its not shown here. – Sora Knight Sep 12 '17 at 19:48
  • @user4581301, I just tried removing the namespace part that you mentioned, and when I did, there was an error with the parent_directory string. Any ideas how to get past this? – Sora Knight Sep 12 '17 at 19:50
  • `#include ` is odd. There is no uppercase S String in C++. You should be getting a "Can't find header" error unless your development is Visual Studio and it is trying to include `String` from the .Net libraries instead of `string` from the C++ standard libraries. Replace `#include ` with `#include ` and see what happens. – user4581301 Sep 12 '17 at 19:57
  • You may want to have an array of 4100 profile classes rather than having array of each attribute. See also `std::vector`. – Thomas Matthews Sep 12 '17 at 20:02
  • Your assignments may be reversed, e.g. `myfile >> line; line = lName[x];` The file read places data into `line`. The next statement replaces `line` with the data from the array, making the input unnecessary. – Thomas Matthews Sep 12 '17 at 20:04
  • What's the purpose of the `z` variable? It is never assigned to anything, passed to a function or printed. In fact, it is set to 0 before the end of the while loop. – Thomas Matthews Sep 12 '17 at 20:07
  • Evil: two variables with same name, differing only by one capital letter. Readers won't know if there is a typo or not. Choose names the differ regardless of case of the letters. See `parent_directory` vs. 'parent_Directory`. – Thomas Matthews Sep 12 '17 at 20:09
  • In your `extension` function, what happens when the string doesn't have an extension? If you are hard coding data, you can simplify the function by returning "txt" rather than extracting it from a string. – Thomas Matthews Sep 12 '17 at 20:11
  • Where is the overloaded assignment operator for your `path` class? How does the compiler know which string member receives the string in the assignment statement? – Thomas Matthews Sep 12 '17 at 20:13
  • Read up on escape characters. "This PC\Desktop\myfumc.txt" needs to be double-slashed to work correctly. – user4581301 Sep 12 '17 at 20:18

1 Answers1

0

Let's rework your code.
First, let's create a data structure for the data:

class Data
{
  public:
    string userName;
    string nickName;
    string fName;
    string arena_FName;
    string lName;
    string arena_LName;
    string email;
    string arenaEmail;
};

If you need an array for the data, it would be declared as:

const unsigned int MAXIMUM_DATA = 4100u;
Data database[MAXIMUM_DATA];

Next, let's overload the extraction operator>> to make reading easier:

class Data
{
  public:
    //...
    friend std::istream& operator>>(std::istream& input, Data& d);
};
std::istream& operator>>(std::istream& input, Data& d)
{
  std::getline(input, d.userName);
  std::getline(input, d.nickName);
  //...
  std::getline(input, d.arenaEmail);
  return input;
}

This simplifies your input loop to:

std::vector<Data> database;
Data d;
while (my_file >> d)
{
  database.push_back(d);
}

You can query the amount of data read in by using the std::vector::size() method, i.e. database.size().

Also, you don't need a separate structure for a file path. A simple std::string will suffice. I recommend using forward slash, '/', because it is recognized by both Windows and *nix operating systems and won't be interpreted as an escape character.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144
  • thank you very much for the help! The errors have started to disappear due to implementing some of your coding! On the z variable, I am using it to read each file in the directory, line by line, and the z variable's value determines under which category that line is to be filed. Also, on the input loop, I need it to only input valid profile info. Also, some profiles have some info missing that, while required, the user simply can't provide, i.e. no phone number or email. As such, I'm afraid I can't simplify the code down that far. – Sora Knight Sep 14 '17 at 15:15
  • @ user4581301, thank you for mentioning the '#include' part as I did not realize that was an error. I have now fixed it. – Sora Knight Sep 14 '17 at 15:17