0

I would like to know how to output multiple lines from a for loop. For example, if I want to input a number of subsequent lines N and then another numbers. I am trying to output the inputs that I provided below, but whenever I do this it keeps returning only the last digits and not everything I entered.

This is what I know so far.


#include <iostream>
using namespace std;

int main()
{
    int N, M, num;
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < 3; j++)
        {
          cin >> M;
          for (int k = 0; k < M; k++)
            cout << M << endl;
        } 
    }
    return 0;
}

Input:

2 (This is for N)
1 2 3
4 5 6

or

3
10 20 30
50 100 500
1000 5 0

---------

Output:

1 2 3
4 5 6

10 20 30
50 100 500
1000 5 0
Joe
  • 83
  • 9
  • I think you need braces around the bodies of the for loop. see: https://stackoverflow.com/questions/39579686/why-i-dont-need-brackets-for-loop-and-if-statement – c2huc2hu Aug 15 '18 at 16:05
  • Sorry, I forgot to added here. Still, it doesn't fix my issue. I keeps returning only one digit from the three digits I entered (if I entered 2 3 4 it returns 2 2 2). – Joe Aug 15 '18 at 16:09
  • Could you add them to your question? link to [edit] – c2huc2hu Aug 15 '18 at 16:11
  • There it is. So, I noticed that when I enter the number of lines which is 2 then the digits 1 2 3 it returns the 3 (three times) only instead of 1 2 3. – Joe Aug 15 '18 at 16:14
  • Do you have braces on the inner loops? The second one is likely the problem – c2huc2hu Aug 15 '18 at 16:42
  • I am getting closer. Look at the updated code. Now it is returning the numbers I entered, however, it returns it the wrong way. Now when I enter 1 2 3, it returns 1 2 2 3 3 3. want only 1 2 3 – Joe Aug 15 '18 at 16:53

0 Answers0