mardi 8 novembre 2022

how to return all read data to the input stream?

#include <iostream>
using namespace std;

int main ()
{
  int n, x;
  while(!cin.fail()) cin >> n;
  cout << n << ' ';
  //TODO
  cin >> n;
  cout << n;
  for(int i = 0; i < n - 1; i++){
    cin >> x;
    cout << x << ' ';
  }
}

This is my code. The task is to move all the elements to the right. That is, you must first display the last element, and then all the rest.

IMPORTANT!!! It needs to be done without using arrays.

In TODO. You need to write something so that the input stream returns to its original position.

Input example:

4 1 2 3 4

Output example:

4 1 2 3

I tried to read just the last element from the input stream, and then all the rest, but I didn’t succeed (((((

Aucun commentaire:

Enregistrer un commentaire