Getline with type int

Hi, I am trying to make a program that writes and reads data from a file, and I am trying to read a number from a file and store it in a variable of type int.

1
2
3
getline (data, temp);

content[y].miles = (int) temp;


Data is the name of the file I opened, and temp is a temporary variable of type string since getline() only accepts type string for a parameter. I tried type casting from string to int but that doesn't work either. Is there any way to get a number from a file and store it as an int?
you can use data >> content[y].miles
or getline and then convert the string in a integer http://www.cplusplus.com/forum/articles/9645/
I tend to prefer using stringstreams too, especially in user input:
1
2
3
4
5
6
7
8
9
10
11
12
#include <sstream>
...

fstream f( ... );
...

string s;
getline( f, s );
istringstream iss( s );
if (!(ss >> content[y].miles)) fooey();

...
Ok thanks I'll get working on that in a bit. New problem though - we recently learned stuff with structures and such. But for some reason when I make a array of a structure, and try to edit a member of it in a .h file, it doesn't work. I know that's a bit confusing the way I said it, here:
1
2
3
4
5
struct.h: In member function ‘int structures::srch()’:
struct.h:120: error: no match foroperator=’ in ‘((structures*)this)->structures::results[((structures*)this)->structures::result].structures::cars::name = ((structures*)this)->structures::content[ref]’
/usr/include/c++/4.3/bits/basic_string.h:501: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/include/c++/4.3/bits/basic_string.h:509: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/include/c++/4.3/bits/basic_string.h:520: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

is just one of the errors I get. Line 120 is:
results[result].name = content[ref+0];
That line is in a file called "struct.h." There are no other problems with the program as I have run it before this edit. All the variables you see are declared and perfectly valid.
Last edited on
Never mind I figured out what's wrong - content was also a struct but I wasn't referencing it properly. Forgot haha sorry.
Just drop the class.
Alright stringstream worked perfectly. Thanks guys.
Topic archived. No new replies allowed.