Problem converting a string to an int with stringstream

I am having trouble converting a string to an integer with istreamstream. I've researched it, tried several different methods, and none seem to work. When I go through the code with the debugger, everything runs smoothly until the actual insertion from the stringstream to the int variable. From what I've ben able to find online, this should work. Any suggestions would be very appreciated.

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
#include <iostream>
#include <string>
#include <time.h>
#include <sstream>

using namespace std;

int main (int argc, char * const argv[]) {

	//get the current time as a string
	time_t rawTime;
	struct tm * localTime;
	time (&rawTime);
	localTime = localtime(&rawTime);
	string currentTime = asctime(localTime);
	cout << "\tIt is currently: " << currentTime << "\t\tTea time!" << endl;
	
        //break the current time into month day and year
	istringstream stringstr(currentTime);

	string month = "none";
	string day = "none";
	string timeOfDay = "none";
	int timeOfDayInteger = 0;
	
	stringstr >> day;
	stringstr.ignore(1000, ' ');
	stringstr.clear();
	stringstr >> month;
	stringstr.ignore(1000, ' ');
	stringstr.clear();
	
	int pos = stringstr.tellg();
	stringstr.seekg(pos + 3);

//I only want to get the hour
	getline(stringstr, timeOfDay, ':');
	stringstr.clear();

	cerr << timeOfDay << endl;
		
	stringstr.str(timeOfDay);

//Here is where it fails. Everything up to here works as it should, and the flags are all good according to the debugger. The timeOfDayInteger doesn't get set/changed.
	stringstr >> timeOfDayInteger;
		
	if (stringstr.fail()) {
		cout << "An error occurred with the date format." << endl;
		return 1;
	}
	
	cout << day << endl;
	cout << month << endl;
	cout << timeOfDayInteger << endl;
	
	if (month == "Dec" || month == "Jan" || month == "Feb"){
		cout << "It's winter god damn it." << endl;
		
	}else if (month == "Mar" || month == "Apr" || month == "May") {
		cout << "It's spring!" << endl;
	
	
	}else if (month == "Jun" || month == "Jul" || month == "Aug") {
		cout << "It's totally summer." << endl;
	
	
	}else{
		cout << "Ah, fall..." << endl;
	}

    return 0;
}

It worked here. timeOfDayInteger was set to the correct hour.

Try testing it to see whether it succeeds or not:

1
2
3
4
5
6
7
8
if (stringstr >> timeOfDayInteger)
{
    cerr << "Success: " << timeOfDayInteger << endl;
}
else
{
    cerr << "Error setting timeOfDayInteger" << endl;
}
Last edited on
I'm getting the error when I run it. I on a mac (10.6) using Xcode. I'm fairly new to programming. Could this make a difference? Here's my output if it helps:
Welcome to TeaMood.

It is currently: Thu Dec 17 10:13:46 2009
Tea time!
10
Error setting timeOfDayInteger
Thu
Dec
0
It's winter god damn it.
Last edited on
What are you initializing stringstr with on line 19?

There is probably non-numeric text in stringstr (a colon, for example). istream.operator>>( int & ) expects a number.
Last edited on
Topic archived. No new replies allowed.