none
A problem involved input and loop RRS feed

  • Question

  • This is a very simple program for the solely purpose to illustrate my problem. For convenience, I use an indefinitely loop.

    My problem is when the program process to the while loop, I enter a character (or any string which size is less than 10

    characters) and then the program just indefinitely display "Enter a line: ".

    My intention is to have the program read the input, display it, display "Enter a line: " and wait for another input. What I do not understand is why the program seems to skip the statements "cin.get(str, ArSize)" and "cout<<str << endl;". Could anyone can explain the reasons to me?

    #include <iostream>
    const int ArSize = 10;
    int main()
    {
    using namespace std;
    char str[ArSize];
    cout << "Enter a line: ";
    while(1)
    {
    cin.get(str, ArSize);
    cout << str << endl;
    cout << "Enter a line: ";
    }
    return 0;
    }



    Wednesday, July 31, 2013 8:59 AM

Answers

  • With further experiments, I finally find out the answer to the problem.

    To be more specific, if I input aaaa( or aaaaaaaaa, 9 'a's, it does not matter), the code I post will write the "aaaa" into str and display it once. Then, here comes the crash. Because the get() method will read the input until it meets a '\n', carriage return, and get() method will just leave the '\n' in the input sequence in contrast to getline() method discarding it. When the program execute to the statement "cin.get(str, ArSize)" the second time, it encounters the character '\n' again. In other situations, the get() method would just bypass the '\n' and read the next input into variables. But if the variable is a char array, the get() method just cant get pass the '\n' and assumes there is nothing to read and set the value of char array to 0.

    Well, this is the whole story, at least I think.

    You are more than welcome to share your thoughts with me.

    This problem took me an hour to solve... good luck with you guys learning!


    • Marked as answer by herbgem Wednesday, July 31, 2013 10:20 AM
    • Edited by herbgem Wednesday, July 31, 2013 10:21 AM
    Wednesday, July 31, 2013 10:19 AM

All replies

  • I figure out the indefinitely repeatedly displaying "Enter a line: " problem can be solved by using "cin.getline(str, ArSize)" or  inserting a statement "cin.get()" in the loop after "cin.get(str, ArSize)". The function of either mean is to get rid of the carriage return, '\n', which is invoked at the end of your input.

    But replacing the char array variable with an int one, without modifying the originally posted code, you will have a normally function program.

    So, the problem seems to be about the relationship between char array and input mechanism.

    Wednesday, July 31, 2013 9:21 AM
  • With further experiments, I finally find out the answer to the problem.

    To be more specific, if I input aaaa( or aaaaaaaaa, 9 'a's, it does not matter), the code I post will write the "aaaa" into str and display it once. Then, here comes the crash. Because the get() method will read the input until it meets a '\n', carriage return, and get() method will just leave the '\n' in the input sequence in contrast to getline() method discarding it. When the program execute to the statement "cin.get(str, ArSize)" the second time, it encounters the character '\n' again. In other situations, the get() method would just bypass the '\n' and read the next input into variables. But if the variable is a char array, the get() method just cant get pass the '\n' and assumes there is nothing to read and set the value of char array to 0.

    Well, this is the whole story, at least I think.

    You are more than welcome to share your thoughts with me.

    This problem took me an hour to solve... good luck with you guys learning!


    • Marked as answer by herbgem Wednesday, July 31, 2013 10:20 AM
    • Edited by herbgem Wednesday, July 31, 2013 10:21 AM
    Wednesday, July 31, 2013 10:19 AM