Having trouble converting string to int.

closed account (zbX9z8AR)
Whenever I compile, it says that stoi ws not declared in this scope. How can I fix line 47 so that I can convert the string to an integer?

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
void load_stream(ifstream& in_file, student* s, course* c, enrollment* e)
{
	float *student1;
	string coursename, temp;
	char input[20];
	int coursenumber, grades[10], num=0, studentenrollment=0, courseenrollment=0;

	cout << "File name: ";
	cin >> input;

	//open file to load
	in_file.open(input);
	//if opening failed
	if(in_file.fail())
	{
		cout << "Failed to open file.\n";
		exit(EXIT_FAILURE);
	}

	//gets the number of courses that are about to be added
	getline(in_file, temp);

	//for every course to be added
	for(int i=0; i<num; i++)
	{
		//get the name of the course
		getline(in_file, temp, ' ');
		c[i].name = temp;
		//get the course number
		getline(in_file, temp);
		//convert the number from string to int
		temp = c[i].id;
		num_courses++;
	}

	//get the number of students to be enrolled
	getline(in_file, temp);
	//read student information from in_file
	for(int i=0; i<num; i++)
	{
		//gets students last name, first name, and id
		getline(in_file, temp, ' ');
		s[i].lastname = temp;
		getline(in_file, temp, ' ');
		s[i].firstname = temp;
		getline(in_file, temp);
		s[i].id = stoi(temp);
		num_students++;

		//checks each enrolled class
		for(int j=0; j<s[i].classcount; j++)
		{
			student1 = &e[i].grades[0];
			s[i].classcount++;

			//read each class
			getline(in_file, temp, ' ');
			coursename = temp;
			getline(in_file, temp, ' ');
			coursenumber = stoi(temp);

			for(int n=0; n<(num_grades-1); n++)
			{
				getline(in_file, temp, ' ');
				grades[n] = stoi(temp);
			}
			getline(in_file, temp);
			grades[num_grades-1] = stoi(temp);

			//searches for course
			for(int k=0; k<=num_courses; k++)
			{
				//if course is found
				if(coursename == c[k].name && coursenumber == c[k].id)
				{
					//get how many students are in this course
					courseenrollment = c[k].roll;
					//check course enrollment counter
					for(int l=0; l<courseenrollment; l++)
					{
						//points the roll to student gradebook address
						c[k].enrolled_students[l] = student1;
						//add one to class counter
						c[k].roll++;
					}

					//if enrollment address matches student's first gradebook
					if(student1 == &e[i].grades[0])
					{
						for(int n=0; n<(num_grades-1); n++)
						{
							e[i].grades[n] = grades[n];
						}
						e[i].grades[num_grades-1] = grades[num_grades-1];
					}
				}
			}
		}
	}
	return;
}
Last edited on
Some compilers don't support std::stoi for some reason, you can use this hack as a workaround:
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
#include <iostream>
#include <string>
#include <cctype>
#include <vector>

const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int main()
{
    std::string myString{};
    std::vector<int> v{};
    std::cout << "Enter your string \n";
    getline(std::cin, myString);
    for (const auto& elem : myString)
    {
        if(std::isdigit(elem))
        {
            int temp = arr[elem - '0'];
            v.push_back(temp);
        }
    }
    for (const auto& elem : v)
    {
        std::cout << elem << " ";
    }
}

Topic archived. No new replies allowed.