Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string to int

Status
Not open for further replies.

YelloFello

Programmer
Joined
Jun 30, 2003
Messages
4
Location
US
9 hours on my own...now I just need a little push.

I intput a string array in the following format .... " 55-dave","32-bab",etc.

55 is the score from test
dave is name of test taker.

I can get "55" out of the string but I can't seem to get it converted to an int so I can put it in an array for sorting.
I have tried to use atoi() but, it doesn't like the fact that I try to use a string array in the parameters...

ex.
string b_score[] = {"55-dave"};

int x = atoi( b_score[]);


Is there a better way ( i'm sure there is) to go about this?

1 more question..should I use char arrays instead of strings?...

I've only been at this for 5 weeks so anything is helpful.
 
atoi works. As it says in MSDN:

"The function stops reading the input string at the first character that it cannot recognize as part of a number."

Your example seems a bit odd. Should probably be:

// An array of strings
string b_score[] = {"55-dave"};

// atoi on the string 0 (c_str() returns the string as const char*)
int x = atoi( b_score[0].c_str());

Assuming we're talking about std::string here....






/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top