0

been learning C++ for couple months and I have now a problem with code that keeps exiting with 0 whenever I try to compile and doesn't give me anything. The idea is to take information from the file, this code is an attempt at parsing it (I used cin and ignore.cin() instead of getline() becuase the file is structured like "type" "food/feeling" "date" end of line). Whenever I run though, there is no error but the compiler says the code exited with 0

My code is posted below, any help is appreciated!

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

struct Day
{
    string date;
    vector<string> feeling;
    vector<string> food;
};

vector<Day*>* makeFile(struct Day, string);

int main()
{ 
    cout << "Please input file name" << endl;
    string fileName;
    cin >> fileName;

    vector<Day*>* makeFile(Day);
}

//function to read data from file and input to vectors

vector<Day*>* makeFile(struct Day, string fileName)
{
    vector<Day*>* days = new vector<Day*>;
    vector<string>masterFood;
    vector<string>masterFeel;
    vector<string>masterDate;
    string date;
    string newDate;
    string type;
    string foodName;
    string feelingType;
    bool exists = false;
    
    ifstream fin(fileName);
    
    while (exists = false)
    {
        if (fin.is_open())
        {
            exists = true;
            Day* d = new Day;
            
            cin >> type;
            cin.ignore();
            if (type == "food")
            {
                cin >> foodName;
                d->food.push_back(foodName);
                masterFood.push_back(foodName);
            }
            else if (type == "feeling")
            {
                cin >> feelingType;
                d->feeling.push_back(feelingType); 
                masterFeel.push_back(feelingType);
            }
            cin.ignore();
            cin >> date;
            d->date = date;
            days->push_back(d);
            
            while (!fin.eof())
            {
                cin >> type;
                cin.ignore();
                if (type == "food")
                {
                    cin >> foodName;
                    d->food.push_back(foodName);
                }
                else if (type == "feeling")
                {
                    cin >> feelingType;
                    d->feeling.push_back(feelingType);
                }
                cin.ignore();
                cin >> newDate;
                if (newDate != date)
                {
                    Day* d = new Day;
                }
            }
        }
        else
        {
            cout << "Please enter valid file name" << endl;
        }
    }
    return days;
}
  • 4
    `vector* makeFile(Day);` is not how you call a function. It's a declaration. You may wish to consult your favorite C++ textbook on the correct syntax for making a function call. – Igor Tandetnik Nov 12 '22 at 02:28
  • 1
    Worth reading: [Why is `iostream::eof` inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Evg Nov 12 '22 at 02:47
  • Whole lot of excess pointers and `new` here. There's parts where you create an object with `new` and then immediately leak the memory without doing anything at all with it. – Nathan Pierson Nov 12 '22 at 02:59
  • 1
    *...been learning C++ for couple months...* Don't use `new`, it's an advanced C++ feature (and even then, there is really no reason to use it directly; use it indirectly via `std::vector` or `std::make_unique` for `std::unique_ptr`). Unless your making your own smart pointer or container ... in which case that's super-advanced C++. – Eljay Nov 12 '22 at 04:02
  • *"and doesn't give me anything"* -- that is odd, as I would expect that you get `Please input file name` and an opportunity to provide a word of input. Getting nothing would indicate something wrong with your tool chain, or perhaps indicate that you are trying just to compile, not compile and run. Have you gotten any programs to produce output? How about a simple program like `#include int main() { std::cout << "Hi\n"; }`? – JaMiT Nov 12 '22 at 05:40
  • 1
    *" there is no error but the compiler says the code exited with 0"* -- exiting with 0 is traditionally used to indicate successful completion, so there is no contradiction here ("and" would be more appropriate than "but"). On the other hand, it might be good to clarify what exited with 0. If your compiler exited with 0, that would simply indicate that your code was successfully compiled. If your program exited with 0, then your compiler is not involved at that point ("compile" and "run" are disjoint steps). – JaMiT Nov 12 '22 at 05:44

1 Answers1

0

Some problems:

while (exists = false)

will exit the while loop immediately and not do anything - you're assigning false to exists, not checking if they're equal. I suspect that's why you're getting an "exited with zero" message. Do exists == false instead.

vector<Day*>* makeFile(Day);

is not how you call a function. It's makeFile(/*arguments*/) that calls it. What you have is a function declaration not call.

On the topic of makeFile, what is makeFile(struct Day, string fileName) supposed to do? Why are you passing a struct Day variable if you won't use it? Just do makeFile(string fileName).

What are you doing with fin? I don't see a single operation done on fin in the code, and the parts where it is used don't make sense. Unless you're planning on using fin, I suggest cutting it out. Also, what does masterFood, masterFeel, and masterDate do? They don't serve much of a purpose - you're adding elements to them, but it's not clear for what reason.

In

if (newDate != date)
{
   Day* d = new Day;
}

It's not clear why you're allocating a new Day. In C++, variables go out of what is called scope. d becomes inaccessible right after the if statement is exited, and you've just leaked memory that you haven't deleted. Perhaps you meant to do d = new Day, which would make more sense.

IMPORTANT: REMEMBER TO DELETE MEMORY YOU AREN'T USING. There are many instances in this code where you are using memory that you aren't deleting. For example,

if (fin.is_open())
{
    exists = true;
    Day* d = new Day;
    //...

d never gets deallocated, resulting in a memory leak. Make sure to do delete d when you don't need the memory anymore:

if (fin.is_open())
{
    exists = true;
    Day* d = new Day;
    //...

    delete d;      //this frees up the memory d occupies for the computer to use
}

Edit: There's also a lot of pointers which I think you can remove. For example, I don't think it would be too hard to change vector<Day*>* days to just vector<Day> days.