CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: whats wrong?

  1. #1
    Join Date
    May 2008
    Posts
    3

    whats wrong?

    All im tryin to do is convert a text file with a list of words to the ascii equivalent. but I always get an error, "Expression: string subscript out of range". Any help would be appriciated.

    int main ( )
    {
    ifstream fin;
    fin.open ( "wordlist.txt" );
    string line;
    int x = 0;

    while ( getline ( fin, line ) )
    {
    while ( x != sizeof ( line ) )
    {
    cout << int (line[x]);
    x++;
    }
    cout << '\n';
    }
    return 0;
    }

  2. #2
    Join Date
    Feb 2003
    Posts
    377

    Re: whats wrong?

    Instead of sizeof(line), use line.size().

    Also, don't forget to reset x to 0 after each run of the inner loop. It might be better to use < instead of != because that would have caught this problem.

  3. #3
    Join Date
    May 2008
    Posts
    3

    Talking Re: whats wrong?

    thz jlou, i forgot about reset x to 0. So dumb of me to have missed that great thz jlou.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured