-3

I am writing a program which is meant to take from the input, an integer N, then read in N number of lines. However when run, it allows me to enter N, then the first line, and then immediately ends without waiting for the following lines. E.g., if I input

3
Line 1
Line 2
Line 3

Then the output is line entered no. 3: Line 1, which happens immediately after I enter Line 1, which suggests it is completing the loop. How can I get it to read further lines from my console input? Looking at other questions and answers they all seem to stem from the use of cin >> var mixed with getline(), but I have avoided that here but still have the problem.

If it's a matter of the console, I'm using Powershell on Win10.

#include <iostream>
#include <string>
using namespace std;

int main(void){
    int N, i;
    string inptstr, temp;

    getline(cin, temp);
    N = stoi(temp);

    for (i = 0; i < N; i++);
        getline(cin, inptstr);
        cout << "Line entered no. " << i << ": " << inptstr << endl;
    
    return 0;
}
YWH
  • 9
  • 4
  • 1
    Is the `;` immediately after `for (i = 0; i < N; i++)` intentional? Most compilers will warn you about this, if you have a sufficiently high warning level enabled. You may want to read this: [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/12149471) – Andreas Wenzel Jul 23 '23 at 10:14
  • Aaah nice catch, that seems to have fixed it, thanks! Also thanks for how to turn on the warnings. Could someone who has the autority please close the question? – YWH Jul 23 '23 at 10:24
  • On Stack Overflow, the proper way to mark a question as solved is to accept an answer (which you have already done). Questions should not be closed only because they have been solved. For example, someone may want to post a better answer, which would not be possible if the question has been closed. – Andreas Wenzel Jul 23 '23 at 10:28
  • I see, thank you! Previously I did get a question closed because it was due to a typo, which I thought to be the case this time – YWH Jul 23 '23 at 10:32
  • Yes, a typo is a [valid close reason](https://stackoverflow.com/help/closed-questions). However, your problem appears to be a bit more than just the misplaced `;`. – Andreas Wenzel Jul 23 '23 at 11:36
  • 1
    Voted to reopen. This problem was **not** caused by typos. It was caused by a fundamental misunderstanding of C++ syntax. Note to close voters: it would be a typo if you or I wrote it, but that doesn't meant it's a typo for a beginner. Try to remember how confused you were when you were starting out. – Pete Becker Jul 23 '23 at 14:10
  • @PeteBecker Sorry mate but it's a typo. Several. A spurious semicolon and two missing braces. VTC. – user207421 Jul 24 '23 at 02:10
  • @user207421 -- do you really not understand that people coming from other languages think that indentation is significant? Yes, it's simple to fix, but not using semicolons was obviously a deliberate decision, not an oversight. Not a typo. It's probably a duplicate, but since you have apparently already voted to close, you can't now vote to close it as a duplicate. – Pete Becker Jul 24 '23 at 12:07

1 Answers1

2

The loop

for (i = 0; i < N; i++);
    getline(cin, inptstr);
    cout << "Line entered no. " << i << ": " << inptstr << endl;

is wrong. Note that you have a ; immediately after for (i = 0; i < N; i++);. This means that this code is equivalent to the following:

for (i = 0; i < N; i++)
{
}

getline(cin, inptstr);
cout << "Line entered no. " << i << ": " << inptstr << endl;

In other words, your loop effectively does nothing.

What you want is probably the following:

for (i = 0; i < N; i++)
{
    getline(cin, inptstr);
    cout << "Line entered no. " << i << ": " << inptstr << endl;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39