Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 string::substr(begin, end)
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Justin_




PostPosted: Sun Feb 12, 2006 10:10 pm   Post subject: string::substr(begin, end)

Hi friends.

There is something screwy going on with the substr method. Here's my code.


c++:

int Hex::ConvertToInt(std::string hexNum)
{
    int num = 66//66 is an arbitrary initial value.
    std::cout << hexNum << std::endl;
    //transform(hexNum.begin(), hexNum.end(),hexNum.begin(), tolower);
    for (int i = 0; i <= 15; i++)
    {
       std::cout << "i= " << i << std::endl;
       std::cout << "substr= " << hexOrder.substr(i, i+1) << " ";
       if (hexNum == hexOrder.substr(i, i+1))
       {
          num = i;
          break;
       }
    }
    std::cout << num;
    return num;
}


here is the output:

code:

a
i= 0
substr= 0 i= 1
substr= 12 i= 2
substr= 234 i= 3
substr= 3456 i= 4
substr= 45678 i= 5
substr= 56789a i= 6
substr= 6789abc i= 7
substr= 789abcde i= 8
substr= 89abcdef i= 9
substr= 9abcdef i= 10
substr= abcdef i= 11
substr= bcdef i= 12
substr= cdef i= 13
substr= def i= 14
substr= ef i= 15
substr= f 66


the problem is that for each substring there should only be one character. For instance, if the substring was (0,1) only one character is printed. If i=0, then i + 1= 1 and thus (0,1). It should consistantly only be one character but you see, each time it goes it gets to be more characters all the way up to i=8 and then it retracts again. Why? And how to fix?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Feb 12, 2006 11:03 pm   Post subject: (No subject)

Try looking at the docs.

basic_string::substr takes two arguments. The first is the starting index, and the second is the count of characters.

You may wish to use the "at" member function instead, though.
Justin_




PostPosted: Sun Feb 12, 2006 11:23 pm   Post subject: (No subject)

okay, thanks.
wtd




PostPosted: Mon Feb 13, 2006 1:33 am   Post subject: (No subject)

Oh, and you're working too hard.

code:
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string hex_as_string = "FF";
    std::stringstream ss(hex_as_string);
    int num;

    ss >> std::hex >> num;

    std::cout << num << std::endl;

    return 0;
}
Justin_




PostPosted: Tue Feb 14, 2006 5:58 pm   Post subject: (No subject)

Where there are comments I am unsure of what is taking place. Where there are not comments, I know what is taking place.

c++:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string hex_as_string = "FF";
    std::stringstream ss(hex_as_string) //initializing ss to "FF";
    int num;

    ss >>//whats this? std::hex //whats this? >> num;

    std::cout << num << std::endl;

    return 0;
}
Andy




PostPosted: Tue Feb 14, 2006 6:13 pm   Post subject: (No subject)

read up on wtd's stringstream tutorial

found here
wtd




PostPosted: Tue Feb 14, 2006 6:34 pm   Post subject: (No subject)

Streams work a little differently with some values than with others.

The std::hex variable, for instance, tells the stream that that next thing should be treated as a hexidecimal number.
Justin_




PostPosted: Tue Feb 14, 2006 8:52 pm   Post subject: (No subject)

good stuff Laughing
Sponsor
Sponsor
Sponsor
sponsor
Justin_




PostPosted: Tue Feb 14, 2006 9:37 pm   Post subject: (No subject)

there doesn't seem to be an std::bin, how should I convert decimal to binary?
wtd




PostPosted: Wed Feb 15, 2006 12:00 am   Post subject: (No subject)

You're gonna have to do that one the hard way. But even then, it's not that hard. Before you start writing code, really think about how you'd do it by hand.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: