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

Thread: String problems

  1. #1
    Join Date
    Nov 2006
    Posts
    292

    String problems

    I'm trying to access a string array by outputting each member of the array like so ...

    Code:
    int main(){
        string str;
        string lines[5] ={ "Line1:", "Line2:", "Line3", "Line4:", "Line5"};
        ifstream read("test.txt");
        while(getline(read, str)){
         int n;
         n++;
        cout << lines[n++] /* <-- Why can't this change from 0 to 5 ?? */ << endl;}
        return 0;
    }
    As you can see I'm not outputting lines array like: lines[0]/or lines[5] - I thought it would be interesting if you could make the array member number by declaring int and counting it in a for loop at first. All I got back was this ...

    ♠ @ x*( τ◄@ ☺ ░♫p
    ♠ @ x*( τ◄@ ☺ ░♫p
    ♠ @ x*( τ◄@ ☺ ░♫p
    ♠ @ x*( τ◄@ ☺ ░♫p
    ♠ @ x*( τ◄@ ☺ ░♫p
    ♠ @ x*( τ◄@ ☺ ░♫p
    I'm guessing that's a sort of error. Can't figure out what is happening at this point of time, but I hypothesize that int n = 0; is being looped faster than expected. Or, the for loop is giving n an unrecognizable number when n++ is being counted within the for loop.

    Is there no way to change that number in run-time? Must it always be a specified number before compilation?
    Last edited by dellthinker; March 4th, 2011 at 11:46 AM.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: String problems

    Code:
    while(getline(read, str))
    {
        int n;
        n++;
        cout << lines[n++] /* <-- Why can't this change from 0 to 5 ?? */ << endl;
    }
    1. You declare "n" in the loop, but do not initialize it.

    2). In two places you have "n++"

    3. no checks if n is out of bounds ... what if there are 20 lines in the file?

    4. What is the purpose of the getline ... you do not use "str" anywhere

    Code:
    int n = 0;
    
    while(getline(read, str))
    {
       ++n
       if (n<5)
       {
          cout << lines[n]  << endl;
       }
    }
    Last edited by Philip Nicoletti; March 4th, 2011 at 11:55 AM.

  3. #3
    Join Date
    Apr 2008
    Posts
    725

    Re: String problems

    as phil said, you think n = 0, but nowhere in your code have you assigned 0 to n.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: String problems

    Quote Originally Posted by dellthinker View Post
    I'm guessing that's a sort of error. Can't figure out what is happening at this point of time, but I hypothesize
    Why hypothesize? Just use the debugger and it would have been obvious as to what the value of n is.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2006
    Posts
    292

    Re: String problems

    Quote Originally Posted by Paul McKenzie View Post
    Why hypothesize?
    Because I learn better if I try to find out 'why' something went wrong.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: String problems

    Quote Originally Posted by dellthinker View Post
    Because I learn better if I try to find out 'why' something went wrong.
    And you use the debugger to learn. Do you consider using the debugger 'not learning'?

    You would have seen that n was a crazy number. OK, so now you say, "why is the value of n crazy, since that's the reason for the problem I'm seeing"? Then you go to your book, and look up initializations and local variables to guide you to what you're looking for. Thus, you have used the debugger to aid your learning.

    Regards,

    Paul McKenzie

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