char to int

Data is input from a text file containing on each line an alphabetic name followed by a numberic value. After the data is input via an istream object, the numeric value is intended to be used as an unsigned int, but isn't is it still a set char values? I suspect an explicit conversion is needed to use the value as intended. Correct?

If so, please include an example. Thanks.

Also, anyone have any more info for my char/string thread?
Yes it would be. You would either need to use a function like atoi() or make your own. Or, you can simply get to where you know the integer starts and ends and do something like this:

 
file>>yourInt; //this will work as long as you don't have random data that could confuse the stream 
Last edited on
Thanks firedraco. I'm still a little slow on the uptake though.

For instance if myfile.txt contained:
smith 1234

Then I have:
char name;
int somenumber;
istream ifs("myfile");

How would it go?
ifs >> name >> somenumber;
(where somenumber will eventually be used in a mathematical expression)

The "int" in your example is keyword int right? Where does it go?
You are currently defining name as a char, which is a single character. I think you wanted an std::string instead.

I think that with the way the >> operator works with streams, it will stop at the first space, so you will probably be OK with just stacking it like you did.

Whoops, I meant for you to put whatever you called your int variable there ^^;;
Will edit...
Topic archived. No new replies allowed.