CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2020
    Posts
    3

    A question from beginner.

    Hi guys, I am new to C++ and got a problem with these codes below.

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main() {
        int n;
        char a[11];
        int i;
    
        cin>>n;
    
        while (n--) {
            cin.getline(a,11);
            cout<<strlen(a)<<endl;
        }
    }
    When i ran it, i got a unexpected "0" immediately after input n.
    Last edited by 2kaud; July 17th, 2020 at 02:40 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: A question from beginner.

    What is the value of n in your test?
    And BTW, have a look at this example.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: A question from beginner.

    [When posting code, please use code tags so that the code is readable. Select Go Advanced, select the inserted formatted code and click '#']
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: A question from beginner.

    This is due to stream extraction (>>) and getline working in slightly different ways.

    >> ignores leading white space (space, tab, newline) before the extraction and extracts up to white space but doesn't extract trailing white space.

    getline() is the opposite. It doesn't ignore leading white space, but does extract the trailing delimiter (which is \n by default).

    So if you say enter 2 for n, what you really enter is 2 then \n (new line)

    >> extracts the 2 but leaves the \n

    getline() then extracts the remaining \n which terminates it and as it hasn't extracted any data, the length of the extracted is 0 - as reported. n is decremented by 1 so is now 1. The getline() again wants input. There is none present so it waits for the input and things work now as probably expected.

    The fix is to extract the remaining white space left by the >> by using .ignore (). See http://www.cplusplus.com/reference/i...stream/ignore/

    Consider:

    Code:
    #include <iostream>
    #include <cstring>
    #include <limits>
    using namespace std;
    
    int main() {
    	int n;
    
    	cin >> n;
    	cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    	while (n--) {
    		char a[11];
    
    		cin.getline(a, 11);
    		cout << strlen(a) << endl;
    	}
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jul 2020
    Posts
    3

    Re: A question from beginner.

    Quote Originally Posted by VictorN View Post
    What is the value of n in your test?
    And BTW, have a look at this example.
    i put 3...and it only showed the first 2 results.

    Thank you.

  6. #6
    Join Date
    Jul 2020
    Posts
    3

    Re: A question from beginner.

    Thank you so much.....you've been a huge help. i will go check how to use cin.ignore().

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