-
March 4th, 2011, 11:44 AM
#1
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.
-
March 4th, 2011, 11:52 AM
#2
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.
-
March 4th, 2011, 12:05 PM
#3
Re: String problems
as phil said, you think n = 0, but nowhere in your code have you assigned 0 to n.
-
March 4th, 2011, 01:39 PM
#4
Re: String problems
Originally Posted by dellthinker
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
-
March 4th, 2011, 01:56 PM
#5
Re: String problems
Originally Posted by Paul McKenzie
Why hypothesize?
Because I learn better if I try to find out 'why' something went wrong.
-
March 4th, 2011, 02:01 PM
#6
Re: String problems
Originally Posted by dellthinker
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|