Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,108 people online right now. Registration is fast and FREE... Join Now!




Reading Numbers For A String For Numbers And Letters

 
Reply to this topicStart new topic

Reading Numbers For A String For Numbers And Letters

WsaENoTsoCK
6 Sep, 2006 - 11:02 AM
Post #1

New D.I.C Head
*

Joined: 6 Sep, 2006
Posts: 4


My Contributions
Heyz dudes I seem to have a problem with getting numbers from a string that consist of numbers and letters.

My Code is as follows:

CODE


while (!cin.eof())
{
   getline(cin,input);
   pos = input.find_first_of("(");
   npos = pos + 1;
   p1 = atoi(&input[npos]);
   pos = input.find_first_of("(",npos);
   npos = pos + 1;
   p2 = atoi(&input[npos]);
   pos = input.find_first_of(")",npos);
   npos = pos + 2;
   li = atoi(&input[npos]);
   cout<<li<<endl;
}



The Input is as follows:

file1-0(20%) file2-0(34%) 199
file1-1(30%) file2-1(35%) 12
file1-2(40%) file2-2(50%) 45

The Output is as follows:

199
129<--- incorrect output. Should be 12.
459<--- incorrect output. Should be 45.

This post has been edited by WsaENoTsoCK: 6 Sep, 2006 - 11:10 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:04AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month