开发者

My for loop is executing more times than is expected

开发者 https://www.devze.com 2023-01-26 09:27 出处:网络
I have the following for loop executing within my program and I can\'t see how it\'s design correlates with the output I\'m receiving.

I have the following for loop executing within my program and I can't see how it's design correlates with the output I'm receiving.

cout << no_of_lines << endl;
for (int count = 0; count < no_of_lines + 1; count ++)
{
    getline(Device, line, '=');
    cout << line << endl;
}

This is the output:

3
DeviceName
GPU
Manufacturer
Intel
GraphicalRam
128MB

And this is the file DeviceLis开发者_开发百科t

DeviceName=GPU
Manufacturer=Intel 
GraphicalRam=128MB

In the loop, no_of_lines refers to the number of lines in the file, in this case 3. I have provided this output as verification that the loop is only executing once per line. Can anyone tell me why this loop is executing more times than is expected? I'm guessing it's because of my inclusion of = as the deliminator, and that the loop is somehow executing an additional time before incrementing, but then why does it stop on the deliminator on the last line, requiring me to add 1 to the loop limit?


This http://www.cplusplus.com/reference/string/getline/ is your friend here. The documentation says that when delem character is passed to getline, it reads up to delem char is found or end of file is reached. '\n' is not treated differently. Once it find delem char it discards it and fills line whatever it read until that point. Next time you call getline, it continues to read from where it left. so in this case, each call reads as follows.

DeviceName
GPU\nManufacturer
Intel\nGraphicalRam
128MB\n

'\n' in the above string is basically newline character. it's not really backslash followed by n (2 characters). its just single newline character '\n'. Just for understanding purposes I used it that way.

For clarity, here is the complete code(compiled and tested).

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int no_of_lines = 3;
    string line;
    cout << no_of_lines << endl;
    for (int count = 0; count < no_of_lines + 1; count++)
    {
        getline(cin, line, '=');
        cout << line << endl ;
    }
    return 0;
}

By now I hope its clear to you why you need to call getline 4 times.


your loop executes no_of_lines+1 times.

I assume you want the delimiter to be '='. However, when the delimiter is '=', then \n is not a delimiter. Hence line will contain \n (say, on the second getline). And so the number of lines displayed is not equal to the number of times the loop executes.


When you use = as line delimiter, \n is not used as line delimiter.

So you get strings containing newline characters.

When you output such a string, it's presented as two or more lines (due to the newlines it contains).

Cheers & hth.,


You want it to count thrice, but your loop is counting 0,1,2,3,4, i.e 5 iterations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号