String to int

I'm reading input from a file and I need to input part of it into an integer.

The problem is I'm using substrings to read everything in and when I try to convert the string it gives me a casting error, so now I know it's not possible through straight casting.

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
for(colNum = 0; colNum < NUM_COLS; colNum++)
	{
	for(rowNum = 0; rowNum < NUM_ROWS; rowNum++)
	{
	if (orgstring[rowNum][colNum] == "HOGWARTS")
		fileName = "hogwarts.in";
		else if (orgstring[rowNum][colNum] == "DEATH_EATERS")
		fileName = "death_eaters.in";
		else if (orgstring[rowNum][colNum] == "MINISTRY_OF_MAGIC")
		fileName = "ministry_of_magic.in";

	inFile.open(fileName.c_str());
	getline(inFile, dummy);
	value = dummy.find(wizData[rowNum][colNum].wizName);
	while (value != 0)
	{
	getline(inFile, dummy);
	value = dummy.find(wizData[rowNum][colNum].wizName);
	}

	index = dummy.find("#");
		dummy = dummy.substr(index+1, dummy.length() - 1);

	index = dummy.find("/");
		wizData[rowNum][colNum].bMonth = int(dummy.substr(0, index));
		dummy = dummy.substr(index+1, dummy.length() - 1);

	index = dummy.find("/");
		wizData[rowNum][colNum].bDay = int(dummy.substr(0, index));
		dummy = dummy.substr(index+1, dummy.length() - 1);

	index = dummy.find("#");
		wizData[rowNum][colNum].bYear = int(dummy.substr(0, index));
		dummy = dummy.substr(index+1, dummy.length() - 1);

	index = dummy.find("#");
	if (orgstring[rowNum][colNum] == "HOGWARTS")
	{wizData[rowNum][colNum].house = dummy.substr(0,index);
		dummy = dummy.substr(index+1, dummy.length() - 1);}
		else if (orgstring[rowNum][colNum] == "MINISTRY_OF_MAGIC")
		{wizData[rowNum][colNum].department =  dummy.substr(0,index);
		dummy = dummy.substr(index+1, dummy.length() - 1);}
		else
		{}	//nothing happens

		//dummy = dummy.substr(index+1, dummy.length() - 1);

	index = dummy.find("#");
		wizData[rowNum][colNum].determination = int(dummy.substr(0, index));
		dummy = dummy.substr(index+1, dummy.length() - 1);

	index = dummy.find("#");
		wizData[rowNum][colNum].gender = dummy.substr(0,index);
		dummy = dummy.substr(index+1, dummy.length() - 1);

	index = dummy.find("#");
	if (orgstring[rowNum][colNum] == "HOGWARTS")
		{wizData[rowNum][colNum].rankH = dummy.substr(0,index);}
		
		else if (orgstring[rowNum][colNum] == "MINISTRY_OF_MAGIC")
			{wizData[rowNum][colNum].rankD =  dummy.substr(0,index);}
		
		else
			{wizData[rowNum][colNum].rankM =  dummy.substr(0,index);}
		


	inFile.close();
	}}


part of the input file here.
1
2
3
4
5
6
7
8
9
Albus Dumbledore#07/31/1881#Gryffindor#10#m#Nicolas Flamel#Faculty#
Firenze#03/07/1931#Ravenclaw#3#m#Chiron#Faculty#
Filius Flitwick#08/25/1890#Ravenclaw#8#m#Helena Ravenclaw#Faculty#
Rubeus Hagrid#05/13/1929#Gryffindor#4#m#Albus Dumbledore#Faculty#
Remus Lupin#03/10/1960#Gryffindor#6#m#Albus Dumbledore#Faculty#
Minerva McGonagall#10/04/1925#Gryffindor#9#f#Albus Dumbledore#Faculty#
Aurora Sinistra#12/12/1920#Ravenclaw#2#f#Filius Flitwick#Faculty#
Horace Slughorn#05/10/1901#Slytherin#3#m#Phineas Black#Faculty#
Severus Snape#01/09/1960#Slytherin#9#m#Horace Slughorn#Faculty# 


I'm wondering if there is a way to read an int from a string.
Last edited on
Here is one way:
1
2
3
4
5
6
7
8
9
10
#include <sstream>

void func()
{
    int i;
    std::string str;
    // ...

    std::istringstream(str) >> i; // convert string str to int i
}

http://cplusplus.com/reference/iostream/istringstream/
Topic archived. No new replies allowed.