terminate called after throwing an instance of 'std::invalid_argument'

So I'm having a weird issue, The compiler doesn't seem to like it when I use stoi in my current project. I have used this exact code before and it complied and worked but with this project it seems to throw out and error stating:

"terminate called after throwing an instance of 'std::invalid_argument'
what (): stoi:"

Debugging the code I confirmed that comment out the code that involves stoi removes the issue so I know for sure that the lines containing stoi are the issue. My first guess was that the issue was that I wasn't compiling in c++ 11 so I added that into the compiler but I still get the same issue. Maybe it has something to do with my header? Maybe I just need an alternative to stoi?

Heres the code:

Main.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include "MovieTree.h"

using namespace std;

int main()
{
    //declarations
    char *fileName; //stores the file we will be opening
    int input; //takes a number from the main menu
    string t; //used for cin of a movie title in if statement 1
    bool is_running = true; //keeps the while loop running and if set to false shuts down the program

    //opening a file
    fileName = "Assignment5Movies.txt"; //argv[1];
    ifstream myFile;
    myFile.open(fileName);

    MovieTree *tree = new MovieTree;

    //opening and reading the file
    if (myFile.is_open());
    {
        while (!myFile.eof())
        {
            string ranking;
            getline(myFile,ranking,',');
            int rankig = stoi(ranking);

            string title;
            getline(myFile,title,',');

            string year;
            getline(myFile, year, ',');
            int yearInt = stoi(year);

            string quantity;
            getline(myFile,quantity);
            int quanInt = stoi(quantity);

            tree -> addMovieNode(rankig, title, yearInt, quanInt);
            //cout<<rankig<< title << yearInt<< quantity;       //FOR TESTING PURPOSES
            }
    }

    while(is_running == true)
    {
        //Main Menu
        cout << "======Main Menu=====" << endl;
        cout << "1. Find a movie" << endl;
        cout << "2. Rent a movie" << endl;
        cout << "3. Print the inventory" << endl;
        cout << "4. Quit" << endl;
        cin >> input;
        if(input == 1)
        {
            std::cout<<"Enter title:"<<std::endl;
            getline(std::cin, t);
            tree->findMovie(t);
        }
        if(input == 2)
        {
            std::cout<<"Enter title:"<<std::endl;
            getline(std::cin, t);
            tree->rentMovie(t);
        }
        if(input == 3)
        {
            tree->printMovieInventory(tree->getRoot());
        }
        if(input == 4)
        {
            std::cout<<"Goodbye!"<<std::endl;
            is_running = false;
        }
    }
    myFile.close();
    return 0;
}


I'll add the header and function page if it helps

Header
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
38
39
40
41
42
43
44
45
#ifndef MOVIETREE_H
#define MOVIETREE_H


struct MovieNode{
    int ranking;
    std::string title;
    int year;
    int quantity;
    MovieNode *parent;
    MovieNode *leftChild;
    MovieNode *rightChild;

    MovieNode(){};

    MovieNode(int in_ranking, std::string in_title, int in_year, int in_quantity)
    {
        ranking = in_ranking;
        title = in_title;
        year = in_year;
        quantity = in_quantity;
    }

};

class MovieTree
{
    public:
        MovieTree();
        virtual ~MovieTree();
        void printMovieInventory();
        void addMovieNode(int ranking, std::string title, int releaseYear, int quantity);
        void findMovie(std::string title);
        void rentMovie(std::string title);
        void printMovieInventory(MovieNode * node);
        bool movieFound = false;
        MovieNode* getRoot();
    protected:
    private:
        void DeleteAll(MovieNode * node);
        MovieNode* searchMovieTree(MovieNode * node, std::string title);
        MovieNode *root;
};

#endif // MOVIETREE_H 


Functions page
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <string>
#include "MovieTree.h"


MovieTree::MovieTree()
{
    //ctor
}

MovieTree::~MovieTree()
{
    //dtor
}

MovieNode* MovieTree::getRoot()
{
    return root;
}

void MovieTree::addMovieNode(int ranking, std::string title, int in_year, int in_quantity)
{
    if(root == NULL)
    {
        root = new MovieNode(ranking, title, in_year, in_quantity);
    }
    else
    {
        MovieNode *temp = root;
        while(true)
        {

            if(temp->title.compare(title) > 0)
            {
                if(temp->leftChild != NULL)
                {
                    temp = temp->leftChild;
                }
                else
                {
                    temp->leftChild = new MovieNode(ranking, title, in_year, in_quantity);
                    break;
                }
            }
            else
            {
                if(temp->rightChild != NULL)
                {
                    temp = temp->rightChild;
                }
                else
                {
                    temp->rightChild = new MovieNode(ranking, title, in_year, in_quantity);
                    break;
                }
            }

        }
    }
}

void MovieTree::findMovie(std::string title)
{

    if(searchMovieTree(root,title) == NULL)
    {
        std::cout<<"Movie not found."<<std::endl;
    }

    else
    {
        std::cout<<"Movie Info:"<<std::endl;
        std::cout<<"==========="<<std::endl;
        std::cout<<"Ranking:"<<searchMovieTree(root,title)->ranking<<std::endl;
        std::cout<<"Title:"<<searchMovieTree(root,title)->title<<std::endl;
        std::cout<<"Year:"<<searchMovieTree(root,title)->year<<std::endl;
        std::cout<<"Quantity:"<<searchMovieTree(root,title)->quantity<<std::endl;
    }


}

void MovieTree::rentMovie(std::string title)
{

    if(searchMovieTree(root,title)->quantity == 0)
    {
        std::cout<<"Movie out of stock."<<std::endl;
    }

    else
    {
        std::cout<<"Movie has been rented."<<std::endl;
        searchMovieTree(root,title)->quantity--;
        findMovie(title);
    }

}

MovieNode* MovieTree::searchMovieTree(MovieNode *node, std::string title)
{

    node = root;
    while(true)
        {
            if(node->title.compare(title)==0)
            {
                return node;
            }

            if(node->title.compare(title) > 0)
            {
                if(node->leftChild != NULL)
                {
                    node = node->leftChild;
                }
                else
                {
                    return NULL;
                    break;
                }
            }
            else
            {
                if(node->rightChild != NULL)
                {
                    node = node->rightChild;
                }
                else
                {
                    return NULL;
                    break;
                }
            }

        }

}


void MovieTree::printMovieInventory(MovieNode *m)
{

    if(m->leftChild != NULL)
    {
        printMovieInventory(m->leftChild);
    }

    std::cout<<"Movie: "<<m->title<<'\n';

    if(m->rightChild != NULL)
    {
        printMovieInventory(m->rightChild);
    }


}


EDIT: Small typo caused massive issue, needed to fix.
Last edited on
Stoi will throw a std::invalid_argument exception if you pass a string that is not formatted as an integer (e.g. an empty string).
Oh god I'm dumb, I think I forgot to add the .txt file I want to use into the folder for this project and that makes the string empty. Working on fixing that.

EDIT: Yep that fixed my problem, thank you for the help.
Last edited on
Topic archived. No new replies allowed.